using EnvelopeGenerator.Application.Common.Dto; using EnvelopeGenerator.Application.Common.Dto.EnvelopeReceiver; using EnvelopeGenerator.Application.Common.Extensions; using EnvelopeGenerator.Application.Common.Notifications.RemoveSignature; using EnvelopeGenerator.Domain.Constants; using MediatR; using System.Dynamic; namespace EnvelopeGenerator.Application.Common.Notifications.DocSigned; /// /// /// /// /// public record PsPdfKitAnnotation(ExpandoObject Instant, IEnumerable Structured); /// /// /// /// public record DocSignedNotification(EnvelopeReceiverDto Original) : EnvelopeReceiverDto(Original), INotification, ISendMailNotification { /// /// /// public PsPdfKitAnnotation PsPdfKitAnnotation { get; init; } = null!; /// /// /// public EmailTemplateType TemplateType => EmailTemplateType.DocumentSigned; /// /// /// public string EmailAddress => Receiver?.EmailAddress ?? throw new InvalidOperationException($"Receiver is null." + $"DocSignedNotification:\n{this.ToJson(Format.Json.ForDiagnostics)}"); } /// /// /// public static class DocSignedNotificationExtensions { /// /// Converts an to a . /// /// The DTO to convert. /// /// A new instance. public static DocSignedNotification ToDocSignedNotification(this EnvelopeReceiverDto dto, PsPdfKitAnnotation psPdfKitAnnotation) => new(dto) { PsPdfKitAnnotation = psPdfKitAnnotation }; /// /// /// /// /// /// public static async Task ToDocSignedNotification(this Task dtoTask, PsPdfKitAnnotation psPdfKitAnnotation) => await dtoTask is EnvelopeReceiverDto dto ? new(dto) { PsPdfKitAnnotation = psPdfKitAnnotation } : null; /// /// /// /// /// /// /// public static async Task PublishSafely(this IPublisher publisher, DocSignedNotification notification, CancellationToken cancel = default) { try { await publisher.Publish(notification, cancel); } catch (Exception) { await publisher.Publish(new RemoveSignatureNotification() { EnvelopeId = notification.EnvelopeId, ReceiverId = notification.ReceiverId }, cancel); throw; } } }