Refactor notification system

Introduced `EnvelopeNotificationBase` as a new base class for notifications, providing a unified structure with an abstract `GetTemplateType` method and optional attachments. Added `DocumentReceivedNotification` and updated `DocumentSignedNotification` to inherit from this base class, streamlining attachment handling and template type determination.

Refactored `IEnvelopeNotificationService` to use a generic `Notify` method, allowing for flexibility with different notification types. Updated `FinalizeEnvelopeCommandHandler` to align with the new attachment handling.

Added `EnvelopeNotificationExtensions` for converting attachments to `EmailAttachmentDto` objects and introduced `ReplacePlaceholderExtensions` for dynamic placeholder replacement in email templates using reflection. Removed outdated placeholder logic from `EnvelopeNotificationService`.
This commit is contained in:
2026-10-28 16:25:19 +01:00
parent 8b7f99eb76
commit 8aabc3e233
8 changed files with 181 additions and 30 deletions

View File

@@ -1,11 +1,11 @@
using DigitalData.Core.Abstraction.Application.Repository;
using DigitalData.Core.Exceptions;
using DigitalData.MessagingService.Application.Common.Dto;
using DigitalData.MessagingService.Client;
using DigitalData.MessagingService.Domain.Entities;
using EnvelopeGenerator.Application.Common.Interfaces.Services.Notification;
using EnvelopeGenerator.Domain.Constants;
using EnvelopeGenerator.Domain.Entities;
using MediatR;
using EnvelopeGenerator.Domain.Exceptions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
@@ -13,32 +13,23 @@ namespace EnvelopeGenerator.Infrastructure.Notification;
public class EnvelopeNotificationService(IOptions<EmailAccount> EmailAccountOptions, IRepository<EmailTemplate> TempRepo) : IEnvelopeNotificationService
{
public async Task Notify(string email, DocumentSignedNotification documentSigned, CancellationToken cancel = default)
private async Task<EmailTemplate> GetTemplateAsync(EmailTemplateType templateType, CancellationToken cancel = default)
{
var template = await TempRepo.Where(t => t.Name == "DocumentCompleted").FirstOrDefaultAsync(cancel)
?? throw new NotFoundException("DocumentCompleted email template not found");
var subjectTemplate = Replace(template.Subject, documentSigned);
var bodyTemplate = Replace(template.Body, documentSigned);
return await TempRepo.Where(t => t.Name == templateType.ToString()).FirstOrDefaultAsync(cancel)
?? throw new DataIntegrityException($"{templateType} email template not found");
}
public async Task Notify<TEnvelopeNotificationBase>(string email, TEnvelopeNotificationBase notification, CancellationToken cancel = default)
where TEnvelopeNotificationBase : EnvelopeNotificationBase
{
var template = await GetTemplateAsync(notification.GetTemplateType(), cancel);
await EmailSender.Send(new EmailContext()
{
Body = bodyTemplate,
Subject = subjectTemplate,
Body = template.Body.ReplacePlaceholders(notification),
Subject = template.Subject.ReplacePlaceholders(notification),
Recipients = [email],
Sender = EmailAccountOptions.Value,
Attachments = [new EmailAttachmentDto()
{
FileName = "unterzeichnetes-dokument.pdf",
Content = documentSigned.Attachment
}]
Attachments = notification.Attachments?.ToEmailAttachmentDto() ?? []
});
}
private static string Replace(string template, DocumentSignedNotification documentSigned)
{
return template
.Replace("[DOCUMENT_TITLE]", documentSigned.DocumentTitle)
.Replace("[NAME_PORTAL]", documentSigned.NamePortal)
.Replace("[NAME_RECEIVER]", documentSigned.NameReceiver);
}
}
}