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:
@@ -0,0 +1,73 @@
|
||||
using EnvelopeGenerator.Domain.Constants;
|
||||
|
||||
namespace EnvelopeGenerator.Application.Common.Interfaces.Services.Notification;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public record DocumentReceivedNotification : EnvelopeNotificationBase
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public required string DocumentTitle { get; init; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public required string NameReceiver { get; init; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public required string NameSender { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// [SIGNATURE_TYPE]
|
||||
/// </summary>
|
||||
public required bool IsReadAndConfirm { private get; init; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string SignatureType => IsReadAndConfirm ? "Signieren" : "Lesen und bestätigen";
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public required bool AdvancedSignature { private get; init; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string DocumentProcess => AdvancedSignature ? " und elektronisch unterschreiben" : string.Empty;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public required string LinkToDocument { get; init; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string LinkToDocumentText => LinkToDocument[..Math.Min(LinkToDocument.Length, 40)];
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public required string Message { get; init; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string NamePortal { get; set; } = "signFLOW Portal";
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override EmailTemplateType GetTemplateType()
|
||||
{
|
||||
return EmailTemplateType.DocumentReceived;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
namespace EnvelopeGenerator.Application.Common.Interfaces.Services.Notification;
|
||||
using EnvelopeGenerator.Domain.Constants;
|
||||
|
||||
namespace EnvelopeGenerator.Application.Common.Interfaces.Services.Notification;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public record DocumentSignedNotification
|
||||
public record DocumentSignedNotification : EnvelopeNotificationBase
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
@@ -19,11 +21,11 @@ public record DocumentSignedNotification
|
||||
///
|
||||
/// </summary>
|
||||
public string NamePortal { get; init; } = "signFLOW Portal";
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public required byte[] Attachment { get; init; }
|
||||
public override EmailTemplateType GetTemplateType() => EmailTemplateType.DocumentSigned;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
using EnvelopeGenerator.Domain.Constants;
|
||||
|
||||
namespace EnvelopeGenerator.Application.Common.Interfaces.Services.Notification;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public abstract record EnvelopeNotificationBase
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public abstract EmailTemplateType GetTemplateType();
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public List<(string FileName, byte[] Content)>? Attachments { get; init; }
|
||||
}
|
||||
@@ -8,9 +8,11 @@ public interface IEnvelopeNotificationService
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="TEnvelopeNotificationBase"></typeparam>
|
||||
/// <param name="email"></param>
|
||||
/// <param name="documentSigned"></param>
|
||||
/// <param name="notification"></param>
|
||||
/// <param name="cancel"></param>
|
||||
/// <returns></returns>
|
||||
public Task Notify(string email, DocumentSignedNotification documentSigned, CancellationToken cancel = default);
|
||||
public Task Notify<TEnvelopeNotificationBase>(string email, TEnvelopeNotificationBase notification, CancellationToken cancel = default)
|
||||
where TEnvelopeNotificationBase : EnvelopeNotificationBase;
|
||||
}
|
||||
|
||||
@@ -98,7 +98,9 @@ public class FinalizeEnvelopeCommandHandler(IRepository<Envelope> Repo, ISender
|
||||
{
|
||||
DocumentTitle = envelope.Title,
|
||||
NameReceiver = er.Name,
|
||||
Attachment = doc
|
||||
Attachments = [
|
||||
($"unterzeichnetes-dokument.pdf", doc)
|
||||
]
|
||||
}, cancel);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using DigitalData.MessagingService.Application.Common.Dto;
|
||||
|
||||
namespace EnvelopeGenerator.Infrastructure.Notification;
|
||||
|
||||
public static class EnvelopeNotificationExtensions
|
||||
{
|
||||
public static EmailAttachmentDto ToEmailAttachmentDto(this (string FileName, byte[] Content) attachment) => new()
|
||||
{
|
||||
FileName = attachment.FileName,
|
||||
Content = attachment.Content
|
||||
};
|
||||
|
||||
public static IEnumerable<EmailAttachmentDto> ToEmailAttachmentDto(this IEnumerable<(string FileName, byte[] Content)> attachments)
|
||||
=> attachments.Select(ToEmailAttachmentDto);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace EnvelopeGenerator.Infrastructure.Notification;
|
||||
|
||||
public static class ReplacePlaceholderExtensions
|
||||
{
|
||||
private sealed record PlaceholderProperty(string Placeholder, PropertyInfo Property);
|
||||
|
||||
private static readonly ConcurrentDictionary<Type, PlaceholderProperty[]> PropertyCache = new();
|
||||
|
||||
public static string ReplacePlaceholders(this string template, object data)
|
||||
{
|
||||
var properties = PropertyCache.GetOrAdd(data.GetType(), CreatePlaceholderProperties);
|
||||
|
||||
foreach (var item in properties)
|
||||
{
|
||||
var value = item.Property.GetValue(data)?.ToString() ?? string.Empty;
|
||||
template = template.Replace(
|
||||
item.Placeholder,
|
||||
value,
|
||||
StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
private static PlaceholderProperty[] CreatePlaceholderProperties(
|
||||
Type type)
|
||||
{
|
||||
return [.. type
|
||||
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(p => p.CanRead && p.GetIndexParameters().Length == 0)
|
||||
.Select(p => new PlaceholderProperty(
|
||||
$"[{ToCapitalSnakeCase(p.Name)}]",
|
||||
p))];
|
||||
}
|
||||
|
||||
private static string ToCapitalSnakeCase(string name)
|
||||
{
|
||||
return Regex.Replace(
|
||||
Regex.Replace(name, @"([A-Z]+)([A-Z][a-z])", "$1_$2"),
|
||||
@"([a-z0-9])([A-Z])",
|
||||
"$1_$2").ToUpperInvariant();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user