- Implements IPipelineBehavior<BurnPdfCommand, byte[]> to send emails to creator and receivers. - Logs debug information when sending emails and warnings if email sending is skipped. - Uses Envelope's FinalEmailType to determine whether to send emails.
61 lines
1.9 KiB
C#
61 lines
1.9 KiB
C#
using EnvelopeGenerator.Domain.Constants;
|
|
using MediatR;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace EnvelopeGenerator.Application.Pdf.Behaviors;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class SendEmailBehavior : IPipelineBehavior<BurnPdfCommand, byte[]>
|
|
{
|
|
private readonly ILogger<SendEmailBehavior> _logger;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="logger"></param>
|
|
public SendEmailBehavior(ILogger<SendEmailBehavior> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="next"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public async Task<byte[]> Handle(BurnPdfCommand request, RequestHandlerDelegate<byte[]> next, CancellationToken cancel)
|
|
{
|
|
var docResult = await next(cancel);
|
|
|
|
var mailToCreator = request.Envelope!.FinalEmailToCreator;
|
|
var mailToReceivers = request.Envelope.FinalEmailToReceivers;
|
|
|
|
if (mailToCreator is not null && mailToCreator != (int)FinalEmailType.No)
|
|
{
|
|
_logger.LogDebug("Sending email to creator ...");
|
|
SendFinalEmailToCreator(request.Envelope); // , pAttachment
|
|
}
|
|
else
|
|
{
|
|
_logger.LogWarning("No SendFinalEmailToCreator - mailToCreator [{mailToCreator}] <> [{FinalEmailType.No}] ",
|
|
mailToCreator, FinalEmailType.No);
|
|
}
|
|
|
|
if (mailToReceivers != (int)FinalEmailType.No)
|
|
{
|
|
_logger.LogDebug("Sending emails to receivers...");
|
|
SendFinalEmailToReceivers(request.Envelope); // , pAttachment
|
|
}
|
|
else
|
|
{
|
|
_logger.LogWarning("No SendFinalEmailToReceivers - mailToReceivers [{mailToReceivers}] <> [{FinalEmailType.No}] ",
|
|
mailToReceivers, FinalEmailType.No);
|
|
}
|
|
|
|
return docResult;
|
|
}
|
|
} |