Refactor document save to use async repository update
Replaced manual SQL and file read with async repository update for saving final document bytes. Removed obsolete helper methods and cleaned up unused imports for improved maintainability and testability.
This commit is contained in:
@@ -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<WorkerOptions> 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<WorkerOptions> 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<WorkerOptions> 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);
|
||||
|
||||
Reference in New Issue
Block a user