Replaced UnauthorizedAccessException with a custom AuthorizationException in SignPdfByEnvelopeCommandHandler to provide more meaningful error handling. Added the EnvelopeGenerator.Domain.Exceptions namespace to support this change.
87 lines
3.0 KiB
C#
87 lines
3.0 KiB
C#
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using DigitalData.Core.Exceptions;
|
|
using EnvelopeGenerator.Application.Common.Query;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using EnvelopeGenerator.Domain.Exceptions;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace EnvelopeGenerator.Application.PdfSigning;
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public record SignPdfByEnvelopeCommand : EnvelopeQueryBase, IRequest<Stream>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public int? ReceiverId { get; init; } = null;
|
|
|
|
internal int? UserId { get; private set; } = null;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="userId"></param>
|
|
/// <returns></returns>
|
|
public SignPdfByEnvelopeCommand Authorized(int userId)
|
|
{
|
|
UserId = userId;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class SignPdfByEnvelopeCommandHandler(IRepository<Envelope> Repo, ISender sender) : IRequestHandler<SignPdfByEnvelopeCommand, Stream>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<Stream> Handle(SignPdfByEnvelopeCommand request, CancellationToken cancel)
|
|
{
|
|
var envelopeQuery = Repo.Query;
|
|
|
|
if (request.Id is int id)
|
|
envelopeQuery = envelopeQuery.Where(e => e.Id == id);
|
|
else if (request.Uuid is string uuid)
|
|
envelopeQuery = envelopeQuery.Where(e => e.Uuid == uuid);
|
|
|
|
var envelope = await envelopeQuery.Include(e => e.EnvelopeReceivers).Include(e => e.Documents).SingleOrDefaultAsync(cancel)
|
|
?? throw new NotFoundException($"Envelope is not found ID: {request.Id} UUID: {request.Uuid}");
|
|
|
|
if (request.UserId is int userId && userId != envelope.UserId)
|
|
throw new AuthorizationException($"Envelope is not authorized for user with Id: {request.UserId} Envelope ID: {request.Id} UUID: {request.Uuid}");
|
|
|
|
if (envelope.EnvelopeReceivers.Count == 0)
|
|
throw new NotFoundException($"Envelope has no receivers to sign ID: {request.Id} UUID: {request.Uuid}");
|
|
|
|
if(envelope.Documents.FirstOrDefault() is not Document document || document.ByteData is not byte[] byteData || byteData.Length == 0)
|
|
throw new NotFoundException($"Envelope has no documents to sign ID: {request.Id} UUID: {request.Uuid}");
|
|
|
|
Stream doc = new MemoryStream(byteData);
|
|
|
|
var eReceivers = request.ReceiverId is int receiverId
|
|
? envelope.EnvelopeReceivers.Where(e => e.ReceiverId == receiverId)
|
|
: envelope.EnvelopeReceivers;
|
|
|
|
foreach (var eReceiver in eReceivers)
|
|
{
|
|
var signedDoc = await sender.Send(new SignPdfByEnvelopeReceiverCommand()
|
|
{
|
|
EnvelopeId = eReceiver.EnvelopeId,
|
|
ReceiverId = eReceiver.ReceiverId
|
|
}, cancel);
|
|
|
|
await doc.DisposeAsync();
|
|
doc = signedDoc;
|
|
}
|
|
|
|
return doc;
|
|
}
|
|
} |