From 90661cb856eaebf58c77c852d0177f7e83c827a4 Mon Sep 17 00:00:00 2001 From: TekH Date: Wed, 10 Jun 2026 15:18:53 +0200 Subject: [PATCH] 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. --- .../Behaviors/SaveSignatureBehavior.cs | 61 ++++++++++++------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/EnvelopeGenerator.Application/DocReceiverElements/Behaviors/SaveSignatureBehavior.cs b/EnvelopeGenerator.Application/DocReceiverElements/Behaviors/SaveSignatureBehavior.cs index 0172ea62..e4aecec0 100644 --- a/EnvelopeGenerator.Application/DocReceiverElements/Behaviors/SaveSignatureBehavior.cs +++ b/EnvelopeGenerator.Application/DocReceiverElements/Behaviors/SaveSignatureBehavior.cs @@ -1,22 +1,11 @@ +using AutoMapper; +using DigitalData.Core.Abstraction.Application.Repository; +using DigitalData.Core.Exceptions; using EnvelopeGenerator.Application.Common.Dto; -using EnvelopeGenerator.Application.DocStatus.Commands; -using EnvelopeGenerator.Domain.Constants; +using EnvelopeGenerator.Application.DocReceiverElements.Commands; +using EnvelopeGenerator.Domain.Entities; using MediatR; -using System.Text.Json; - -<<<<<<< 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; +using Microsoft.EntityFrameworkCore; namespace EnvelopeGenerator.Application.DocReceiverElements.Behaviors; @@ -28,13 +17,22 @@ public class SaveSignatureBehavior : IPipelineBehavior { private readonly ISender _sender; + private readonly IRepository _elementRepo; + + private readonly IMapper _mapper; + /// /// /// /// - public SaveSignatureBehavior(ISender sender) + /// + /// + public SaveSignatureBehavior(ISender sender, IRepository elementRepo, IMapper mapper) { _sender = sender; + _elementRepo = elementRepo; + _elementRepo = elementRepo; + _mapper = mapper; } /// @@ -42,10 +40,31 @@ public class SaveSignatureBehavior : IPipelineBehavior /// /// /// - /// + /// /// - public async Task Handle(SigningCommand request, RequestHandlerDelegate next, CancellationToken cancellationToken) + public async Task Handle(SigningCommand request, RequestHandlerDelegate next, CancellationToken cancel) { - return await next(cancellationToken); + if (request.ReceiverAppType == ReceiverAppType.LegacyWeb) + return await next(cancel); + else if(request.Signatures is not IEnumerable 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); } }