- Introduced PublishSafely method to safely publish DocSignedNotification events. - Added fallback logic to publish RemoveSignatureNotification when publishing fails. - Added missing using directive for RemoveSignatureNotification namespace.
88 lines
3.0 KiB
C#
88 lines
3.0 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="Instant"></param>
|
|
/// <param name="Structured"></param>
|
|
public record PsPdfKitAnnotation(ExpandoObject Instant, IEnumerable<AnnotationCreateDto> Structured);
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="Original"></param>
|
|
public record DocSignedNotification(EnvelopeReceiverDto Original) : EnvelopeReceiverDto(Original), INotification, ISendMailNotification
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public PsPdfKitAnnotation PsPdfKitAnnotation { get; init; } = null!;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public EmailTemplateType TemplateType => EmailTemplateType.DocumentSigned;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string EmailAddress => Receiver?.EmailAddress
|
|
?? throw new InvalidOperationException($"Receiver is null." +
|
|
$"DocSignedNotification:\n{this.ToJson(Format.Json.ForDiagnostics)}");
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class DocSignedNotificationExtensions
|
|
{
|
|
/// <summary>
|
|
/// Converts an <see cref="EnvelopeReceiverDto"/> to a <see cref="DocSignedNotification"/>.
|
|
/// </summary>
|
|
/// <param name="dto">The DTO to convert.</param>
|
|
/// <param name="psPdfKitAnnotation"></param>
|
|
/// <returns>A new <see cref="DocSignedNotification"/> instance.</returns>
|
|
public static DocSignedNotification ToDocSignedNotification(this EnvelopeReceiverDto dto, PsPdfKitAnnotation psPdfKitAnnotation)
|
|
=> new(dto) { PsPdfKitAnnotation = psPdfKitAnnotation };
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="dtoTask"></param>
|
|
/// <param name="psPdfKitAnnotation"></param>
|
|
/// <returns></returns>
|
|
public static async Task<DocSignedNotification?> ToDocSignedNotification(this Task<EnvelopeReceiverDto?> dtoTask, PsPdfKitAnnotation psPdfKitAnnotation)
|
|
=> await dtoTask is EnvelopeReceiverDto dto ? new(dto) { PsPdfKitAnnotation = psPdfKitAnnotation } : null;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="publisher"></param>
|
|
/// <param name="notification"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
} |