Refactor jobs to use EnvelopeDto/ReceiverDto instead of entities
Refactored ActionService and FinalizeDocumentJob to use EnvelopeDto and ReceiverDto in place of domain entities. Updated method signatures, internal logic, and envelope receiver handling to operate on DTOs. Improved logging, removed obsolete code, and added necessary using statements for DTO namespaces. Also updated document retrieval logic and removed null-conditional operator from actionService calls.
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using EnvelopeGenerator.Application.Common.Dto;
|
||||
using EnvelopeGenerator.Application.Common.Dto.Receiver;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
|
||||
namespace EnvelopeGenerator.ServiceHost.Jobs;
|
||||
@@ -10,25 +12,25 @@ namespace EnvelopeGenerator.ServiceHost.Jobs;
|
||||
public class ActionService(IRepository<History> histRepo)
|
||||
{
|
||||
[Obsolete("This is a placeholder service added by copilot. Migrate the actual logic from CommonServices.Jobs")]
|
||||
public bool CreateReport(Envelope envelope, CancellationToken cancel = default)
|
||||
public bool CreateReport(EnvelopeDto envelope, CancellationToken cancel = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
[Obsolete("This is a placeholder service added by copilot. Migrate the actual logic from CommonServices.Jobs")]
|
||||
public bool FinalizeEnvelope(Envelope envelope, CancellationToken cancel = default)
|
||||
public bool FinalizeEnvelope(EnvelopeDto envelope, CancellationToken cancel = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
[Obsolete("This is a placeholder service added by copilot. Migrate the actual logic from CommonServices.Jobs")]
|
||||
public bool CompleteEnvelope(Envelope envelope, CancellationToken cancel = default)
|
||||
public bool CompleteEnvelope(EnvelopeDto envelope, CancellationToken cancel = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
[Obsolete("This is a placeholder service added by copilot. Migrate the actual logic from CommonServices.Jobs")]
|
||||
public bool CompleteEnvelope(Envelope envelope, Receiver receiver, CancellationToken cancel = default)
|
||||
public bool CompleteEnvelope(EnvelopeDto envelope, ReceiverDto receiver, CancellationToken cancel = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
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;
|
||||
@@ -35,7 +36,7 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, ILogger<Finali
|
||||
|
||||
public bool RethrowOnError { get; set; } = true;
|
||||
|
||||
public async Task ExecuteAsync(CancellationToken cancel = default)
|
||||
public async Task ExecuteAsync(IEnumerable<EnvelopeDto> envelopes, CancellationToken cancel = default)
|
||||
{
|
||||
var gdPictureKey = _options.GdPictureLicenseKey;
|
||||
tempFiles.Create();
|
||||
@@ -43,16 +44,6 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, ILogger<Finali
|
||||
|
||||
_config = await mediator.Send(new ReadDefaultConfigQuery(), cancel);
|
||||
|
||||
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);
|
||||
|
||||
if (envelopes.Count > 0)
|
||||
logger.LogInformation("Found [{count}] completed envelopes.", envelopes.Count);
|
||||
|
||||
foreach (var envelope in envelopes)
|
||||
{
|
||||
try
|
||||
@@ -61,20 +52,29 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, ILogger<Finali
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Unhandled exception while working envelope [{id}]", envelope.Id);
|
||||
logger.LogWarning(ex, "An unexpected exception was ignored while processing the envelope with ID [{id}].", envelope.Id);
|
||||
|
||||
if(RethrowOnError)
|
||||
throw;
|
||||
}
|
||||
|
||||
logger.LogInformation("Envelope [{id}] finalized!", envelope.Id);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Finalize(Envelope envelope, CancellationToken cancel)
|
||||
public async Task ExecuteAsync(CancellationToken cancel = default)
|
||||
{
|
||||
var envelopes = await mediator.Send(new ReadEnvelopeQuery()
|
||||
{
|
||||
Status = new () { Include = [EnvelopeStatus.EnvelopeCompletelySigned] },
|
||||
MinMinutesSinceLastChange = CompleteWaitTime,
|
||||
}, cancel);
|
||||
|
||||
await ExecuteAsync(envelopes, cancel);
|
||||
}
|
||||
|
||||
private async Task Finalize(EnvelopeDto 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.Documents?.FirstOrDefault()?.ByteData!, annotations, envelope.Id)
|
||||
?? throw new ApplicationException("Document could not be finalized");
|
||||
|
||||
actionService.CreateReport(envelope, cancel);
|
||||
@@ -104,10 +104,10 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, ILogger<Finali
|
||||
|
||||
SendFinalEmails(envelope);
|
||||
|
||||
actionService?.FinalizeEnvelope(envelope, cancel);
|
||||
actionService.FinalizeEnvelope(envelope, cancel);
|
||||
}
|
||||
|
||||
private bool SendFinalEmails(Envelope envelope)
|
||||
private bool SendFinalEmails(EnvelopeDto envelope)
|
||||
{
|
||||
var mailToCreator = (FinalEmailType)(envelope.FinalEmailToCreator ?? 0);
|
||||
var mailToReceivers = (FinalEmailType)(envelope.FinalEmailToReceivers ?? 0);
|
||||
@@ -129,9 +129,9 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, ILogger<Finali
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool SendFinalEmailToCreator(Envelope envelope, FinalEmailType mailToCreator)
|
||||
private bool SendFinalEmailToCreator(EnvelopeDto envelope, FinalEmailType mailToCreator)
|
||||
{
|
||||
if (actionService?.CompleteEnvelope(envelope) == false)
|
||||
if (actionService.CompleteEnvelope(envelope) == false)
|
||||
{
|
||||
logger?.LogError(new Exception("CompleteEnvelope failed"), "Envelope could not be completed for receiver [{email}]", envelope.User?.Email);
|
||||
return false;
|
||||
@@ -140,15 +140,12 @@ public class FinalizeDocumentJob(IOptions<WorkerOptions> options, ILogger<Finali
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool SendFinalEmailToReceivers(Envelope envelope, FinalEmailType mailToReceivers)
|
||||
private bool SendFinalEmailToReceivers(EnvelopeDto envelope, FinalEmailType mailToReceivers)
|
||||
{
|
||||
foreach (var receiver in envelope.EnvelopeReceivers ?? Enumerable.Empty<EnvelopeReceiver>())
|
||||
foreach (var receiver in envelope.EnvelopeReceivers ?? [])
|
||||
{
|
||||
if (actionService?.CompleteEnvelope(envelope, receiver.Receiver) == false)
|
||||
{
|
||||
logger?.LogError(new Exception("CompleteEnvelope failed"), "Envelope could not be completed for receiver [{email}]", receiver.Receiver?.EmailAddress);
|
||||
if (!actionService.CompleteEnvelope(envelope, receiver.Receiver!))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user