refactor(EnvelopeController): migrate envelope update to MediatR with annotations

- Renamed `Update` action to `CreateOrUpdate`.
- Replaced manual signing logic with `_mediator.SignDocAsync`.
- Added `ExpandoObject` parameter to handle document annotations.
- Improved authorization checks and logging for missing claims.
- Kept legacy `Reject` endpoint intact with obsolete services.
This commit is contained in:
tekh 2025-08-25 16:40:46 +02:00
parent 00bdfeb9bb
commit 0939e57c56
3 changed files with 18 additions and 25 deletions

View File

@ -7,10 +7,13 @@ using EnvelopeGenerator.Extensions;
using EnvelopeGenerator.Application.Interfaces.Services; using EnvelopeGenerator.Application.Interfaces.Services;
using static EnvelopeGenerator.Domain.Constants; using static EnvelopeGenerator.Domain.Constants;
using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Domain.Entities;
using EnvelopeGenerator.Domain;
using DigitalData.Core.Abstraction.Application.DTO; using DigitalData.Core.Abstraction.Application.DTO;
using EnvelopeGenerator.Web.Extensions; using EnvelopeGenerator.Web.Extensions;
using MediatR; using MediatR;
using System.Dynamic;
using EnvelopeGenerator.Application.EnvelopeReceivers.Queries;
using EnvelopeGenerator.Application.DocStatus.Commands;
using Newtonsoft.Json;
namespace EnvelopeGenerator.Web.Controllers; namespace EnvelopeGenerator.Web.Controllers;
@ -47,34 +50,25 @@ public class EnvelopeController : BaseController
[Authorize(Roles = ReceiverRole.FullyAuth)] [Authorize(Roles = ReceiverRole.FullyAuth)]
[HttpPost("{envelopeKey}")] [HttpPost("{envelopeKey}")]
[Obsolete("Use MediatR")] [Obsolete("Use MediatR")]
public async Task<IActionResult> Update(string envelopeKey, int index) public async Task<IActionResult> CreateOrUpdate([FromRoute] string envelopeKey, int index, [FromBody] ExpandoObject annotations, CancellationToken cancel = default)
{ {
envelopeKey = _urlEncoder.Encode(envelopeKey); // get claims
var signature = User.GetAuthReceiverSignature();
var uuid = User.GetAuthEnvelopeUuid();
var authSignature = User.GetAuthReceiverSignature(); if (signature is null || uuid is null)
if (authSignature != envelopeKey.GetReceiverSignature())
return Unauthorized();
EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey);
// Again check if receiver has already signed
if (envelopeService.ReceiverAlreadySigned(response.Envelope, response.Receiver.Id) == true)
{ {
return Problem(statusCode: 403); _logger.LogError("Authorization failed: authenticated user does not have a valid signature or envelope UUID.");
return Unauthorized("User authentication is incomplete. Missing required claims for processing this request.");
} }
var document = envelopeService.GetDocument(index, envelopeKey); // Again check if receiver has already signed
if (await _mediator.IsSignedAsync(uuid, signature, cancel))
return Problem(statusCode: 403);
string? annotationData = await envelopeService.EnsureValidAnnotationData(Request); await _mediator.SignDocAsync(uuid, signature, JsonConvert.SerializeObject(annotations), cancel);
envelopeService.InsertDocumentStatus(new Domain.Entities.DocumentStatus() EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey);
{
EnvelopeId = response.Envelope.Id,
ReceiverId = response.Receiver.Id,
Value = annotationData,
Status = Constants.DocumentStatus.Signed
});
var signResult = actionService?.SignEnvelope(response.Envelope, ReceiverVM.From(response)); var signResult = actionService?.SignEnvelope(response.Envelope, ReceiverVM.From(response));

View File

@ -277,7 +277,6 @@ class App {
try { try {
const json = await iJSON const json = await iJSON
const postEnvelopeResult = await this.Network.postEnvelope( const postEnvelopeResult = await this.Network.postEnvelope(
this.envelopeKey,
this.currentDocument.id, this.currentDocument.id,
json json
) )

View File

@ -15,8 +15,8 @@
* @param {any} documentId * @param {any} documentId
* @param {any} json * @param {any} json
*/ */
async postEnvelope(envelopeKey, documentId, json) { async postEnvelope(documentId, json) {
return this.postRequest(`/api/envelope/${envelopeKey}?index=${documentId}`, json) return this.postRequest(`/api/envelope?index=${documentId}`, json)
.then(this.wrapJsonResponse.bind(this)) .then(this.wrapJsonResponse.bind(this))
} }