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);