diff --git a/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs b/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs index f7e7f4c3..95693b04 100644 --- a/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs +++ b/EnvelopeGenerator.ServiceHost/Jobs/FinalizeDocumentJob.cs @@ -1,19 +1,20 @@ -using System.Data; +using DigitalData.Core.Abstraction.Application.Repository; using DigitalData.Modules.Database; +using EnvelopeGenerator.Application.Common.Dto; +using EnvelopeGenerator.Application.Configuration.Queries; +using EnvelopeGenerator.Application.Envelopes.Queries; using EnvelopeGenerator.Domain.Constants; using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.ServiceHost.Exceptions; -using EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument; -using GdPicture14; -using Microsoft.Data.SqlClient; -using Microsoft.Extensions.Options; using EnvelopeGenerator.ServiceHost.Extensions; +using EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument; +using GdPicture.Internal.MSOfficeBinary.translator.Spreadsheet.XlsFileFormat.Records; +using GdPicture14; using MediatR; -using EnvelopeGenerator.Application.Configuration.Queries; -using EnvelopeGenerator.Application.Common.Dto; -using EnvelopeGenerator.Application.Envelopes.Queries; -using DigitalData.Core.Abstraction.Application.Repository; +using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; +using System.Data; namespace EnvelopeGenerator.ServiceHost.Jobs; @@ -77,15 +78,10 @@ public class FinalizeDocumentJob(IOptions options, IConfiguration private async Task Finalize(Envelope envelope, CancellationToken cancel) { 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); + var burnedDocument = pdfBurner!.BurnAnnotsToPDF(envelope.DefaultDocument.ByteData!, annotations, envelope.Id) + ?? throw new ApplicationException("Document could not be finalized"); - if (burnedDocument is null) - { - logger.LogWarning("Document could not be finalized!"); - throw new ApplicationException("Document could not be finalized"); - } - - if (actionService?.CreateReport(envelope) == false) + if (!actionService.CreateReport(envelope, cancel)) { logger.LogWarning("Document Report could not be created!"); throw new ApplicationException("Document Report could not be created"); @@ -113,7 +109,8 @@ public class FinalizeDocumentJob(IOptions options, IConfiguration throw new ExportDocumentException("Could not export final document to disk!", ex); } - UpdateFileDb(outputFilePath, envelope.Id); + var outputFile = await File.ReadAllBytesAsync(outputFilePath, cancel); + await envRepo.UpdateAsync(e => e.DocResult = outputFile, e => e.Id == envelope.Id, cancel); if (!SendFinalEmails(envelope)) throw new ApplicationException("Final emails could not be sent!"); @@ -127,36 +124,6 @@ public class FinalizeDocumentJob(IOptions options, IConfiguration } } - private void UpdateFileDb(string filePath, long envelopeId) - { - try - { - var imageData = ReadFile(filePath); - if (imageData is null) - { - return; - } - - var query = $"UPDATE TBSIG_ENVELOPE SET DOC_RESULT = @ImageData WHERE GUID = {envelopeId}"; - using var command = new SqlCommand(query, _database!.GetConnection); - command.Parameters.Add(new SqlParameter("@ImageData", imageData)); - command.ExecuteNonQuery(); - } - catch (Exception ex) - { - logger?.LogError(ex); - } - } - - private static byte[]? ReadFile(string path) - { - var fileInfo = new FileInfo(path); - var numBytes = fileInfo.Length; - using var stream = new FileStream(path, FileMode.Open, FileAccess.Read); - using var reader = new BinaryReader(stream); - return reader.ReadBytes((int)numBytes); - } - private bool SendFinalEmails(Envelope envelope) { var mailToCreator = (FinalEmailType)(envelope.FinalEmailToCreator ?? 0);