Add envelope finalization command and signed-envelope query
This commit is contained in:
@@ -0,0 +1,62 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
|
using EnvelopeGenerator.Application.Common.Dto;
|
||||||
|
using EnvelopeGenerator.Domain.Entities;
|
||||||
|
using MediatR;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace EnvelopeGenerator.Application.Envelopes.Queries;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public record ReadSignedEnvelopeQuery : IRequest<IEnumerable<EnvelopeDto>>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public bool? Burned { get; init; } = null;
|
||||||
|
|
||||||
|
internal int? UserId { get; private set; } = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public ReadSignedEnvelopeQuery Authorized(int userId)
|
||||||
|
{
|
||||||
|
UserId = userId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Repo"></param>
|
||||||
|
/// <param name="Mapper"></param>
|
||||||
|
public class ReadSignedEnvelopeQueryHandler(IRepository<Envelope> Repo, IMapper Mapper) : IRequestHandler<ReadSignedEnvelopeQuery, IEnumerable<EnvelopeDto>>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancel"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="NotImplementedException"></exception>
|
||||||
|
public async Task<IEnumerable<EnvelopeDto>> Handle(ReadSignedEnvelopeQuery request, CancellationToken cancel)
|
||||||
|
{
|
||||||
|
var eQuery = Repo.Query.Where(e => e.Status == Domain.Constants.EnvelopeStatus.EnvelopeCompletelySigned);
|
||||||
|
|
||||||
|
if (request.Burned is bool burned)
|
||||||
|
eQuery = burned ? eQuery.Where(e => e.DocResult != null) : eQuery.Where(e => e.DocResult == null);
|
||||||
|
|
||||||
|
if(request.UserId is int userId)
|
||||||
|
eQuery = eQuery.Where(e => e.UserId == userId);
|
||||||
|
|
||||||
|
var envelopes = await eQuery.ToListAsync(cancel);
|
||||||
|
|
||||||
|
return Mapper.Map<IEnumerable<EnvelopeDto>>(envelopes);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
|
using DigitalData.Core.Exceptions;
|
||||||
|
using EnvelopeGenerator.Application.Common.Interfaces.Services.Notification;
|
||||||
|
using EnvelopeGenerator.Application.Common.Query;
|
||||||
|
using EnvelopeGenerator.Domain.Constants;
|
||||||
|
using EnvelopeGenerator.Domain.Entities;
|
||||||
|
using EnvelopeGenerator.Domain.Interfaces;
|
||||||
|
using MediatR;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace EnvelopeGenerator.Application.PdfSigning;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public record FinalizeEnvelopeCommand : EnvelopeQueryBase, IRequest
|
||||||
|
{
|
||||||
|
internal int? UserId { get; private set; } = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public FinalizeEnvelopeCommand Authorized(int userId)
|
||||||
|
{
|
||||||
|
UserId = userId;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public bool BurnedAllowed { get; init; } = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public bool SaveDocResult { get; init; } = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Repo"></param>
|
||||||
|
/// <param name="Sender"></param>
|
||||||
|
public class FinalizeEnvelopeCommandHandler(IRepository<Envelope> Repo, ISender Sender, IEnvelopeNotificationService NtfServer) : IRequestHandler<FinalizeEnvelopeCommand>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancel"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="NotFoundException"></exception>
|
||||||
|
public async Task Handle(FinalizeEnvelopeCommand 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).ThenInclude(er => er.Receiver)
|
||||||
|
.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 InvalidOperationException($"Envelope is not owned by user ID: {request.Id} UUID: {request.Uuid}");
|
||||||
|
|
||||||
|
// already burned
|
||||||
|
if (!request.BurnedAllowed && envelope.IsBurned())
|
||||||
|
throw new InvalidOperationException($"Envelope is already burned ID: {request.Id} UUID: {request.Uuid}");
|
||||||
|
|
||||||
|
// envelope is not completely signed
|
||||||
|
if (envelope.Status!= EnvelopeStatus.EnvelopeCompletelySigned)
|
||||||
|
throw new InvalidOperationException($"Envelope status is not completely signed ID: {request.Id} UUID: {request.Uuid}");
|
||||||
|
|
||||||
|
using var docStream = await Sender.Send(new SignPdfByEnvelopeCommand()
|
||||||
|
{
|
||||||
|
Envelope = envelope
|
||||||
|
}, cancel);
|
||||||
|
|
||||||
|
if (docStream.CanSeek)
|
||||||
|
docStream.Position = 0;
|
||||||
|
|
||||||
|
using var ms = new MemoryStream();
|
||||||
|
docStream.CopyTo(ms);
|
||||||
|
byte[] doc = ms.ToArray();
|
||||||
|
|
||||||
|
foreach (var er in envelope.EnvelopeReceivers)
|
||||||
|
{
|
||||||
|
await NtfServer.Notify(er.Receiver.EmailAddress, new DocumentSignedNotification()
|
||||||
|
{
|
||||||
|
DocumentTitle = envelope.Title,
|
||||||
|
NameReceiver = er.Name,
|
||||||
|
Attachment = doc
|
||||||
|
}, cancel);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.SaveDocResult)
|
||||||
|
await Repo.UpdateAsync(e => e.DocResult = doc, e => e.Id == envelope.Id, cancel);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,6 +29,8 @@ public record SignPdfByEnvelopeCommand : EnvelopeQueryBase, IRequest<Stream>
|
|||||||
UserId = userId;
|
UserId = userId;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal Envelope? Envelope { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -45,15 +47,20 @@ public class SignPdfByEnvelopeCommandHandler(IRepository<Envelope> Repo, ISender
|
|||||||
/// <exception cref="NotImplementedException"></exception>
|
/// <exception cref="NotImplementedException"></exception>
|
||||||
public async Task<Stream> Handle(SignPdfByEnvelopeCommand request, CancellationToken cancel)
|
public async Task<Stream> Handle(SignPdfByEnvelopeCommand request, CancellationToken cancel)
|
||||||
{
|
{
|
||||||
var envelopeQuery = Repo.Query;
|
if (request.Envelope is null)
|
||||||
|
{
|
||||||
|
var envelopeQuery = Repo.Query;
|
||||||
|
|
||||||
if (request.Id is int id)
|
if (request.Id is int id)
|
||||||
envelopeQuery = envelopeQuery.Where(e => e.Id == id);
|
envelopeQuery = envelopeQuery.Where(e => e.Id == id);
|
||||||
else if (request.Uuid is string uuid)
|
else if (request.Uuid is string uuid)
|
||||||
envelopeQuery = envelopeQuery.Where(e => e.Uuid == uuid);
|
envelopeQuery = envelopeQuery.Where(e => e.Uuid == uuid);
|
||||||
|
|
||||||
var envelope = await envelopeQuery.Include(e => e.EnvelopeReceivers).Include(e => e.Documents).SingleOrDefaultAsync(cancel)
|
request.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}");
|
?? throw new NotFoundException($"Envelope is not found ID: {request.Id} UUID: {request.Uuid}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var envelope = request.Envelope;
|
||||||
|
|
||||||
if (request.UserId is int userId && userId != envelope.UserId)
|
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}");
|
throw new AuthorizationException($"Envelope is not authorized for user with Id: {request.UserId} Envelope ID: {request.Id} UUID: {request.Uuid}");
|
||||||
|
|||||||
Reference in New Issue
Block a user