using EnvelopeGenerator.Application.Common.Dto;
using EnvelopeGenerator.Application.DocStatus.Commands;
using EnvelopeGenerator.Application.Signature.Commands;
using EnvelopeGenerator.Domain.Constants;
using MediatR;
using System.Text.Json;
namespace EnvelopeGenerator.Application.Signature.Behaviors;
///
/// Pipeline behavior that creates document status.
/// Executes second in the signing process.
///
public class DocStatusBehavior : IPipelineBehavior
{
private const string BlankAnnotationJson = "{}";
private readonly ISender _sender;
///
///
///
///
public DocStatusBehavior(ISender sender)
{
_sender = sender;
}
///
///
///
///
///
///
///
public async Task Handle(SignCommand request, RequestHandlerDelegate next, CancellationToken cancellationToken)
{
await _sender.Send(new CreateDocStatusCommand()
{
EnvelopeId = request.EnvelopeReceiver.EnvelopeId,
ReceiverId = request.EnvelopeReceiver.ReceiverId,
Value = request.PsPdfKitAnnotation is PsPdfKitAnnotation annot
? JsonSerializer.Serialize(annot.Instant, Format.Json.ForAnnotations)
: BlankAnnotationJson
}, cancellationToken);
return await next();
}
}