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
{
logger.LogDebug("Loading Configuration..");
_config = await mediator.Send(new ReadDefaultConfigQuery(), cancel);
logger.LogDebug("DocumentPath: [{documentPath}]", _config.DocumentPath);
logger.LogDebug("ExportPath: [{exportPath}]", _config.ExportPath);
var envelopes = await envRepo
.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;
var sql = $"SELECT * FROM TBSIG_ENVELOPE WHERE STATUS = {completeStatus} AND DATEDIFF(minute, CHANGED_WHEN, GETDATE()) >= {CompleteWaitTime} ORDER BY GUID";
var table = _database.GetDatatable(sql);
if (envelopes.Count > 0)
logger.LogInformation("Found [{count}] completed envelopes.", envelopes.Count);
var envelopeIds = table.Rows.Cast<DataRow>()
.Select(r => r.Field<int>("GUID"))
.ToList();
if (envelopeIds.Count > 0)
{
logger.LogInformation("Found [{count}] completed envelopes.", envelopeIds.Count);
}
var total = envelopeIds.Count;
var total = envelopes.Count;
var current = 1;
foreach (var id in envelopeIds)
foreach (var envelope in envelopes)
{
logger.LogInformation("Finalizing Envelope [{id}] ({current}/{total})", id, current, total);
try
{
var envelope = await envRepo.Where(e => e.Id == id).SingleOrDefaultAsync(cancel);
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);
var envelopeData = GetEnvelopeData(envelope.Id);
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));
}
@@ -152,11 +136,11 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, IConfiguration
catch (Exception 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;
logger.LogInformation("Envelope [{id}] finalized!", id);
logger.LogInformation("Envelope [{id}] finalized!", envelope.Id);
}
logger.LogDebug("Completed job {jobId} successfully!", jobId);