using DigitalData.Core.Abstraction.Application.Repository;
using DigitalData.EmailProfilerDispatcher.Abstraction.Entities;
using EnvelopeGenerator.Application.Common.Configurations;
using EnvelopeGenerator.Application.Common.Extensions;
using EnvelopeGenerator.Domain.Constants;
using EnvelopeGenerator.Domain.Entities;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
namespace EnvelopeGenerator.Application.Common.Notifications;
///
///
///
public interface ISendMailNotification : INotification
{
///
///
///
public EmailTemplateType TemplateType { get; }
///
///
///
public string EmailAddress { get; }
}
///
///
///
public abstract class SendMailHandler : INotificationHandler
where TNotification : ISendMailNotification
{
///
///
///
protected readonly IRepository TempRepo;
///
///
///
protected readonly IRepository EmailOutRepo;
///
///
///
protected abstract Dictionary CreatePlaceHolders(TNotification notification);
///
///{ "[MESSAGE]", notification.Message },
///{ "[DOCUMENT_ACCESS_CODE]", notification.ReceiverAccessCode },
///{ "[REASON]", pReason }
///{ "[NAME_SENDER]", notification.Envelope.User?.FullName},
///{ "[NAME_PORTAL]", DispatcherParams. },
///{ "[SIGNATURE_TYPE]", "signieren" },
///{ "[LINK_TO_DOCUMENT]", notification.SignatureLink },
///{ "[LINK_TO_DOCUMENT_TEXT]", $"{notification.SignatureLink.Truncate(40)}.." },
///
protected readonly MailParams MailParams;
///
///
///
protected readonly DispatcherParams DispatcherParams;
///
///
///
///
///
protected abstract void ConfigureEmailOut(TNotification notification, EmailOut emailOut);
///
///
///
///
///
///
///
protected SendMailHandler(IRepository tempRepo, IRepository emailOutRepo, IOptions mailParamsOptions, IOptions dispatcherParamsOptions)
{
TempRepo = tempRepo;
EmailOutRepo = emailOutRepo;
MailParams = mailParamsOptions.Value;
DispatcherParams = dispatcherParamsOptions.Value;
}
///
///
///
///
///
///
///
public virtual async Task Handle(TNotification notification, CancellationToken cancel)
{
var placeHolders = CreatePlaceHolders(notification);
var temp = await TempRepo
.Where(x => x.Name == notification.TemplateType.ToString())
.SingleOrDefaultAsync(cancel)
?? throw new InvalidOperationException($"Receiver information is missing in the notification." +
$"{typeof(TNotification)}:\n {notification.ToJson(Format.Json.ForDiagnostics)}");
temp.Subject = ReplacePlaceHolders(temp.Subject, placeHolders, MailParams.Placeholders);
temp.Body = ReplacePlaceHolders(temp.Body, placeHolders, MailParams.Placeholders);
var emailOut = new EmailOut
{
EmailAddress = notification.EmailAddress,
EmailBody = temp.Body,
EmailSubj = temp.Subject,
AddedWhen = DateTime.UtcNow,
AddedWho = DispatcherParams.AddedWho,
SendingProfile = DispatcherParams.SendingProfile,
ReminderTypeId = DispatcherParams.ReminderTypeId,
EmailAttmt1 = DispatcherParams.EmailAttmt1,
WfId = (int)EnvelopeStatus.MessageConfirmationSent,
};
ConfigureEmailOut(notification, emailOut);
await EmailOutRepo.CreateAsync(emailOut, cancel);
}
private static string ReplacePlaceHolders(string text, params Dictionary[] placeHoldersList)
{
foreach (var placeHolders in placeHoldersList)
foreach (var ph in placeHolders)
text = text.Replace(ph.Key, ph.Value);
return text;
}
}