Updated SaveSignatureBehavior.cs to improve functionality: - Added new dependencies: AutoMapper, IRepository, and EF Core. - Enhanced constructor to initialize new dependencies. - Updated Handle method to validate signatures and handle errors. - Introduced repository operations for querying and updating elements. - Improved error handling with BadRequestException. - Cleaned up redundant code and resolved namespace conflicts.
71 lines
2.5 KiB
C#
71 lines
2.5 KiB
C#
using AutoMapper;
|
|
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using DigitalData.Core.Exceptions;
|
|
using EnvelopeGenerator.Application.Common.Dto;
|
|
using EnvelopeGenerator.Application.DocReceiverElements.Commands;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace EnvelopeGenerator.Application.DocReceiverElements.Behaviors;
|
|
|
|
/// <summary>
|
|
/// Pipeline behavior that creates document status.
|
|
/// Executes second in the signing process.
|
|
/// </summary>
|
|
public class SaveSignatureBehavior : IPipelineBehavior<SigningCommand, Unit>
|
|
{
|
|
private readonly ISender _sender;
|
|
|
|
private readonly IRepository<DocReceiverElement> _elementRepo;
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="elementRepo"></param>
|
|
/// <param name="mapper"></param>
|
|
public SaveSignatureBehavior(ISender sender, IRepository<DocReceiverElement> elementRepo, IMapper mapper)
|
|
{
|
|
_sender = sender;
|
|
_elementRepo = elementRepo;
|
|
_elementRepo = elementRepo;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="next"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public async Task<Unit> Handle(SigningCommand request, RequestHandlerDelegate<Unit> next, CancellationToken cancel)
|
|
{
|
|
if (request.ReceiverAppType == ReceiverAppType.LegacyWeb)
|
|
return await next(cancel);
|
|
else if(request.Signatures is not IEnumerable<Signature> signatures)
|
|
throw new BadRequestException($"Signatures are required for saving signature behavior.");
|
|
|
|
var elements = await _elementRepo
|
|
.Where(e => e.Document.EnvelopeId == request.Envelope.Id)
|
|
.Where(e => e.ReceiverId == request.Receiver.Id)
|
|
.ToListAsync(cancel);
|
|
|
|
foreach (var element in elements)
|
|
{
|
|
var signatures = request.Signatures.Where(s => s.Id == element.Id).ToList();
|
|
if(signatures.Count == 0)
|
|
throw new BadRequestException("No signature found for element with id {element.Id}.");
|
|
else if(signatures.Count > 1)
|
|
throw new BadRequestException("Multiple signatures found for element with id {element.Id}.");
|
|
|
|
await _elementRepo.UpdateAsync(signatures.First(), e => e.Id == element.Id, cancel);
|
|
}
|
|
|
|
return await next(cancel);
|
|
}
|
|
}
|