Files
EnvelopeGenerator/EnvelopeGenerator.Application/DocReceiverElements/Behaviors/SaveSignatureBehavior.cs
TekH 380a5f4c0c Add signature submission feature and pipeline updates
Introduced a new feature for submitting signatures, including
backend and frontend changes. Added `SignatureSubmitDto` and
`SubmitAsync` in `SignatureService` to handle signature data
submission. Implemented a new `Submit` endpoint in
`SignatureController` to validate and process signature payloads.

Updated pipeline behaviors to include `SaveSignatureBehavior`
and adjusted the execution order. Enhanced query logic in
`EnvelopeReceiverResolutionBehavior` and `SaveSignatureBehavior`
to align with the updated data model.

Refactored `EGDbContext` to improve trigger handling and logging.
Updated `ReceiverSignedPage.razor` to integrate `SignatureService`
and handle submission errors gracefully. Adjusted application
URLs in `launchSettings.json` and added new database triggers
in `appsettings.json`.
2026-09-28 12:21:44 +02:00

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.EnvelopeReceiver.EnvelopeId)
.Where(e => e.ReceiverId == request.EnvelopeReceiver.ReceiverId)
.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);
}
}