diff --git a/EnvelopeGenerator.API/Controllers/DocumentController.cs b/EnvelopeGenerator.API/Controllers/DocumentController.cs index 0adac3cb..39e503d7 100644 --- a/EnvelopeGenerator.API/Controllers/DocumentController.cs +++ b/EnvelopeGenerator.API/Controllers/DocumentController.cs @@ -1,6 +1,5 @@ -using DigitalData.Core.Exceptions; -using EnvelopeGenerator.Application.Common.Extensions; -using EnvelopeGenerator.Application.EnvelopeReceivers.Queries; +using EnvelopeGenerator.API.Extensions; +using EnvelopeGenerator.Application.Documents.Queries; using EnvelopeGenerator.Domain.Constants; using MediatR; using Microsoft.AspNetCore.Authorization; @@ -14,7 +13,7 @@ namespace EnvelopeGenerator.API.Controllers; /// /// Initializes a new instance of the class. /// -[Authorize(Roles = Role.FullyAuth)] +[Authorize] [ApiController] [Route("api/[controller]")] public class DocumentController(IMediator mediator, ILogger logger) : ControllerBase @@ -25,19 +24,28 @@ public class DocumentController(IMediator mediator, ILogger /// Encoded envelope key. /// Cancellation token. [HttpGet] - public async Task GetDocument(ReadEnvelopeReceiverQuery query, CancellationToken cancel) + [Authorize(Roles = Role.Sender)] + public async Task GetDocument(ReadDocumentQuery query, CancellationToken cancel) { - var envRcv = await mediator.Send(query, cancel).FirstAsync(Exceptions.NotFound); - - 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"); + var doc = await mediator.Send(query, cancel); + return doc.ByteData is byte[] docByte + ? File(docByte, "application/octet-stream") + : NotFound("Document is empty."); } -} + + /// + /// Returns the document bytes for the receiver. + /// + /// Cancellation token. + [HttpGet] + [Authorize(Roles = Role.Receiver.FullyAuth)] + public async Task GetDocument(CancellationToken cancel) + { + var envelopeId = User.GetEnvelopeIdOfReceiver(); + + var doc = await mediator.Send(new ReadDocumentQuery() { EnvelopeId = envelopeId }, cancel); + return doc.ByteData is byte[] docByte + ? File(docByte, "application/octet-stream") + : NotFound("Document is empty."); + } +} \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Documents/Queries/ReadDocumentQuery.cs b/EnvelopeGenerator.Application/Documents/Queries/ReadDocumentQuery.cs index dd579622..34c7fd7a 100644 --- a/EnvelopeGenerator.Application/Documents/Queries/ReadDocumentQuery.cs +++ b/EnvelopeGenerator.Application/Documents/Queries/ReadDocumentQuery.cs @@ -4,6 +4,7 @@ using EnvelopeGenerator.Domain.Entities; using Microsoft.EntityFrameworkCore; using AutoMapper; using EnvelopeGenerator.Application.Common.Dto; +using DigitalData.Core.Exceptions; namespace EnvelopeGenerator.Application.Documents.Queries; @@ -12,14 +13,14 @@ namespace EnvelopeGenerator.Application.Documents.Queries; /// /// The unique identifier of the document. Optional. /// The identifier of the envelope associated with the document. Optional. -public record ReadDocumentQuery(int? Id = null, int? EnvelopeId = null) : IRequest +public record ReadDocumentQuery(int? Id = null, int? EnvelopeId = null) : IRequest { } /// /// Handles queries for reading data based on either the document ID or the envelope ID. /// -public class ReadDocumentQueryHandler : IRequestHandler +public class ReadDocumentQueryHandler : IRequestHandler { /// /// TempRepo for accessing entities. @@ -50,20 +51,19 @@ public class ReadDocumentQueryHandler : IRequestHandler /// Thrown when neither nor is provided. /// - public async Task Handle(ReadDocumentQuery query, CancellationToken cancel) + public async Task Handle(ReadDocumentQuery query, CancellationToken cancel) { if (query.Id is not null) { - var doc = await _repo.ReadOnly().Where(d => d.Id == query.Id).FirstOrDefaultAsync(cancel); + var doc = await _repo.Query.Where(d => d.Id == query.Id).FirstOrDefaultAsync(cancel); return _mapper.Map(doc); } else if (query.EnvelopeId is not null) { - var doc = await _repo.ReadOnly().Where(d => d.EnvelopeId == query.EnvelopeId).FirstOrDefaultAsync(cancel); + var doc = await _repo.Query.Where(d => d.EnvelopeId == query.EnvelopeId).FirstOrDefaultAsync(cancel); return _mapper.Map(doc); } - throw new InvalidOperationException( - $"Invalid {nameof(ReadDocumentQuery)}: either {nameof(query.Id)} or {nameof(query.EnvelopeId)} must be provided."); + throw new NotFoundException(); } } \ No newline at end of file