diff --git a/EnvelopeGenerator.Application/Documents/Queries/ReadDocumentQuery.cs b/EnvelopeGenerator.Application/Documents/Queries/ReadDocumentQuery.cs index 4dac7e74..e2e809d6 100644 --- a/EnvelopeGenerator.Application/Documents/Queries/ReadDocumentQuery.cs +++ b/EnvelopeGenerator.Application/Documents/Queries/ReadDocumentQuery.cs @@ -22,7 +22,7 @@ public record ReadDocumentQuery(int? Id = null, int? EnvelopeId = null) : IReque public class ReadDocumentQueryHandler : IRequestHandler { /// - /// Repository for accessing entities. + /// TempRepo for accessing entities. /// private readonly IRepository _repo; diff --git a/EnvelopeGenerator.Application/Notifications/SendMailHandler.cs b/EnvelopeGenerator.Application/Notifications/SendMailHandler.cs new file mode 100644 index 00000000..03237f3c --- /dev/null +++ b/EnvelopeGenerator.Application/Notifications/SendMailHandler.cs @@ -0,0 +1,77 @@ +using DigitalData.Core.Abstraction.Application.Repository; +using EnvelopeGenerator.Domain.Constants; +using EnvelopeGenerator.Domain.Entities; +using MediatR; +using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; + +namespace EnvelopeGenerator.Application.Notifications; + +/// +/// +/// +public interface ISendMailNotification : INotification +{ + /// + /// + /// + public EmailTemplateType TemplateType { get; } +} + +/// +/// +/// +public abstract class SendMailHandler : INotificationHandler + where TNotification : ISendMailNotification +{ + /// + /// + /// + protected readonly IRepository TempRepo; + + /// + /// + /// + protected virtual Dictionary BodyPlaceHolders { get; } = new(); + + /// + /// + /// + protected virtual Dictionary SubjectPlaceHolders { get; } = new(); + + /// + /// + /// + /// + protected SendMailHandler(IRepository tempRepo) + { + TempRepo = tempRepo; + } + + /// + /// + /// + /// + /// + /// + /// + 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); + + temp.Body = ReplacePlaceHolders(temp.Body, BodyPlaceHolders); + } + + private static string ReplacePlaceHolders(string text, Dictionary placeHolders) + { + foreach (var ph in placeHolders) + text = text.Replace(ph.Key, ph.Value); + return text; + } +} diff --git a/EnvelopeGenerator.Infrastructure/Repositories/EmailTemplateRepository.cs b/EnvelopeGenerator.Infrastructure/Repositories/EmailTemplateRepository.cs index 7c966f3f..714d48a5 100644 --- a/EnvelopeGenerator.Infrastructure/Repositories/EmailTemplateRepository.cs +++ b/EnvelopeGenerator.Infrastructure/Repositories/EmailTemplateRepository.cs @@ -7,7 +7,7 @@ using EnvelopeGenerator.Domain.Constants; namespace EnvelopeGenerator.Infrastructure.Repositories; -[Obsolete("Use Repository")] +[Obsolete("Use TempRepo")] public class EmailTemplateRepository : CRUDRepository, IEmailTemplateRepository { private readonly IMemoryCache _cache; diff --git a/EnvelopeGenerator.Infrastructure/Repositories/EnvelopeHistoryRepository.cs b/EnvelopeGenerator.Infrastructure/Repositories/EnvelopeHistoryRepository.cs index 6b8d8751..c5dda94e 100644 --- a/EnvelopeGenerator.Infrastructure/Repositories/EnvelopeHistoryRepository.cs +++ b/EnvelopeGenerator.Infrastructure/Repositories/EnvelopeHistoryRepository.cs @@ -6,7 +6,7 @@ using EnvelopeGenerator.Domain.Constants; namespace EnvelopeGenerator.Infrastructure.Repositories; -[Obsolete("Use Repository")] +[Obsolete("Use TempRepo")] public class EnvelopeHistoryRepository : CRUDRepository, IEnvelopeHistoryRepository { public EnvelopeHistoryRepository(EGDbContext dbContext) : base(dbContext, dbContext.EnvelopeHistories) diff --git a/EnvelopeGenerator.Infrastructure/Repositories/EnvlopeReceiverRepository.cs b/EnvelopeGenerator.Infrastructure/Repositories/EnvlopeReceiverRepository.cs index 14870415..65d983a1 100644 --- a/EnvelopeGenerator.Infrastructure/Repositories/EnvlopeReceiverRepository.cs +++ b/EnvelopeGenerator.Infrastructure/Repositories/EnvlopeReceiverRepository.cs @@ -8,7 +8,7 @@ using EnvelopeGenerator.Domain.Constants; namespace EnvelopeGenerator.Infrastructure.Repositories; -[Obsolete("Use Repository")] +[Obsolete("Use TempRepo")] public class EnvelopeReceiverRepository : CRUDRepository, IEnvelopeReceiverRepository { public EnvelopeReceiverRepository(EGDbContext dbContext) : base(dbContext, dbContext.EnvelopeReceivers)