From cd077aa1bdd8ad48abb7a00399c0c5bd99aad184 Mon Sep 17 00:00:00 2001 From: TekH Date: Tue, 9 Jun 2026 16:34:49 +0200 Subject: [PATCH] Refactor DocSignedNotification for better encapsulation Refactored `DocSignedNotification` to remove inheritance from `EnvelopeReceiverDto` and introduced a required `EnvelopeReceiver` property. Updated all usages across the codebase to align with the new structure, including controllers, handlers, and tests. - Improved encapsulation and reduced coupling by making dependencies explicit. - Updated `AnnotationController`, `DocStatusHandler`, `HistoryHandler`, and `SendSignedMailHandler` to use the `EnvelopeReceiver` property. - Adjusted `DocSignedNotificationTests` to reflect the new instantiation pattern. - Updated XML documentation and ensured consistent access to `EnvelopeReceiver` properties like `EnvelopeId`, `ReceiverId`, and `EmailAddress`. --- .../Controllers/AnnotationController.cs | 6 +++--- .../DocSigned/DocSignedNotification.cs | 18 +++++++++++------- .../DocSigned/Handlers/DocStatusHandler.cs | 4 ++-- .../DocSigned/Handlers/HistoryHandler.cs | 6 +++--- .../Handlers/SendSignedMailHandler.cs | 8 ++++---- .../Application/DocSignedNotificationTests.cs | 2 +- .../Controllers/AnnotationController.cs | 6 +++--- 7 files changed, 27 insertions(+), 23 deletions(-) diff --git a/EnvelopeGenerator.API/Controllers/AnnotationController.cs b/EnvelopeGenerator.API/Controllers/AnnotationController.cs index 45dfd4b9..e5c8b880 100644 --- a/EnvelopeGenerator.API/Controllers/AnnotationController.cs +++ b/EnvelopeGenerator.API/Controllers/AnnotationController.cs @@ -75,7 +75,7 @@ public class AnnotationController : ControllerBase var envelopeReceiverDto = await _mediator.ReadEnvelopeReceiverAsync(uuid, signature, cancel); var docSignedNotification = envelopeReceiverDto is not null - ? new DocSignedNotification(envelopeReceiverDto) { PsPdfKitAnnotation = psPdfKitAnnotation } + ? new DocSignedNotification { EnvelopeReceiver = envelopeReceiverDto, PsPdfKitAnnotation = psPdfKitAnnotation } : throw new NotFoundException("Envelope receiver is not found."); try @@ -86,8 +86,8 @@ public class AnnotationController : ControllerBase { await _mediator.Publish(new RemoveSignatureNotification() { - EnvelopeId = docSignedNotification.EnvelopeId, - ReceiverId = docSignedNotification.ReceiverId + EnvelopeId = docSignedNotification.EnvelopeReceiver.EnvelopeId, + ReceiverId = docSignedNotification.EnvelopeReceiver.ReceiverId }, cancel); throw; } diff --git a/EnvelopeGenerator.Application/Common/Notifications/DocSigned/DocSignedNotification.cs b/EnvelopeGenerator.Application/Common/Notifications/DocSigned/DocSignedNotification.cs index 802519dd..fc9b91eb 100644 --- a/EnvelopeGenerator.Application/Common/Notifications/DocSigned/DocSignedNotification.cs +++ b/EnvelopeGenerator.Application/Common/Notifications/DocSigned/DocSignedNotification.cs @@ -16,25 +16,29 @@ namespace EnvelopeGenerator.Application.Common.Notifications.DocSigned; public record PsPdfKitAnnotation(ExpandoObject Instant, IEnumerable Structured); /// -/// +/// Notification raised when a document is signed by a receiver. /// -/// -public record DocSignedNotification(EnvelopeReceiverDto Original) : EnvelopeReceiverDto(Original), INotification, ISendMailNotification +public record DocSignedNotification : INotification, ISendMailNotification { /// - /// + /// The envelope receiver information. + /// + public required EnvelopeReceiverDto EnvelopeReceiver { get; init; } + + /// + /// The PSPDFKit annotation data. /// public PsPdfKitAnnotation? PsPdfKitAnnotation { get; init; } /// - /// + /// Gets the email template type. /// public EmailTemplateType TemplateType => EmailTemplateType.DocumentSigned; /// - /// + /// Gets the email address of the receiver. /// - public string EmailAddress => Receiver?.EmailAddress + public string EmailAddress => EnvelopeReceiver.Receiver?.EmailAddress ?? throw new InvalidOperationException($"Receiver is null." + $"DocSignedNotification:\n{this.ToJson(Format.Json.ForDiagnostics)}"); } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Common/Notifications/DocSigned/Handlers/DocStatusHandler.cs b/EnvelopeGenerator.Application/Common/Notifications/DocSigned/Handlers/DocStatusHandler.cs index dd90d3c5..4951aef6 100644 --- a/EnvelopeGenerator.Application/Common/Notifications/DocSigned/Handlers/DocStatusHandler.cs +++ b/EnvelopeGenerator.Application/Common/Notifications/DocSigned/Handlers/DocStatusHandler.cs @@ -31,8 +31,8 @@ public class DocStatusHandler : INotificationHandler /// public Task Handle(DocSignedNotification notification, CancellationToken cancel) => _sender.Send(new CreateDocStatusCommand() { - EnvelopeId = notification.EnvelopeId, - ReceiverId = notification.ReceiverId, + EnvelopeId = notification.EnvelopeReceiver.EnvelopeId, + ReceiverId = notification.EnvelopeReceiver.ReceiverId, Value = notification.PsPdfKitAnnotation is PsPdfKitAnnotation annot ? JsonSerializer.Serialize(annot.Instant, Format.Json.ForAnnotations) : BlankAnnotationJson diff --git a/EnvelopeGenerator.Application/Common/Notifications/DocSigned/Handlers/HistoryHandler.cs b/EnvelopeGenerator.Application/Common/Notifications/DocSigned/Handlers/HistoryHandler.cs index 80178e6c..d825f0d6 100644 --- a/EnvelopeGenerator.Application/Common/Notifications/DocSigned/Handlers/HistoryHandler.cs +++ b/EnvelopeGenerator.Application/Common/Notifications/DocSigned/Handlers/HistoryHandler.cs @@ -29,13 +29,13 @@ public class HistoryHandler : INotificationHandler /// public async Task Handle(DocSignedNotification notification, CancellationToken cancel) { - if (notification.Receiver is null) + if (notification.EnvelopeReceiver.Receiver is null) throw new InvalidOperationException($"Receiver information is missing in the notification. DocSignedNotification:\n {notification.ToJson(Format.Json.ForDiagnostics)}"); await _sender.Send(new CreateHistoryCommand() { - EnvelopeId = notification.EnvelopeId, - UserReference = notification.Receiver.EmailAddress, + EnvelopeId = notification.EnvelopeReceiver.EnvelopeId, + UserReference = notification.EnvelopeReceiver.Receiver.EmailAddress, Status = EnvelopeStatus.DocumentSigned, }, cancel); } diff --git a/EnvelopeGenerator.Application/Common/Notifications/DocSigned/Handlers/SendSignedMailHandler.cs b/EnvelopeGenerator.Application/Common/Notifications/DocSigned/Handlers/SendSignedMailHandler.cs index 8b7aa4f1..eac9104d 100644 --- a/EnvelopeGenerator.Application/Common/Notifications/DocSigned/Handlers/SendSignedMailHandler.cs +++ b/EnvelopeGenerator.Application/Common/Notifications/DocSigned/Handlers/SendSignedMailHandler.cs @@ -31,7 +31,7 @@ public class SendSignedMailHandler : SendMailHandler protected override void ConfigureEmailOut(DocSignedNotification notification, EmailOut emailOut) { emailOut.ReferenceString = notification.EmailAddress; - emailOut.ReferenceId = notification.ReceiverId; + emailOut.ReferenceId = notification.EnvelopeReceiver.ReceiverId; } /// @@ -42,11 +42,11 @@ public class SendSignedMailHandler : SendMailHandler { var placeHolders = new Dictionary() { - { "[NAME_RECEIVER]", notification.Name ?? string.Empty }, - { "[DOCUMENT_TITLE]", notification.Envelope?.Title ?? string.Empty }, + { "[NAME_RECEIVER]", notification.EnvelopeReceiver.Name ?? string.Empty }, + { "[DOCUMENT_TITLE]", notification.EnvelopeReceiver.Envelope?.Title ?? string.Empty }, }; - if (notification.Envelope.IsReadAndConfirm()) + if (notification.EnvelopeReceiver.Envelope.IsReadAndConfirm()) { placeHolders["[SIGNATURE_TYPE]"] = "Lesen und bestätigen"; placeHolders["[DOCUMENT_PROCESS]"] = string.Empty; diff --git a/EnvelopeGenerator.Tests/Application/DocSignedNotificationTests.cs b/EnvelopeGenerator.Tests/Application/DocSignedNotificationTests.cs index 9d2bd900..45479f98 100644 --- a/EnvelopeGenerator.Tests/Application/DocSignedNotificationTests.cs +++ b/EnvelopeGenerator.Tests/Application/DocSignedNotificationTests.cs @@ -53,7 +53,7 @@ public class DocSignedNotificationTests : TestBase var annots = Services.GetRequiredService(); - var docSignedNtf = new DocSignedNotification(envRcvDto) { PsPdfKitAnnotation = annots }; + var docSignedNtf = new DocSignedNotification { EnvelopeReceiver = envRcvDto, PsPdfKitAnnotation = annots }; var sendSignedMailHandler = Host.Services.GetRequiredService(); diff --git a/EnvelopeGenerator.Web/Controllers/AnnotationController.cs b/EnvelopeGenerator.Web/Controllers/AnnotationController.cs index adc4a041..16dedf25 100644 --- a/EnvelopeGenerator.Web/Controllers/AnnotationController.cs +++ b/EnvelopeGenerator.Web/Controllers/AnnotationController.cs @@ -72,7 +72,7 @@ public class AnnotationController : ControllerBase var envelopeReceiverDto = await _mediator.ReadEnvelopeReceiverAsync(uuid, signature, cancel); var docSignedNotification = envelopeReceiverDto is not null - ? new DocSignedNotification(envelopeReceiverDto) { PsPdfKitAnnotation = psPdfKitAnnotation } + ? new DocSignedNotification { EnvelopeReceiver = envelopeReceiverDto, PsPdfKitAnnotation = psPdfKitAnnotation } : throw new NotFoundException("Envelope receiver is not found."); try @@ -83,8 +83,8 @@ public class AnnotationController : ControllerBase { await _mediator.Publish(new RemoveSignatureNotification() { - EnvelopeId = docSignedNotification.EnvelopeId, - ReceiverId = docSignedNotification.ReceiverId + EnvelopeId = docSignedNotification.EnvelopeReceiver.EnvelopeId, + ReceiverId = docSignedNotification.EnvelopeReceiver.ReceiverId }, cancel); throw; }