diff --git a/EnvelopeGenerator.Application/Common/Notifications/Handlers/DocSignedNotificationHistoryHandler.cs b/EnvelopeGenerator.Application/Common/Notifications/Handlers/DocSignedNotificationHistoryHandler.cs new file mode 100644 index 00000000..31c8799a --- /dev/null +++ b/EnvelopeGenerator.Application/Common/Notifications/Handlers/DocSignedNotificationHistoryHandler.cs @@ -0,0 +1,45 @@ +using EnvelopeGenerator.Application.DocStatus.Commands; +using EnvelopeGenerator.Application.Histories.Commands; +using EnvelopeGenerator.Domain.Constants; +using MediatR; +using Newtonsoft.Json; + +namespace EnvelopeGenerator.Application.Common.Notifications.Handlers; + +/// +/// +/// +public class DocSignedNotificationHistoryHandler : INotificationHandler +{ + private readonly ISender _sender; + + /// + /// + /// + /// + public DocSignedNotificationHistoryHandler(ISender sender) + { + _sender = sender; + } + + /// + /// + /// + /// + /// + /// + public async Task Handle(DocSignedNotification notification, CancellationToken cancel) + { + if(notification.Receiver is null) + if (notification.Receiver is null) + throw new InvalidOperationException($"Receiver information is missing in the notification. DocSignedNotification:\n {JsonConvert.SerializeObject(notification, Format.Json.ForDiagnostics)}"); + + await _sender.Send(new CreateHistoryCommand() + { + EnvelopeId = notification.EnvelopeId, + UserReference = notification.Receiver.EmailAddress, + Status = EnvelopeStatus.DocumentSigned, + Comment = JsonConvert.SerializeObject(notification.Annotations, Format.Json.ForAnnotations) + }, cancel); + } +} \ No newline at end of file