From 658cb28e1cdfc832bff2f679a04dbc36e2688bfd Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 22 Oct 2026 01:35:18 +0200 Subject: [PATCH] Add notification infrastructure project --- .../DependencyInjection.cs | 43 ++++++++++++++++++ ...nerator.Infrastructure.Notification.csproj | 19 ++++++++ .../EnvelopeNotificationService.cs | 44 +++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 EnvelopeGenerator.Infrastructure.Notification/DependencyInjection.cs create mode 100644 EnvelopeGenerator.Infrastructure.Notification/EnvelopeGenerator.Infrastructure.Notification.csproj create mode 100644 EnvelopeGenerator.Infrastructure.Notification/EnvelopeNotificationService.cs diff --git a/EnvelopeGenerator.Infrastructure.Notification/DependencyInjection.cs b/EnvelopeGenerator.Infrastructure.Notification/DependencyInjection.cs new file mode 100644 index 00000000..c84c778e --- /dev/null +++ b/EnvelopeGenerator.Infrastructure.Notification/DependencyInjection.cs @@ -0,0 +1,43 @@ +using DigitalData.MessagingService.Client; +using DigitalData.MessagingService.Domain.Entities; +using DigitalData.MessagingService.RabbitMQ; +using EnvelopeGenerator.Application.Common.Interfaces.Services.Notification; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EnvelopeGenerator.Infrastructure.Notification; + +public static class DependencyInjection +{ + public static IServiceCollection AddNotificationInfrastructure(this IServiceCollection services, IConfiguration configuration) + { + var rMqConfig = configuration.GetSection("RabbitMQ").Get() + ?? throw new ArgumentNullException(nameof(RabbitMqConfiguration), "RabbitMQ configuration is missing"); + + EmailSender.ConnectRabbitMq(config => + { + config.HostName = rMqConfig.HostName; + config.Port = rMqConfig.Port; + config.UserName = rMqConfig.UserName; + config.Password = rMqConfig.Password; + + config.VirtualHost = "/"; + config.QueueName = "emailprofiler.email.outbox"; + config.ExchangeName = "emailprofiler.emails"; + config.RoutingKey = "email.outbox"; + config.DlqQueueName = "emailprofiler.email.outbox.dlq"; + config.DlqExchangeName = "emailprofiler.emails.dlq"; + config.DlqRoutingKey = "email.outbox.dlq"; + }); + + services.Configure(configuration.GetSection("NotificationEmailAccount")); + + services.AddScoped(); + return services; + } +} diff --git a/EnvelopeGenerator.Infrastructure.Notification/EnvelopeGenerator.Infrastructure.Notification.csproj b/EnvelopeGenerator.Infrastructure.Notification/EnvelopeGenerator.Infrastructure.Notification.csproj new file mode 100644 index 00000000..469d3e1b --- /dev/null +++ b/EnvelopeGenerator.Infrastructure.Notification/EnvelopeGenerator.Infrastructure.Notification.csproj @@ -0,0 +1,19 @@ + + + + net8.0; + enable + enable + latest + + + + + + + + + + + + diff --git a/EnvelopeGenerator.Infrastructure.Notification/EnvelopeNotificationService.cs b/EnvelopeGenerator.Infrastructure.Notification/EnvelopeNotificationService.cs new file mode 100644 index 00000000..79e87020 --- /dev/null +++ b/EnvelopeGenerator.Infrastructure.Notification/EnvelopeNotificationService.cs @@ -0,0 +1,44 @@ +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.Entities; +using MediatR; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; + +namespace EnvelopeGenerator.Infrastructure.Notification; + +public class EnvelopeNotificationService(IOptions EmailAccountOptions, IRepository TempRepo) : IEnvelopeNotificationService +{ + public async Task Notify(string email, DocumentSignedNotification documentSigned, 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); + + await EmailSender.Send(new EmailContext() + { + Body = bodyTemplate, + Subject = subjectTemplate, + Recipients = [email], + Sender = EmailAccountOptions.Value, + Attachments = [new EmailAttachmentDto() + { + FileName = "unterzeichnetes-dokument.pdf", + Content = documentSigned.Attachment + }] + }); + } + + 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); + } +}