From 519a8bf3f9dd7a117bf8b5394d46ed7105cfd0ea Mon Sep 17 00:00:00 2001 From: TekH Date: Sun, 27 Sep 2026 20:20:01 +0200 Subject: [PATCH] Refactor sender resolution and add history logging Refactored sender email resolution into a new private async method `ResolveSenderReferenceAsync` to improve code reusability and readability. Replaced inline logic for resolving sender references with this method. Added a `CreateHistoryCommand` to log envelope creation events, including envelope ID, sender reference, and status (`EnvelopeCreated`). This enhances auditability and ensures consistent event logging. --- .../Commands/CreateEnvelopeCommandHandler.cs | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeCommandHandler.cs b/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeCommandHandler.cs index 6229e4f7..5dcdc03c 100644 --- a/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeCommandHandler.cs +++ b/EnvelopeGenerator.Application/Envelopes/Commands/CreateEnvelopeCommandHandler.cs @@ -104,6 +104,13 @@ public class CreateEnvelopeCommandHandler : IRequestHandler(envelope); } + private async Task ResolveSenderReferenceAsync(Envelope envelope, CancellationToken cancel) + { + if (!string.IsNullOrWhiteSpace(envelope.User?.Email)) + return envelope.User.Email; + + var senderEmail = await Repository.Query + .Where(e => e.Id == envelope.Id) + .Select(e => e.User.Email) + .FirstOrDefaultAsync(cancel); + + if (string.IsNullOrWhiteSpace(senderEmail)) + throw new BadRequestException("Envelope sender email is missing."); + + return senderEmail; + } + private async Task UpsertDocumentAsync(int envelopeId, CreateEnvelopeDocumentDto? document, DateTime now, CancellationToken cancel) { if (document is null || string.IsNullOrWhiteSpace(document.DataAsBase64)) @@ -338,9 +361,7 @@ public class CreateEnvelopeCommandHandler : IRequestHandler e.Id == envelope.Id, cancel); - var userReference = envelope.User?.Email; - if (string.IsNullOrWhiteSpace(userReference)) - throw new BadRequestException("Envelope sender email is missing."); + var userReference = await ResolveSenderReferenceAsync(envelope, cancel); await Mediator.Send(new CreateHistoryCommand {