From 67faa441b8e3780d522447516fa5a0437f5af06c Mon Sep 17 00:00:00 2001 From: TekH Date: Thu, 26 Nov 2026 16:42:28 +0100 Subject: [PATCH] Refactor SendSignedMailBehavior for simplicity Refactored the `SendSignedMailBehavior` class to streamline its implementation by removing unnecessary dependencies and simplifying the notification logic. Replaced the constructor with a primary constructor using `IEnvelopeNotificationService` as the sole dependency. Updated the `Handle` method to use `Notify` from `IEnvelopeNotificationService` for sending `DocumentSignedNotification`, eliminating the need for placeholder replacement and repository interactions. Removed unused methods and namespaces, and updated class documentation. --- .../Behaviors/SendSignedMailBehavior.cs | 109 ++---------------- 1 file changed, 12 insertions(+), 97 deletions(-) diff --git a/EnvelopeGenerator.Application/DocReceiverElements/Behaviors/SendSignedMailBehavior.cs b/EnvelopeGenerator.Application/DocReceiverElements/Behaviors/SendSignedMailBehavior.cs index 43fe1cd7..6e23d1a8 100644 --- a/EnvelopeGenerator.Application/DocReceiverElements/Behaviors/SendSignedMailBehavior.cs +++ b/EnvelopeGenerator.Application/DocReceiverElements/Behaviors/SendSignedMailBehavior.cs @@ -1,15 +1,7 @@ -using DigitalData.Core.Abstraction.Application.Repository; -using DigitalData.EmailProfilerDispatcher.Abstraction.Entities; -using EnvelopeGenerator.Application.Common.Configurations; -using EnvelopeGenerator.Application.Common.Extensions; +using EnvelopeGenerator.Application.Common.Interfaces.Services.Notification; using EnvelopeGenerator.Application.DocReceiverElements.Commands; -using EnvelopeGenerator.Domain.Constants; -using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Domain.Interfaces; using MediatR; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Options; -using Newtonsoft.Json; namespace EnvelopeGenerator.Application.DocReceiverElements.Behaviors; @@ -17,32 +9,12 @@ namespace EnvelopeGenerator.Application.DocReceiverElements.Behaviors; /// Pipeline behavior that sends signed mail notification. /// Executes LAST in the signing process - only if all previous behaviors succeed. /// -public class SendSignedMailBehavior : IPipelineBehavior +/// +/// +/// +/// +public class SendSignedMailBehavior(IEnvelopeNotificationService NtfService) : IPipelineBehavior { - private readonly IRepository _tempRepo; - private readonly IRepository _emailOutRepo; - private readonly MailParams _mailParams; - private readonly DispatcherParams _dispatcherParams; - - /// - /// - /// - /// - /// - /// - /// - public SendSignedMailBehavior( - IRepository tempRepo, - IRepository emailOutRepo, - IOptions mailParamsOptions, - IOptions dispatcherParamsOptions) - { - _tempRepo = tempRepo; - _emailOutRepo = emailOutRepo; - _mailParams = mailParamsOptions.Value; - _dispatcherParams = dispatcherParamsOptions.Value; - } - /// /// /// @@ -52,71 +24,14 @@ public class SendSignedMailBehavior : IPipelineBehavior /// public async Task Handle(SigningCommand request, RequestHandlerDelegate next, CancellationToken cancellationToken) { - var placeHolders = CreatePlaceHolders(request); - - var temp = await _tempRepo - .Where(x => x.Name == EmailTemplateType.DocumentSigned.ToString()) - .SingleOrDefaultAsync(cancellationToken) - ?? throw new InvalidOperationException($"Email template not found. SignCommand:\n {request.ToJson(Format.Json.ForDiagnostics)}"); - - temp.Subject = ReplacePlaceHolders(temp.Subject, placeHolders, _mailParams.Placeholders); - temp.Body = ReplacePlaceHolders(temp.Body, placeHolders, _mailParams.Placeholders); - - var emailOut = new EmailOut + var email = request.EnvelopeReceiver.Receiver!.EmailAddress; + await NtfService.Notify(email, new DocumentSignedNotification() { - EmailAddress = request.EnvelopeReceiver.Receiver!.EmailAddress, - EmailBody = temp.Body, - EmailSubj = temp.Subject, - AddedWhen = DateTime.Now, - AddedWho = _dispatcherParams.AddedWho, - SendingProfile = _dispatcherParams.SendingProfile, - ReminderTypeId = _dispatcherParams.ReminderTypeId, - EmailAttmt1 = _dispatcherParams.EmailAttmt1, - WfId = (int)EnvelopeStatus.MessageConfirmationSent, - ReferenceString = request.EnvelopeReceiver.Receiver!.EmailAddress, - ReferenceId = request.EnvelopeReceiver.ReceiverId - }; - - await _emailOutRepo.CreateAsync(emailOut, cancellationToken); + DocumentTitle = request.EnvelopeReceiver.Envelope?.Title ?? string.Empty, + IsReadAndConfirm = request.EnvelopeReceiver.Envelope.IsReadAndConfirm(), + NameReceiver = request.EnvelopeReceiver.Name ?? string.Empty, + }, cancellationToken); return await next(cancellationToken); } - - private Dictionary CreatePlaceHolders(SigningCommand request) - { - var placeHolders = new Dictionary() - { - { "[NAME_RECEIVER]", request.EnvelopeReceiver.Name ?? string.Empty }, - { "[DOCUMENT_TITLE]", request.EnvelopeReceiver.Envelope?.Title ?? string.Empty }, - }; - - if (request.EnvelopeReceiver.Envelope.IsReadAndConfirm()) - { - placeHolders["[SIGNATURE_TYPE]"] = "Lesen und bestätigen"; - placeHolders["[DOCUMENT_PROCESS]"] = string.Empty; - placeHolders["[FINAL_STATUS]"] = "Lesebestätigung"; - placeHolders["[FINAL_ACTION]"] = "Empfänger bestätigt"; - placeHolders["[REJECTED_BY_OTHERS]"] = "anderen Empfänger abgelehnt!"; - placeHolders["[RECEIVER_ACTION]"] = "bestätigt"; - } - else - { - placeHolders["[SIGNATURE_TYPE]"] = "Signieren"; - placeHolders["[DOCUMENT_PROCESS]"] = " und elektronisch unterschreiben"; - placeHolders["[FINAL_STATUS]"] = "Signatur"; - placeHolders["[FINAL_ACTION]"] = "Vertragspartner unterzeichnet"; - placeHolders["[REJECTED_BY_OTHERS]"] = "anderen Vertragspartner abgelehnt! Ihre notwendige Unterzeichnung wurde verworfen."; - placeHolders["[RECEIVER_ACTION]"] = "unterschrieben"; - } - - return placeHolders; - } - - private static string ReplacePlaceHolders(string text, params Dictionary[] placeHoldersList) - { - foreach (var placeHolders in placeHoldersList) - foreach (var ph in placeHolders) - text = text.Replace(ph.Key, ph.Value); - return text; - } }