diff --git a/EnvelopeGenerator.Application/Common/Interfaces/Services/Notification/IEnvelopeNotificationService.cs b/EnvelopeGenerator.Application/Common/Interfaces/Services/Notification/IEnvelopeNotificationService.cs index 7ad10284..442190c3 100644 --- a/EnvelopeGenerator.Application/Common/Interfaces/Services/Notification/IEnvelopeNotificationService.cs +++ b/EnvelopeGenerator.Application/Common/Interfaces/Services/Notification/IEnvelopeNotificationService.cs @@ -9,10 +9,10 @@ public interface IEnvelopeNotificationService /// /// /// - /// + /// /// /// /// - public Task Notify(string email, TEnvelopeNotificationBase notification, CancellationToken cancel = default) + public Task Notify(string receiverEmail, TEnvelopeNotificationBase notification, CancellationToken cancel = default) where TEnvelopeNotificationBase : EnvelopeNotificationBase; } diff --git a/EnvelopeGenerator.Infrastructure.Notification/EnvelopeNotificationService.cs b/EnvelopeGenerator.Infrastructure.Notification/EnvelopeNotificationService.cs index 5bf02122..595caea0 100644 --- a/EnvelopeGenerator.Infrastructure.Notification/EnvelopeNotificationService.cs +++ b/EnvelopeGenerator.Infrastructure.Notification/EnvelopeNotificationService.cs @@ -1,35 +1,48 @@ using DigitalData.Core.Abstraction.Application.Repository; using DigitalData.MessagingService.Application.Common.Dto; +using DigitalData.MessagingService.Application.Common.Interfaces; +using DigitalData.MessagingService.Application.EmailSending.Commands; 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 EnvelopeGenerator.Infrastructure.Notification.Configuration; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; namespace EnvelopeGenerator.Infrastructure.Notification; -public class EnvelopeNotificationService(IOptions EmailAccountOptions, IRepository TempRepo) : IEnvelopeNotificationService +public class EnvelopeNotificationService(IRepository TempRepo, IMessagingServiceClient MsgClient, IOptions ntfOpt) : IEnvelopeNotificationService { + private readonly NotificationOptions NtfOpt = ntfOpt.Value; + 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) + public async Task Notify(string receiverEmail, TEnvelopeNotificationBase notification, CancellationToken cancel = default) where TEnvelopeNotificationBase : EnvelopeNotificationBase { var template = await GetTemplateAsync(notification.GetTemplateType(), cancel); - await EmailSender.Send(new EmailContext() + + // create email command + var mail = new PublishEmailViaSmtpCommand() { Body = template.Body.ReplacePlaceholders(notification), Subject = template.Subject.ReplacePlaceholders(notification), - Recipients = [email], - Sender = EmailAccountOptions.Value, - Attachments = notification.Attachments?.ToEmailAttachmentDto() ?? [] - }); + Recipients = [receiverEmail], + Sender = NtfOpt.SenderEmail.ToSender(), + IsHtml = true + }; + + // add attachments if any + if(notification.Attachments?.Any() ?? false) + mail = mail.WithAttachments(notification.Attachments.ToEmailAttachmentDto()); + + await MsgClient.SendAsync(mail, cancel); } } \ No newline at end of file