From 3bb2a013abbe5dad9934e5aa80a5509057580b02 Mon Sep 17 00:00:00 2001 From: TekH Date: Tue, 9 Jun 2026 22:43:19 +0200 Subject: [PATCH] Add EnvelopeReceiverResolutionBehavior pipeline behavior Introduced a new pipeline behavior class `EnvelopeReceiverResolutionBehavior` to resolve and validate the `EnvelopeReceiver` during the signing process. - Added necessary `using` directives for dependencies such as `AutoMapper`, `MediatR`, and `IRepository`. - Implemented the `Handle` method to query the database for `EnvelopeReceiver` if not provided in the `SignCommand` request. - Throws a `NotFoundException` if the `EnvelopeReceiver` is not found. - Maps the retrieved entity to a DTO and sets it in the request. - Ensures the behavior executes before other signing process behaviors. --- .../EnvelopeReceiverResolutionBehavior.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 EnvelopeGenerator.Application/Signatures/Behaviors/EnvelopeReceiverResolutionBehavior.cs diff --git a/EnvelopeGenerator.Application/Signatures/Behaviors/EnvelopeReceiverResolutionBehavior.cs b/EnvelopeGenerator.Application/Signatures/Behaviors/EnvelopeReceiverResolutionBehavior.cs new file mode 100644 index 00000000..c90603e3 --- /dev/null +++ b/EnvelopeGenerator.Application/Signatures/Behaviors/EnvelopeReceiverResolutionBehavior.cs @@ -0,0 +1,54 @@ +using AutoMapper; +using DigitalData.Core.Abstraction.Application.Repository; +using DigitalData.Core.Exceptions; +using EnvelopeGenerator.Application.Common.Dto.EnvelopeReceiver; +using EnvelopeGenerator.Application.Common.Extensions; +using EnvelopeGenerator.Application.Signatures.Commands; +using EnvelopeGenerator.Domain.Entities; +using MediatR; +using Microsoft.EntityFrameworkCore; + +namespace EnvelopeGenerator.Application.Signatures.Behaviors; + +/// +/// Pipeline behavior that resolves and validates EnvelopeReceiver. +/// Executes FIRST in the signing process - before all other behaviors. +/// If EnvelopeReceiver is not provided, it queries the database using EnvelopeReceiverQueryBase parameters. +/// +public class EnvelopeReceiverResolutionBehavior : IPipelineBehavior +{ + private readonly IRepository _erRepo; + private readonly IMapper _mapper; + + /// + /// + /// + /// + /// + public EnvelopeReceiverResolutionBehavior(IRepository erRepo, IMapper mapper) + { + _erRepo = erRepo; + _mapper = mapper; + } + + /// + /// + /// + /// + /// + /// + /// + public async Task Handle(SignCommand request, RequestHandlerDelegate next, CancellationToken cancellationToken) + { + // If EnvelopeReceiver is not provided, query it from database + if (request.EnvelopeReceiver is null) + { + var er = await _erRepo.Query.Where(request, notnull: true).SingleOrDefaultAsync(cancellationToken) + ?? throw new NotFoundException("EnvelopeReceiver not found"); + + request.SetEnvelopeReceiver(_mapper.Map(er)); + } + + return await next(cancellationToken); + } +}