From 7d620988d8c9a146278f4fa8aecc6a1e71c93022 Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 9 Mar 2026 11:25:36 +0100 Subject: [PATCH] Refactor envelope finalization into a private method Extracted envelope finalization logic from the foreach loop into a new private Finalize(Envelope envelope) method. This improves code readability and maintainability by encapsulating all steps of the finalization process without changing functionality. --- .../Jobs/FinalizeDocumentJob.cs | 145 +++++++++--------- 1 file changed, 73 insertions(+), 72 deletions(-) diff --git a/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs b/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs index 8cece9fb..9e336c80 100644 --- a/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs +++ b/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs @@ -55,81 +55,11 @@ public class FinalizeDocumentJob(IOptions options, IConfiguration if (envelopes.Count > 0) logger.LogInformation("Found [{count}] completed envelopes.", envelopes.Count); - var total = envelopes.Count; - var current = 1; - foreach (var envelope in envelopes) { try { - var envelopeData = GetEnvelopeData(envelope.Id); - - if (envelopeData is null) - { - logger.LogWarning("EnvelopeData could not be loaded for Id [{id}]!", envelope.Id); - throw new ArgumentNullException(nameof(EnvelopeData)); - } - - logger.LogDebug("Burning Annotations to pdf ..."); - var burnedDocument = BurnAnnotationsToPdf(envelopeData); - if (burnedDocument is null) - { - logger.LogWarning("Document could not be finalized!"); - throw new ApplicationException("Document could not be finalized"); - } - - if (actionService?.CreateReport(envelope) == false) - { - logger.LogWarning("Document Report could not be created!"); - throw new ApplicationException("Document Report could not be created"); - } - - logger.LogDebug("Creating report.."); - var report = reportCreator!.CreateReport(envelope); - logger.LogDebug("Report created!"); - - logger.LogDebug("Merging documents ..."); - var mergedDocument = pdfMerger!.MergeDocuments(burnedDocument, report); - logger.LogDebug("Documents merged!"); - - var outputDirectoryPath = Path.Combine(_config.ExportPath, _parentFolderUid); - logger.LogDebug("oOutputDirectoryPath is {outputDirectoryPath}", outputDirectoryPath); - if (!Directory.Exists(outputDirectoryPath)) - { - logger.LogDebug("Directory not existing. Creating ... "); - Directory.CreateDirectory(outputDirectoryPath); - } - - var outputFilePath = Path.Combine(outputDirectoryPath, $"{envelope.Uuid}.pdf"); - logger.LogDebug("Writing finalized Pdf to disk.."); - logger.LogInformation("Output path is [{outputFilePath}]", outputFilePath); - - try - { - File.WriteAllBytes(outputFilePath, mergedDocument); - } - catch (Exception ex) - { - logger.LogWarning("Could not export final document to disk!"); - throw new ExportDocumentException("Could not export final document to disk!", ex); - } - - logger.LogDebug("Writing EB-bytes to database..."); - UpdateFileDb(outputFilePath, envelope.Id); - - if (!SendFinalEmails(envelope)) - { - throw new ApplicationException("Final emails could not be sent!"); - } - - logger.LogInformation("Report-mails successfully sent!"); - - logger.LogDebug("Setting envelope status.."); - if (actionService?.FinalizeEnvelope(envelope) == false) - { - logger.LogWarning("Envelope could not be finalized!"); - throw new ApplicationException("Envelope could not be finalized"); - } + Finalize(envelope); } catch (Exception ex) { @@ -137,13 +67,84 @@ public class FinalizeDocumentJob(IOptions options, IConfiguration logger.LogWarning(ex, "Unhandled exception while working envelope [{id}]", envelope.Id); } - current += 1; logger.LogInformation("Envelope [{id}] finalized!", envelope.Id); } logger.LogDebug("Completed job {jobId} successfully!", jobId); } + private void Finalize(Envelope envelope) + { + var envelopeData = GetEnvelopeData(envelope.Id); + + if (envelopeData is null) + { + logger.LogWarning("EnvelopeData could not be loaded for Id [{id}]!", envelope.Id); + throw new ArgumentNullException(nameof(EnvelopeData)); + } + + logger.LogDebug("Burning Annotations to pdf ..."); + var burnedDocument = BurnAnnotationsToPdf(envelopeData); + if (burnedDocument is null) + { + logger.LogWarning("Document could not be finalized!"); + throw new ApplicationException("Document could not be finalized"); + } + + if (actionService?.CreateReport(envelope) == false) + { + logger.LogWarning("Document Report could not be created!"); + throw new ApplicationException("Document Report could not be created"); + } + + logger.LogDebug("Creating report.."); + var report = reportCreator!.CreateReport(envelope); + logger.LogDebug("Report created!"); + + logger.LogDebug("Merging documents ..."); + var mergedDocument = pdfMerger!.MergeDocuments(burnedDocument, report); + logger.LogDebug("Documents merged!"); + + var outputDirectoryPath = Path.Combine(_config.ExportPath, _parentFolderUid); + logger.LogDebug("oOutputDirectoryPath is {outputDirectoryPath}", outputDirectoryPath); + if (!Directory.Exists(outputDirectoryPath)) + { + logger.LogDebug("Directory not existing. Creating ... "); + Directory.CreateDirectory(outputDirectoryPath); + } + + var outputFilePath = Path.Combine(outputDirectoryPath, $"{envelope.Uuid}.pdf"); + logger.LogDebug("Writing finalized Pdf to disk.."); + logger.LogInformation("Output path is [{outputFilePath}]", outputFilePath); + + try + { + File.WriteAllBytes(outputFilePath, mergedDocument); + } + catch (Exception ex) + { + logger.LogWarning("Could not export final document to disk!"); + throw new ExportDocumentException("Could not export final document to disk!", ex); + } + + logger.LogDebug("Writing EB-bytes to database..."); + UpdateFileDb(outputFilePath, envelope.Id); + + if (!SendFinalEmails(envelope)) + { + throw new ApplicationException("Final emails could not be sent!"); + } + + logger.LogInformation("Report-mails successfully sent!"); + + logger.LogDebug("Setting envelope status.."); + if (actionService?.FinalizeEnvelope(envelope) == false) + { + logger.LogWarning("Envelope could not be finalized!"); + throw new ApplicationException("Envelope could not be finalized"); + } + } + private void UpdateFileDb(string filePath, long envelopeId) { try