feat(history): CreateHistoryCommand wurde verbessert, um DTO zurückzugeben und den Empfänger zu validieren.
- Der Rückgabetyp des Handlers wurde von „long?“ zu „EnvelopeHistoryDto?“ geändert. - AutoMapper-Integration für die Zuordnung von EnvelopeHistory zu DTO hinzugefügt. - IRepository<EnvelopeReceiver> zur Validierung der Benutzerreferenz eingeführt. - Validierung implementiert, um sicherzustellen, dass genau ein Empfänger gefunden wird. - BadRequestException-Behandlung für fehlende oder mehrere Empfänger hinzugefügt.
This commit is contained in:
parent
c67bac3e16
commit
8ca0519dbc
@ -1,17 +1,20 @@
|
|||||||
using DigitalData.Core.Abstraction.Application.Repository;
|
using AutoMapper;
|
||||||
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
|
using DigitalData.Core.Exceptions;
|
||||||
|
using EnvelopeGenerator.Application.Dto.EnvelopeHistory;
|
||||||
|
using EnvelopeGenerator.Application.Extensions;
|
||||||
using EnvelopeGenerator.Application.Model;
|
using EnvelopeGenerator.Application.Model;
|
||||||
|
using EnvelopeGenerator.Domain.Constants;
|
||||||
using EnvelopeGenerator.Domain.Entities;
|
using EnvelopeGenerator.Domain.Entities;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using EnvelopeGenerator.Application.Extensions;
|
|
||||||
using EnvelopeGenerator.Domain.Constants;
|
|
||||||
|
|
||||||
namespace EnvelopeGenerator.Application.Histories.Commands;
|
namespace EnvelopeGenerator.Application.Histories.Commands;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record CreateHistoryCommand : EnvelopeReceiverQueryBase, IRequest<long?>
|
public record CreateHistoryCommand : EnvelopeReceiverQueryBase, IRequest<EnvelopeHistoryDto?>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
@ -47,17 +50,25 @@ public record CreateHistoryCommand : EnvelopeReceiverQueryBase, IRequest<long?>
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class CreateHistoryCommandHandler : IRequestHandler<CreateHistoryCommand, long?>
|
public class CreateHistoryCommandHandler : IRequestHandler<CreateHistoryCommand, EnvelopeHistoryDto?>
|
||||||
{
|
{
|
||||||
private readonly IRepository<EnvelopeHistory> _repo;
|
private readonly IRepository<EnvelopeHistory> _repo;
|
||||||
|
|
||||||
|
private readonly IRepository<EnvelopeReceiver> _erRepo;
|
||||||
|
|
||||||
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="repo"></param>
|
/// <param name="repo"></param>
|
||||||
public CreateHistoryCommandHandler(IRepository<EnvelopeHistory> repo)
|
/// <param name="erRepo"></param>
|
||||||
|
/// <param name="mapper"></param>
|
||||||
|
public CreateHistoryCommandHandler(IRepository<EnvelopeHistory> repo, IRepository<EnvelopeReceiver> erRepo, IMapper mapper)
|
||||||
{
|
{
|
||||||
_repo = repo;
|
_repo = repo;
|
||||||
|
_erRepo = erRepo;
|
||||||
|
_mapper = mapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -66,26 +77,32 @@ public class CreateHistoryCommandHandler : IRequestHandler<CreateHistoryCommand,
|
|||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <param name="cancel"></param>
|
/// <param name="cancel"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<long?> Handle(CreateHistoryCommand request, CancellationToken cancel)
|
public async Task<EnvelopeHistoryDto?> Handle(CreateHistoryCommand request, CancellationToken cancel)
|
||||||
{
|
{
|
||||||
|
if(request.UserReference is null)
|
||||||
|
{
|
||||||
|
var receivers = await _erRepo
|
||||||
|
.ReadOnly()
|
||||||
|
.Where(request)
|
||||||
|
.Include(er => er.Receiver)
|
||||||
|
.ToListAsync(cancel);
|
||||||
|
|
||||||
|
if (receivers.Count != 1)
|
||||||
|
throw new BadRequestException(
|
||||||
|
receivers.Count > 1
|
||||||
|
? "Multiple receivers found for the given envelope and receiver criteria."
|
||||||
|
: "No receiver found for the given envelope and receiver criteria."
|
||||||
|
);
|
||||||
|
|
||||||
|
var receiver = receivers.Single().Receiver
|
||||||
|
?? throw new BadRequestException("No receiver found for the given envelope and receiver criteria.");
|
||||||
|
|
||||||
|
request.UserReference = receiver.EmailAddress;
|
||||||
|
}
|
||||||
|
|
||||||
// create entitiy
|
// create entitiy
|
||||||
await _repo.CreateAsync(request, cancel);
|
var hist = await _repo.CreateAsync(request, cancel);
|
||||||
|
|
||||||
// check if created
|
return _mapper.Map<EnvelopeHistoryDto>(hist);
|
||||||
var query = _repo.ReadOnly();
|
|
||||||
|
|
||||||
query = request.EnvelopeId is null
|
|
||||||
? query.Where(request.Envelope)
|
|
||||||
: query.Where(h => h.EnvelopeId == request.EnvelopeId);
|
|
||||||
|
|
||||||
query = request.UserReference is null
|
|
||||||
? query.Where(request.Receiver)
|
|
||||||
: query.Where(h => h.UserReference == request.UserReference);
|
|
||||||
|
|
||||||
var record = await query
|
|
||||||
.Where(h => h.ActionDate == request.ActionDate)
|
|
||||||
.SingleOrDefaultAsync(cancel);
|
|
||||||
|
|
||||||
return record?.Id;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user