using AutoMapper;
using DigitalData.Core.Abstraction.Application.Repository;
using EnvelopeGenerator.Application.Envelopes.Queries;
using EnvelopeGenerator.Application.Receivers.Queries;
using MediatR;
using EnvelopeGenerator.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using EnvelopeGenerator.Application.Common.Dto.EnvelopeReceiver;
using EnvelopeGenerator.Application.Common.Query;
using EnvelopeGenerator.Application.Common.Extensions;
namespace EnvelopeGenerator.Application.EnvelopeReceivers.Queries;
///
/// Represents a query for reading an envelope receiver including sensitive fields
/// (access code, phone number) that are excluded from the standard .
///
///
/// Returns a single matched by UUID and receiver signature.
/// Equivalent to the legacy ReadWithSecretByUuidSignatureAsync service method.
///
public record ReadEnvelopeReceiverSecretQuery
: EnvelopeReceiverQueryBase,
IRequest;
///
/// Extension methods for dispatching via .
///
public static class ReadEnvelopeReceiverSecretQueryExtensions
{
///
/// Sends a using the composite key (uuid::signature).
///
/// The mediator instance.
/// Composite key in the format uuid::signature.
/// Cancellation token.
/// The matching , or null if not found.
public static Task ReadEnvelopeReceiverSecretAsync(
this IMediator mediator,
string key,
CancellationToken cancel = default)
=> mediator.Send(new ReadEnvelopeReceiverSecretQuery { Key = key }, cancel);
///
/// Sends a using UUID and receiver signature.
///
/// The mediator instance.
/// Envelope UUID.
/// Receiver signature.
/// Cancellation token.
/// The matching , or null if not found.
public static Task ReadEnvelopeReceiverSecretAsync(
this IMediator mediator,
string uuid,
string signature,
CancellationToken cancel = default)
{
var q = new ReadEnvelopeReceiverSecretQuery();
q.Envelope.Uuid = uuid;
q.Receiver.Signature = signature;
return mediator.Send(q, cancel);
}
///
/// Handles and returns a
/// containing sensitive fields.
///
public class ReadEnvelopeReceiverSecretQueryHandler
: IRequestHandler
{
private readonly IRepository _repo;
private readonly IRepository _rcvRepo;
private readonly IMapper _mapper;
///
/// Initializes a new instance of .
///
/// Repository for .
/// Repository for .
/// AutoMapper instance.
public ReadEnvelopeReceiverSecretQueryHandler(
IRepository envelopeReceiver,
IRepository rcvRepo,
IMapper mapper)
{
_repo = envelopeReceiver;
_rcvRepo = rcvRepo;
_mapper = mapper;
}
///
/// Handles the query and returns the matching .
///
/// The query containing filter criteria.
/// Cancellation token.
///
/// The matched , or null if no record is found.
///
public async Task Handle(
ReadEnvelopeReceiverSecretQuery request,
CancellationToken cancel)
{
var q = _repo.Query.Where(request, notnull: false);
var envRcvs = await q
.Include(er => er.Envelope).ThenInclude(e => e!.Documents!).ThenInclude(d => d.Elements)
.Include(er => er.Envelope).ThenInclude(e => e!.Histories)
.Include(er => er.Envelope).ThenInclude(e => e!.User)
.Include(er => er.Receiver)
.ToListAsync(cancel);
if (request.Receiver.HasAnyCriteria && envRcvs.Count != 0)
{
var receiver = await _rcvRepo.Query.Where(request.Receiver).FirstAsync(cancel);
foreach (var item in envRcvs)
item.Envelope?.Documents?.FirstOrDefault()?.Elements?.RemoveAll(s => s.ReceiverId != receiver.Id);
}
var envRcv = envRcvs.FirstOrDefault();
if (envRcv is null)
return null;
return _mapper.Map(envRcv);
}
}
}