init SendMailHandler to read tempalte and replace placeholders

This commit is contained in:
tekh 2025-09-03 17:46:24 +02:00
parent 954d665ac3
commit 5ccd1fee26
5 changed files with 81 additions and 4 deletions

View File

@ -22,7 +22,7 @@ public record ReadDocumentQuery(int? Id = null, int? EnvelopeId = null) : IReque
public class ReadDocumentQueryHandler : IRequestHandler<ReadDocumentQuery, EnvelopeDocumentDto?>
{
/// <summary>
/// Repository for accessing <see cref="EnvelopeDocument"/> entities.
/// TempRepo for accessing <see cref="EnvelopeDocument"/> entities.
/// </summary>
private readonly IRepository<EnvelopeDocument> _repo;

View File

@ -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;
/// <summary>
///
/// </summary>
public interface ISendMailNotification : INotification
{
/// <summary>
///
/// </summary>
public EmailTemplateType TemplateType { get; }
}
/// <summary>
///
/// </summary>
public abstract class SendMailHandler<TNotification> : INotificationHandler<TNotification>
where TNotification : ISendMailNotification
{
/// <summary>
///
/// </summary>
protected readonly IRepository<EmailTemplate> TempRepo;
/// <summary>
///
/// </summary>
protected virtual Dictionary<string, string> BodyPlaceHolders { get; } = new();
/// <summary>
///
/// </summary>
protected virtual Dictionary<string, string> SubjectPlaceHolders { get; } = new();
/// <summary>
///
/// </summary>
/// <param name="tempRepo"></param>
protected SendMailHandler(IRepository<EmailTemplate> tempRepo)
{
TempRepo = tempRepo;
}
/// <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);
temp.Body = ReplacePlaceHolders(temp.Body, BodyPlaceHolders);
}
private static string ReplacePlaceHolders(string text, Dictionary<string, string> placeHolders)
{
foreach (var ph in placeHolders)
text = text.Replace(ph.Key, ph.Value);
return text;
}
}

View File

@ -7,7 +7,7 @@ using EnvelopeGenerator.Domain.Constants;
namespace EnvelopeGenerator.Infrastructure.Repositories;
[Obsolete("Use Repository")]
[Obsolete("Use TempRepo")]
public class EmailTemplateRepository : CRUDRepository<EmailTemplate, int, EGDbContext>, IEmailTemplateRepository
{
private readonly IMemoryCache _cache;

View File

@ -6,7 +6,7 @@ using EnvelopeGenerator.Domain.Constants;
namespace EnvelopeGenerator.Infrastructure.Repositories;
[Obsolete("Use Repository")]
[Obsolete("Use TempRepo")]
public class EnvelopeHistoryRepository : CRUDRepository<EnvelopeHistory, long, EGDbContext>, IEnvelopeHistoryRepository
{
public EnvelopeHistoryRepository(EGDbContext dbContext) : base(dbContext, dbContext.EnvelopeHistories)

View File

@ -8,7 +8,7 @@ using EnvelopeGenerator.Domain.Constants;
namespace EnvelopeGenerator.Infrastructure.Repositories;
[Obsolete("Use Repository")]
[Obsolete("Use TempRepo")]
public class EnvelopeReceiverRepository : CRUDRepository<EnvelopeReceiver, (int Envelope, int Receiver), EGDbContext>, IEnvelopeReceiverRepository
{
public EnvelopeReceiverRepository(EGDbContext dbContext) : base(dbContext, dbContext.EnvelopeReceivers)