Enhance SaveSignatureBehavior with validation and repo logic

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.
This commit is contained in:
2026-06-10 15:18:53 +02:00
parent 04e30b0d79
commit 90661cb856

View File

@@ -1,22 +1,11 @@
using AutoMapper;
using DigitalData.Core.Abstraction.Application.Repository;
using DigitalData.Core.Exceptions;
using EnvelopeGenerator.Application.Common.Dto; using EnvelopeGenerator.Application.Common.Dto;
using EnvelopeGenerator.Application.DocStatus.Commands; using EnvelopeGenerator.Application.DocReceiverElements.Commands;
using EnvelopeGenerator.Domain.Constants; using EnvelopeGenerator.Domain.Entities;
using MediatR; using MediatR;
using System.Text.Json; using Microsoft.EntityFrameworkCore;
<<<<<<< TODO: Unmerged change from project 'EnvelopeGenerator.Application (net8.0)', Before:
=======
using EnvelopeGenerator.Application.DocReceiverElements.Commands.SigningCommand.SigningCommand;
using EnvelopeGenerator.Application.DocReceiverElements.Commands.SigningCommand;
>>>>>>> After
<<<<<<< TODO: Unmerged change from project 'EnvelopeGenerator.Application (net9.0)', Before:
=======
using EnvelopeGenerator.Application.DocReceiverElements.Commands.SigningCommand;
>>>>>>> After
using EnvelopeGenerator.Application.DocReceiverElements.Commands.SigningCommand.SigningCommand.SigningCommand;
using EnvelopeGenerator.Application.DocReceiverElements.Commands.SigningCommand.SigningCommand;
using EnvelopeGenerator.Application.DocReceiverElements.Commands.SigningCommand;
namespace EnvelopeGenerator.Application.DocReceiverElements.Behaviors; namespace EnvelopeGenerator.Application.DocReceiverElements.Behaviors;
@@ -28,13 +17,22 @@ public class SaveSignatureBehavior : IPipelineBehavior<SigningCommand, Unit>
{ {
private readonly ISender _sender; private readonly ISender _sender;
private readonly IRepository<DocReceiverElement> _elementRepo;
private readonly IMapper _mapper;
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
public SaveSignatureBehavior(ISender sender) /// <param name="elementRepo"></param>
/// <param name="mapper"></param>
public SaveSignatureBehavior(ISender sender, IRepository<DocReceiverElement> elementRepo, IMapper mapper)
{ {
_sender = sender; _sender = sender;
_elementRepo = elementRepo;
_elementRepo = elementRepo;
_mapper = mapper;
} }
/// <summary> /// <summary>
@@ -42,10 +40,31 @@ public class SaveSignatureBehavior : IPipelineBehavior<SigningCommand, Unit>
/// </summary> /// </summary>
/// <param name="request"></param> /// <param name="request"></param>
/// <param name="next"></param> /// <param name="next"></param>
/// <param name="cancellationToken"></param> /// <param name="cancel"></param>
/// <returns></returns> /// <returns></returns>
public async Task<Unit> Handle(SigningCommand request, RequestHandlerDelegate<Unit> next, CancellationToken cancellationToken) public async Task<Unit> Handle(SigningCommand request, RequestHandlerDelegate<Unit> next, CancellationToken cancel)
{ {
return await next(cancellationToken); 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);
} }
} }