- Replaced internal string properties with EnvelopeQuery and ReceiverQuery records - Updated Key property to encode/decode using the new structured types - Added overloaded IMediator extension methods IsSignedAsync for better usability - Simplified ReceiverAlreadySignedQueryHandler to work with the new structure
128 lines
3.5 KiB
C#
128 lines
3.5 KiB
C#
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using DigitalData.Core.Exceptions;
|
|
using EnvelopeGenerator.Domain;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using EnvelopeGenerator.Extensions;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace EnvelopeGenerator.Application.EnvelopeReceivers.Queries;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public record ReceiverAlreadySignedQuery : IRequest<bool>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string Key
|
|
{
|
|
get => (Envelope.Uuid, Receiver.Signature).EncodeEnvelopeReceiverId();
|
|
init
|
|
{
|
|
(string? EnvelopeUuid, string? ReceiverSignature) = value.DecodeEnvelopeReceiverId();
|
|
if (string.IsNullOrEmpty(EnvelopeUuid) || string.IsNullOrEmpty(ReceiverSignature))
|
|
{
|
|
throw new BadRequestException("Der EnvelopeReceiverKey muss ein gültiger Base64-kodierter String sein, der die EnvelopeUuid und die ReceiverSignature enthält.");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public EnvelopeQuery Envelope { get; set; } = new EnvelopeQuery();
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public ReceiverQuery Receiver { get; set; } = new ReceiverQuery();
|
|
}
|
|
|
|
#region Queries
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public record EnvelopeQuery()
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string Uuid { get; set; } = null!;
|
|
};
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public record ReceiverQuery()
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string Signature { get; set; } = null!;
|
|
};
|
|
#endregion
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class ReceiverAlreadySignedQueryExtensions
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="mediator"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public static Task<bool> IsSignedAsync(this IMediator mediator, string key, CancellationToken cancel = default)
|
|
=> mediator.Send(new ReceiverAlreadySignedQuery { Key = key }, cancel);
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="mediator"></param>
|
|
/// <param name="uuid"></param>
|
|
/// <param name="signature"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public static Task<bool> IsSignedAsync(this IMediator mediator, string uuid, string signature, CancellationToken cancel = default)
|
|
=> mediator.Send(new ReceiverAlreadySignedQuery
|
|
{
|
|
Envelope = new() { Uuid = uuid },
|
|
Receiver = new() { Signature = signature }
|
|
}, cancel);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class ReceiverAlreadySignedQueryHandler : IRequestHandler<ReceiverAlreadySignedQuery, bool>
|
|
{
|
|
private readonly IRepository<EnvelopeReceiver> _repo;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="repo"></param>
|
|
public ReceiverAlreadySignedQueryHandler(IRepository<EnvelopeReceiver> repo)
|
|
{
|
|
_repo = repo;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> Handle(ReceiverAlreadySignedQuery request, CancellationToken cancel = default)
|
|
{
|
|
return await _repo.Read()
|
|
.Where(er => er.Envelope.Uuid == request.Envelope.Uuid)
|
|
.Where(er => er.Receiver.Signature == request.Receiver.Signature)
|
|
.Where(er => er.Envelope.History.Any(hist => hist.Status == Constants.EnvelopeStatus.DocumentSigned))
|
|
.AnyAsync(cancel);
|
|
}
|
|
} |