Refactor envelope fetch to use EF LINQ instead of SQL

Replaced raw SQL and DataTable usage with Entity Framework LINQ queries for retrieving completed envelopes. The process now works directly with envelope entities, improving code readability, maintainability, and leveraging EF's querying capabilities. Logging and error handling have been updated to use envelope properties directly.
This commit is contained in:
2026-03-09 09:38:08 +01:00
parent f04385a03c
commit d6e2690bb8

View File

@@ -45,46 +45,30 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, IConfiguration
try try
{ {
logger.LogDebug("Loading Configuration..");
_config = await mediator.Send(new ReadDefaultConfigQuery(), cancel); _config = await mediator.Send(new ReadDefaultConfigQuery(), cancel);
logger.LogDebug("DocumentPath: [{documentPath}]", _config.DocumentPath); var envelopes = await envRepo
logger.LogDebug("ExportPath: [{exportPath}]", _config.ExportPath); .Where(e => e.Status == EnvelopeStatus.EnvelopeCompletelySigned
&& e.ChangedWhen.HasValue
&& EF.Functions.DateDiffMinute(e.ChangedWhen.Value, DateTime.Now) >= CompleteWaitTime)
.OrderBy(e => e.Id)
.ToListAsync(cancel);
var completeStatus = EnvelopeStatus.EnvelopeCompletelySigned; if (envelopes.Count > 0)
var sql = $"SELECT * FROM TBSIG_ENVELOPE WHERE STATUS = {completeStatus} AND DATEDIFF(minute, CHANGED_WHEN, GETDATE()) >= {CompleteWaitTime} ORDER BY GUID"; logger.LogInformation("Found [{count}] completed envelopes.", envelopes.Count);
var table = _database.GetDatatable(sql);
var envelopeIds = table.Rows.Cast<DataRow>() var total = envelopes.Count;
.Select(r => r.Field<int>("GUID"))
.ToList();
if (envelopeIds.Count > 0)
{
logger.LogInformation("Found [{count}] completed envelopes.", envelopeIds.Count);
}
var total = envelopeIds.Count;
var current = 1; var current = 1;
foreach (var id in envelopeIds) foreach (var envelope in envelopes)
{ {
logger.LogInformation("Finalizing Envelope [{id}] ({current}/{total})", id, current, total);
try try
{ {
var envelope = await envRepo.Where(e => e.Id == id).SingleOrDefaultAsync(cancel); var envelopeData = GetEnvelopeData(envelope.Id);
if (envelope is null)
{
logger.LogWarning("Envelope could not be loaded for Id [{id}]!", id);
throw new ArgumentNullException(nameof(EnvelopeData));
}
logger.LogDebug("Loading Envelope Data..");
var envelopeData = GetEnvelopeData(id);
if (envelopeData is null) if (envelopeData is null)
{ {
logger.LogWarning("EnvelopeData could not be loaded for Id [{id}]!", id); logger.LogWarning("EnvelopeData could not be loaded for Id [{id}]!", envelope.Id);
throw new ArgumentNullException(nameof(EnvelopeData)); throw new ArgumentNullException(nameof(EnvelopeData));
} }
@@ -152,11 +136,11 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, IConfiguration
catch (Exception ex) catch (Exception ex)
{ {
logger.LogError(ex); logger.LogError(ex);
logger.LogWarning(ex, "Unhandled exception while working envelope [{id}]", id); logger.LogWarning(ex, "Unhandled exception while working envelope [{id}]", envelope.Id);
} }
current += 1; current += 1;
logger.LogInformation("Envelope [{id}] finalized!", id); logger.LogInformation("Envelope [{id}] finalized!", envelope.Id);
} }
logger.LogDebug("Completed job {jobId} successfully!", jobId); logger.LogDebug("Completed job {jobId} successfully!", jobId);