diff --git a/EnvelopeGenerator.Application/Histories/Commands/CreateHistoryCommand.cs b/EnvelopeGenerator.Application/Histories/Commands/CreateHistoryCommand.cs index 6b0169a7..c0013bda 100644 --- a/EnvelopeGenerator.Application/Histories/Commands/CreateHistoryCommand.cs +++ b/EnvelopeGenerator.Application/Histories/Commands/CreateHistoryCommand.cs @@ -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.Domain.Constants; using EnvelopeGenerator.Domain.Entities; using MediatR; using Microsoft.EntityFrameworkCore; -using EnvelopeGenerator.Application.Extensions; -using EnvelopeGenerator.Domain.Constants; namespace EnvelopeGenerator.Application.Histories.Commands; /// /// /// -public record CreateHistoryCommand : EnvelopeReceiverQueryBase, IRequest +public record CreateHistoryCommand : EnvelopeReceiverQueryBase, IRequest { /// /// @@ -47,17 +50,25 @@ public record CreateHistoryCommand : EnvelopeReceiverQueryBase, IRequest /// /// /// -public class CreateHistoryCommandHandler : IRequestHandler +public class CreateHistoryCommandHandler : IRequestHandler { private readonly IRepository _repo; + private readonly IRepository _erRepo; + + private readonly IMapper _mapper; + /// /// /// /// - public CreateHistoryCommandHandler(IRepository repo) + /// + /// + public CreateHistoryCommandHandler(IRepository repo, IRepository erRepo, IMapper mapper) { _repo = repo; + _erRepo = erRepo; + _mapper = mapper; } /// @@ -66,26 +77,32 @@ public class CreateHistoryCommandHandler : IRequestHandler /// /// - public async Task Handle(CreateHistoryCommand request, CancellationToken cancel) + public async Task 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 - await _repo.CreateAsync(request, cancel); - - // check if created - 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; + var hist = await _repo.CreateAsync(request, cancel); + + return _mapper.Map(hist); } } \ No newline at end of file