using DigitalData.Core.Abstraction.Application.Repository; 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 EnvelopeGenerator.Domain.Exceptions; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; namespace EnvelopeGenerator.Infrastructure.Notification; public class EnvelopeNotificationService(IOptions EmailAccountOptions, IRepository TempRepo) : IEnvelopeNotificationService { private async Task GetTemplateAsync(EmailTemplateType templateType, CancellationToken cancel = default) { return await TempRepo.Where(t => t.Name == templateType.ToString()).FirstOrDefaultAsync(cancel) ?? throw new DataIntegrityException($"{templateType} email template not found"); } public async Task Notify(string email, TEnvelopeNotificationBase notification, CancellationToken cancel = default) where TEnvelopeNotificationBase : EnvelopeNotificationBase { var template = await GetTemplateAsync(notification.GetTemplateType(), cancel); await EmailSender.Send(new EmailContext() { Body = template.Body.ReplacePlaceholders(notification), Subject = template.Subject.ReplacePlaceholders(notification), Recipients = [email], Sender = EmailAccountOptions.Value, Attachments = notification.Attachments?.ToEmailAttachmentDto() ?? [] }); } }