Refactor FinalizeDocumentJob to async and use docStatusRepo

Refactored FinalizeDocumentJob to make the Finalize method asynchronous and fetch document annotations from docStatusRepo instead of using GetEnvelopeData. Updated constructor to inject IRepository<Domain.Entities.DocumentStatus>. Improved logging and removed obsolete envelopeData checks.
This commit is contained in:
2026-03-09 15:32:32 +01:00
parent ead33ab2e7
commit 69499273cc

View File

@@ -18,7 +18,7 @@ using Microsoft.EntityFrameworkCore;
namespace EnvelopeGenerator.ServiceHost.Jobs;
[Obsolete("ActionService is a placeholder service added by copilot. Migrate the actual logic from CommonServices.Jobs")]
public class FinalizeDocumentJob(IOptions<WorkerOptions> options, IConfiguration config, ILogger<FinalizeDocumentJob> logger, TempFiles tempFiles, ActionService actionService, PDFBurner pdfBurner, PDFMerger pdfMerger, ReportCreator reportCreator, ReportModel _reportModel, MSSQLServer _database, GdViewer? _gdViewer, LicenseManager licenseManager, IMediator mediator, IRepository<Envelope> envRepo)
public class FinalizeDocumentJob(IOptions<WorkerOptions> options, IConfiguration config, ILogger<FinalizeDocumentJob> logger, TempFiles tempFiles, ActionService actionService, PDFBurner pdfBurner, PDFMerger pdfMerger, ReportCreator reportCreator, ReportModel _reportModel, MSSQLServer _database, GdViewer? _gdViewer, LicenseManager licenseManager, IMediator mediator, IRepository<Envelope> envRepo, IRepository<Domain.Entities.DocumentStatus> docStatusRepo)
{
private readonly WorkerOptions _options = options.Value;
@@ -60,7 +60,7 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, IConfiguration
{
try
{
Finalize(envelope);
await Finalize(envelope, cancel);
}
catch (Exception ex)
{
@@ -74,17 +74,11 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, IConfiguration
}
}
private void Finalize(Envelope envelope)
private async Task Finalize(Envelope envelope, CancellationToken cancel)
{
var envelopeData = GetEnvelopeData(envelope.Id);
var annotations = await docStatusRepo.Where(s => s.EnvelopeId == envelope.Id).Select(s => s.Value).ToListAsync(cancel);
var burnedDocument = pdfBurner!.BurnAnnotsToPDF(envelope.DefaultDocument.ByteData!, annotations, envelope.Id);
if (envelopeData is null)
{
logger.LogWarning("EnvelopeData could not be loaded for Id [{id}]!", envelope.Id);
throw new ArgumentNullException(nameof(EnvelopeData));
}
var burnedDocument = BurnAnnotationsToPdf(envelopeData);
if (burnedDocument is null)
{
logger.LogWarning("Document could not be finalized!");
@@ -206,7 +200,7 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, IConfiguration
{
if (actionService?.CompleteEnvelope(envelope, receiver.Receiver) == false)
{
logger?.LogError(new Exception("CompleteEnvelope failed"), "Envelope could not be completed for receiver [{0}]", receiver.Receiver?.EmailAddress);
logger?.LogError(new Exception("CompleteEnvelope failed"), "Envelope could not be completed for receiver [{email}]", receiver.Receiver?.EmailAddress);
return false;
}
}