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.
This commit is contained in:
@@ -104,6 +104,13 @@ public class CreateEnvelopeCommandHandler : IRequestHandler<CreateEnvelopeComman
|
||||
UseAccessCode = false,
|
||||
SendReminderEmails = false
|
||||
}, cancel);
|
||||
|
||||
await Mediator.Send(new CreateHistoryCommand
|
||||
{
|
||||
EnvelopeId = envelope.Id,
|
||||
UserReference = await ResolveSenderReferenceAsync(envelope, cancel),
|
||||
Status = EnvelopeStatus.EnvelopeCreated
|
||||
}, cancel);
|
||||
}
|
||||
|
||||
await UpsertDocumentAsync(envelope.Id, request.Document, now, cancel);
|
||||
@@ -121,6 +128,22 @@ public class CreateEnvelopeCommandHandler : IRequestHandler<CreateEnvelopeComman
|
||||
return _mapper.Map<EnvelopeDto>(envelope);
|
||||
}
|
||||
|
||||
private async Task<string> 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<CreateEnvelopeComman
|
||||
e.ChangedWhen = changedWhen;
|
||||
}, e => 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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user