149 lines
4.7 KiB
C#
149 lines
4.7 KiB
C#
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using DigitalData.EmailProfilerDispatcher.Abstraction.Entities;
|
|
using EnvelopeGenerator.Application.Configurations;
|
|
using EnvelopeGenerator.Domain.Constants;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Options;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace EnvelopeGenerator.Application.Notifications;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public interface ISendMailNotification : INotification
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public EmailTemplateType TemplateType { get; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string EmailAddress { get; }
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public abstract class SendMailHandler<TNotification> : INotificationHandler<TNotification>
|
|
where TNotification : ISendMailNotification
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
protected readonly IRepository<EmailTemplate> TempRepo;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
protected readonly IRepository<EmailOut> EmailOutRepo;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
protected virtual Dictionary<string, string> BodyPlaceHolders { get; } = new();
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
protected readonly MailParams MailParams;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
protected readonly DispatcherParams DispatcherParams;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
protected virtual Dictionary<string, string> SubjectPlaceHolders { get; } = new();
|
|
|
|
/// <summary>
|
|
/// ReferenceString = Envelope.Uuid
|
|
/// </summary>
|
|
/// <param name="emailOut"></param>
|
|
/// <returns></returns>
|
|
protected abstract void ConfigEmailOut(EmailOut emailOut);
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="tempRepo"></param>
|
|
/// <param name="emailOutRepo"></param>
|
|
/// <param name="mailParamsOptions"></param>
|
|
/// <param name="dispatcherParamsOptions"></param>
|
|
protected SendMailHandler(IRepository<EmailTemplate> tempRepo, IRepository<EmailOut> emailOutRepo, IOptions<MailParams> mailParamsOptions, IOptions<DispatcherParams> dispatcherParamsOptions)
|
|
{
|
|
TempRepo = tempRepo;
|
|
EmailOutRepo = emailOutRepo;
|
|
MailParams = mailParamsOptions.Value;
|
|
DispatcherParams = dispatcherParamsOptions.Value;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="notification"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task Handle(TNotification notification, CancellationToken cancel)
|
|
{
|
|
var temp = await TempRepo
|
|
.ReadOnly()
|
|
.SingleOrDefaultAsync(x => x.Name == notification.TemplateType.ToString(), cancel)
|
|
?? throw new InvalidOperationException($"Receiver information is missing in the notification." +
|
|
$"{typeof(TNotification)}:\n {JsonConvert.SerializeObject(notification, Format.Json.ForDiagnostics)}");
|
|
|
|
temp.Subject = ReplacePlaceHolders(temp.Subject, SubjectPlaceHolders, MailParams.Placeholders);
|
|
|
|
temp.Body = ReplacePlaceHolders(temp.Body, BodyPlaceHolders, 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,
|
|
|
|
};
|
|
ConfigEmailOut(emailOut);
|
|
await EmailOutRepo.CreateAsync(emailOut, cancel);
|
|
}
|
|
|
|
private static string ReplacePlaceHolders(string text, params Dictionary<string, string>[] placeHoldersList)
|
|
{
|
|
foreach (var placeHolders in placeHoldersList)
|
|
foreach (var ph in placeHolders)
|
|
text = text.Replace(ph.Key, ph.Value);
|
|
return text;
|
|
}
|
|
|
|
private static string TextToHtml(string input)
|
|
{
|
|
if (string.IsNullOrEmpty(input)) return "";
|
|
|
|
// HTML encoding special characters
|
|
string encoded = System.Net.WebUtility.HtmlEncode(input);
|
|
|
|
// Convert tabs to (4 non-breaking spaces)
|
|
encoded = encoded.Replace("\t", " ");
|
|
|
|
// Convert line breaks to <br />
|
|
encoded = encoded.Replace("\r\n", "<br />"); // Windows
|
|
encoded = encoded.Replace("\r", "<br />"); // Mac old
|
|
encoded = encoded.Replace("\n", "<br />"); // Unix/Linux
|
|
|
|
return encoded;
|
|
}
|
|
}
|