Add notification infrastructure project

This commit is contained in:
2026-10-22 01:35:18 +02:00
parent f38098aed8
commit 658cb28e1c
3 changed files with 106 additions and 0 deletions

View File

@@ -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<RabbitMqConfiguration>()
?? 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<EmailAccount>(configuration.GetSection("NotificationEmailAccount"));
services.AddScoped<IEnvelopeNotificationService, EnvelopeNotificationService>();
return services;
}
}

View File

@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0;</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DigitalData.MessagingService.Client" Version="1.0.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.10" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EnvelopeGenerator.Application\EnvelopeGenerator.Application.csproj" />
</ItemGroup>
</Project>

View File

@@ -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<EmailAccount> EmailAccountOptions, IRepository<EmailTemplate> 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);
}
}