From 92b93e862e9476e84e752abd7df12707f20c9619 Mon Sep 17 00:00:00 2001 From: TekH Date: Fri, 29 May 2026 13:37:51 +0200 Subject: [PATCH] Add GetDocumentOfReceiver endpoint and logging support Updated DocumentController to include a new GetDocumentOfReceiver endpoint for retrieving envelope documents. Added authorization based on the Receiver policy and implemented error handling for invalid or missing EnvelopeId claims. Integrated ILogger to log errors and provide detailed diagnostics. Included necessary namespace imports to support the new functionality. --- .../Controllers/DocumentController.cs | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/EnvelopeGenerator.API/Controllers/DocumentController.cs b/EnvelopeGenerator.API/Controllers/DocumentController.cs index 5ad42022..41d43fc2 100644 --- a/EnvelopeGenerator.API/Controllers/DocumentController.cs +++ b/EnvelopeGenerator.API/Controllers/DocumentController.cs @@ -1,3 +1,4 @@ +using DigitalData.Auth.Claims; using EnvelopeGenerator.API.Controllers.Interfaces; using EnvelopeGenerator.API.Extensions; using EnvelopeGenerator.Application.Documents.Queries; @@ -5,6 +6,7 @@ using EnvelopeGenerator.Domain.Constants; using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using System.Threading.Channels; namespace EnvelopeGenerator.API.Controllers; @@ -17,7 +19,7 @@ namespace EnvelopeGenerator.API.Controllers; [Authorize] [ApiController] [Route("api/[controller]")] -public class DocumentController(IMediator mediator, IAuthorizationService authService) : ControllerBase, IAuthController +public class DocumentController(IMediator mediator, IAuthorizationService authService, ILogger logger) : ControllerBase, IAuthController { /// /// @@ -60,4 +62,33 @@ public class DocumentController(IMediator mediator, IAuthorizationService authSe return Unauthorized(); } + + /// + /// + /// + /// + /// + /// + [HttpGet("{envelopeKey}")] + [Authorize(Policy = AuthPolicy.Receiver)] + public async Task GetDocumentOfReceiver(string envelopeKey, CancellationToken cancel) + { + var envelopeIdStr = User.FindFirst(EnvelopeClaimNames.EnvelopeId)?.Value; + + if (!int.TryParse(envelopeIdStr, out int envelopeId)) + { + logger.LogError( + "Inner service error: Failed to parse Envelope ID from claims. Claim '{ClaimName}' had an invalid or missing value: '{ClaimValue}'.", + EnvelopeClaimNames.EnvelopeId, + envelopeIdStr ?? "null"); + + return StatusCode(StatusCodes.Status500InternalServerError, "Inner service error: Invalid envelope claim."); + } + + var senderDoc = await mediator.Send(new ReadDocumentQuery() { EnvelopeId = envelopeId }, cancel); + + return senderDoc.ByteData is byte[] senderDocByte + ? File(senderDocByte, "application/octet-stream") + : NotFound("Document is empty."); + } } \ No newline at end of file