feat(SendEmailBehavior): make SendEmailBehavior fully async and record history
- Added ISender dependency to send history commands - Refactored SendFinalEmailToCreator and SendFinalEmailToReceivers to async - Log attachment inclusion for emails - Use Task.WhenAll for sending to multiple receivers
This commit is contained in:
parent
40cc467b47
commit
cbe2acc37d
@ -1,4 +1,6 @@
|
||||
using EnvelopeGenerator.Domain.Constants;
|
||||
using EnvelopeGenerator.Application.Histories.Commands;
|
||||
using EnvelopeGenerator.Domain.Constants;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
@ -11,13 +13,17 @@ public class SendEmailBehavior : IPipelineBehavior<BurnPdfCommand, byte[]>
|
||||
{
|
||||
private readonly ILogger<SendEmailBehavior> _logger;
|
||||
|
||||
private readonly ISender _sender;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="logger"></param>
|
||||
public SendEmailBehavior(ILogger<SendEmailBehavior> logger)
|
||||
/// <param name="sender"></param>
|
||||
public SendEmailBehavior(ILogger<SendEmailBehavior> logger, ISender sender)
|
||||
{
|
||||
_logger = logger;
|
||||
_sender = sender;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -37,25 +43,64 @@ public class SendEmailBehavior : IPipelineBehavior<BurnPdfCommand, byte[]>
|
||||
if (mailToCreator is not null && mailToCreator != (int)FinalEmailType.No)
|
||||
{
|
||||
_logger.LogDebug("Sending email to creator ...");
|
||||
SendFinalEmailToCreator(request.Envelope); // , pAttachment
|
||||
await SendFinalEmailToCreatorAsync(request.Envelope, cancel); // , pAttachment
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("No SendFinalEmailToCreator - mailToCreator [{mailToCreator}] <> [{FinalEmailType.No}] ",
|
||||
_logger.LogWarning("No SendFinalEmailToCreatorAsync - mailToCreator [{mailToCreator}] <> [{FinalEmailType.No}] ",
|
||||
mailToCreator, FinalEmailType.No);
|
||||
}
|
||||
|
||||
if (mailToReceivers != (int)FinalEmailType.No)
|
||||
{
|
||||
_logger.LogDebug("Sending emails to receivers...");
|
||||
SendFinalEmailToReceivers(request.Envelope); // , pAttachment
|
||||
await SendFinalEmailToReceiversAsync(request.Envelope, cancel); // , pAttachment
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("No SendFinalEmailToReceivers - mailToReceivers [{mailToReceivers}] <> [{FinalEmailType.No}] ",
|
||||
_logger.LogWarning("No SendFinalEmailToReceiversAsync - mailToReceivers [{mailToReceivers}] <> [{FinalEmailType.No}] ",
|
||||
mailToReceivers, FinalEmailType.No);
|
||||
}
|
||||
|
||||
return docResult;
|
||||
}
|
||||
|
||||
private async Task SendFinalEmailToCreatorAsync(Envelope envelope, CancellationToken cancel) //, string pAttachment
|
||||
{
|
||||
bool oIncludeAttachment = SendFinalEmailWithAttachment(envelope.FinalEmailToCreator);
|
||||
// string oAttachment = string.Empty;
|
||||
|
||||
_logger.LogDebug("Attachment included: [{oIncludeAttachment}]", oIncludeAttachment);
|
||||
if (oIncludeAttachment)
|
||||
{
|
||||
// oAttachment = pAttachment;
|
||||
}
|
||||
|
||||
await _sender.Send(new CreateHistoryCommand()
|
||||
{
|
||||
EnvelopeId = envelope.Id,
|
||||
Status = EnvelopeStatus.MessageCompletionSent,
|
||||
UserReference = envelope.User.Email,
|
||||
}, cancel);
|
||||
}
|
||||
|
||||
private async Task SendFinalEmailToReceiversAsync(Envelope envelope, CancellationToken cancel) //, string pAttachment
|
||||
{
|
||||
bool oIncludeAttachment = SendFinalEmailWithAttachment(envelope.FinalEmailToReceivers);
|
||||
// string oAttachment = string.Empty;
|
||||
|
||||
_logger.LogDebug("Attachment included: [{oIncludeAttachment}]", oIncludeAttachment);
|
||||
if (oIncludeAttachment)
|
||||
{
|
||||
// oAttachment = pAttachment;
|
||||
}
|
||||
|
||||
// TODO update CreateHistoryCommand to be able to create all records together
|
||||
await Task.WhenAll(envelope.EnvelopeReceivers!.Select(receiver => _sender.Send(new CreateHistoryCommand()
|
||||
{
|
||||
EnvelopeId = envelope.Id,
|
||||
Status = EnvelopeStatus.MessageCompletionSent,
|
||||
UserReference = receiver.Receiver!.EmailAddress,
|
||||
}, cancel)));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user