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); } }