From d6e2690bb8195463fb01b04286870a04987f63fb Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 9 Mar 2026 09:38:08 +0100 Subject: [PATCH] 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. --- .../Jobs/FinalizeDocumentJob.cs | 44 ++++++------------- 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs b/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs index d8020afa..cf95a6ff 100644 --- a/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs +++ b/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs @@ -45,46 +45,30 @@ public class FinalizeDocumentJob(IOptions 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() - .Select(r => r.Field("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 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);