Files
EnvelopeGenerator/EnvelopeGenerator.Web/Controllers/DocumentController.cs
TekH 6b23dcdba7 Refactor: unify role constants under new Role class
Replaced all usages of ReceiverRole with the new Role class in EnvelopeGenerator.Domain.Constants. Removed ReceiverRole.cs and added Role.cs with PreAuth and FullyAuth constants. Updated all [Authorize] attributes and role checks in controllers and authentication logic to use Role.FullyAuth and Role.PreAuth. This centralizes role management for improved maintainability and clarity.
2026-02-02 11:53:26 +01:00

42 lines
1.4 KiB
C#

using DigitalData.Core.Exceptions;
using EnvelopeGenerator.Application.Common.Extensions;
using EnvelopeGenerator.Application.EnvelopeReceivers.Queries;
using EnvelopeGenerator.Domain.Constants;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace EnvelopeGenerator.Web.Controllers;
[Authorize(Roles = Role.FullyAuth)]
[ApiController]
[Route("api/[controller]")]
public class DocumentController : ControllerBase
{
private readonly IMediator _mediator;
private readonly ILogger<DocumentController> _logger;
public DocumentController(IMediator mediator, ILogger<DocumentController> logger)
{
_mediator = mediator;
_logger = logger;
}
[HttpGet("{envelopeKey}")]
public async Task<IActionResult> GetDocument([FromRoute] string envelopeKey, CancellationToken cancel)
{
var envRcv = await _mediator.ReadEnvelopeReceiverAsync(envelopeKey, cancel) ?? throw new NotFoundException("Envelope Receiver is not found.");
var byteData = envRcv.Envelope?.Documents?.FirstOrDefault()?.ByteData;
if(byteData is null || byteData.Length == 0)
{
_logger.LogError("Document byte data is null or empty for envelope-receiver entity:\n{envelopeKey}.",
envRcv.ToJson(Format.Json.ForDiagnostics));
throw new NotFoundException("Document is empty.");
}
return File(byteData, "application/octet-stream");
}
}