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 {