Add support for fetching document metadata separately
Introduced a new `ReadDocumentDetailsQuery` to fetch document metadata (`DocumentDetailsDto`) without the binary payload. Updated `MappingProfile` to map `Document` to `DocumentDetailsDto`. Refactored `DocumentService` to handle separate fetching of metadata and binary data, combining them into `DocumentDto`. Changed the metadata endpoint to `/api/Document/details`. Added a new `GetDocumentDetails` endpoint in `DocumentController` to return metadata without binary data. Updated `EnvelopeReceiverPageDataService` with a new method `GetDocumentMetadataOfSenderAsync` for fetching metadata. These changes improve efficiency by allowing clients to load metadata without transferring large binary data when unnecessary.
This commit is contained in:
@@ -32,6 +32,7 @@ public class MappingProfile : Profile
|
||||
.ForMember(dest => dest.EnvelopeTypeTitle,
|
||||
opt => opt.MapFrom(src => src.Type != null ? src.Type.Title : null));
|
||||
CreateMap<Document, DocumentDto>();
|
||||
CreateMap<Document, DocumentDetailsDto>();
|
||||
CreateMap<History, HistoryDto>().ForMember(dest => dest.ActionDate, opt => opt.MapFrom(src => src.ChangedWhen));
|
||||
CreateMap<History, HistoryCreateDto>().ForMember(dest => dest.ActionDate, opt => opt.MapFrom(src => src.ChangedWhen));
|
||||
CreateMap<EnvelopeReceiver, EnvelopeReceiverDto>();
|
||||
@@ -68,4 +69,4 @@ public class MappingProfile : Profile
|
||||
? new SmsResponse() { Ok = true }
|
||||
: new SmsResponse() { Ok = false, Errors = gtxRes });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
using AutoMapper;
|
||||
using AutoMapper.QueryableExtensions;
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using EnvelopeGenerator.Application.Common.Dto;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace EnvelopeGenerator.Application.Documents.Queries;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a query to read document metadata/details without binary payload.
|
||||
/// </summary>
|
||||
/// <param name="EnvelopeId">The identifier of the envelope associated with the document.</param>
|
||||
/// <param name="UserId">The unique identifier of the sending user. Optional.</param>
|
||||
public record ReadDocumentDetailsQuery(int EnvelopeId, int? UserId = null) : IRequest<DocumentDetailsDto?>;
|
||||
|
||||
/// <summary>
|
||||
/// Handles queries for reading document details without loading BYTE_DATA.
|
||||
/// </summary>
|
||||
public class ReadDocumentDetailsQueryHandler : IRequestHandler<ReadDocumentDetailsQuery, DocumentDetailsDto?>
|
||||
{
|
||||
private readonly IRepository<Document> _repo;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public ReadDocumentDetailsQueryHandler(IRepository<Document> repo, IMapper mapper)
|
||||
{
|
||||
_repo = repo;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<DocumentDetailsDto?> Handle(ReadDocumentDetailsQuery query, CancellationToken cancel)
|
||||
{
|
||||
var docQuery = _repo.Query.AsNoTracking();
|
||||
|
||||
if (query.UserId is int userId)
|
||||
docQuery = docQuery.Where(doc => doc.Envelope.UserId == userId);
|
||||
|
||||
return await docQuery
|
||||
.Where(d => d.EnvelopeId == query.EnvelopeId)
|
||||
.ProjectTo<DocumentDetailsDto>(_mapper.ConfigurationProvider)
|
||||
.FirstOrDefaultAsync(cancel);
|
||||
}
|
||||
}
|
||||
@@ -36,24 +36,41 @@ public class DocumentService(IHttpClientFactory httpClientFactory)
|
||||
|
||||
/// <summary>
|
||||
/// Fetches sender document details (including signature elements) by envelope id.
|
||||
/// Uses GET /api/Document?EnvelopeId={envelopeId}.
|
||||
/// Uses GET /api/Document/details?EnvelopeId={envelopeId}.
|
||||
/// </summary>
|
||||
public async Task<DocumentDto?> GetSenderDocumentDetailsByEnvelopeIdAsync(int envelopeId, CancellationToken cancel = default)
|
||||
{
|
||||
using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server");
|
||||
var response = await http.GetAsync($"/api/Document?EnvelopeId={envelopeId}", cancel);
|
||||
var detailsResponse = await http.GetAsync($"/api/Document/details?EnvelopeId={envelopeId}", cancel);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
if (!detailsResponse.IsSuccessStatusCode)
|
||||
{
|
||||
var statusCode = (int)response.StatusCode;
|
||||
var reasonPhrase = response.ReasonPhrase ?? "Unknown error";
|
||||
var statusCode = (int)detailsResponse.StatusCode;
|
||||
var reasonPhrase = detailsResponse.ReasonPhrase ?? "Unknown error";
|
||||
throw new HttpRequestException(
|
||||
$"Failed to load sender document details. Status: {statusCode} ({reasonPhrase})",
|
||||
null,
|
||||
response.StatusCode);
|
||||
detailsResponse.StatusCode);
|
||||
}
|
||||
|
||||
return await response.Content.ReadFromJsonAsync<DocumentDto>(_jsonOptions, cancel);
|
||||
var details = await detailsResponse.Content.ReadFromJsonAsync<DocumentDetailsDto>(_jsonOptions, cancel)
|
||||
?? throw new HttpRequestException("Failed to deserialize sender document details response.");
|
||||
|
||||
var bytesResponse = await http.GetAsync($"/api/Document?EnvelopeId={envelopeId}", cancel);
|
||||
|
||||
if (!bytesResponse.IsSuccessStatusCode)
|
||||
{
|
||||
var statusCode = (int)bytesResponse.StatusCode;
|
||||
var reasonPhrase = bytesResponse.ReasonPhrase ?? "Unknown error";
|
||||
throw new HttpRequestException(
|
||||
$"Failed to load sender document bytes. Status: {statusCode} ({reasonPhrase})",
|
||||
null,
|
||||
bytesResponse.StatusCode);
|
||||
}
|
||||
|
||||
var byteData = await bytesResponse.Content.ReadAsByteArrayAsync(cancel);
|
||||
|
||||
return new DocumentDto(byteData, details);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -31,17 +31,30 @@ public class DocumentController(IMediator mediator, IAuthorizationService authSe
|
||||
/// <param name="cancel">Cancellation token.</param>
|
||||
[HttpGet]
|
||||
[Authorize(AuthenticationSchemes = AuthScheme.Sender)]
|
||||
public async Task<IActionResult> GetDocument(CancellationToken cancel, [FromQuery] ReadDocumentQuery? query = null)
|
||||
public async Task<IActionResult> GetDocument(CancellationToken cancel, [FromQuery] ReadDocumentQuery query)
|
||||
{
|
||||
if (query is null)
|
||||
return BadRequest("Missing document query.");
|
||||
|
||||
var senderDoc = await mediator.Send(query, cancel);
|
||||
return senderDoc?.ByteData is byte[] senderDocByte
|
||||
? File(senderDocByte, "application/octet-stream")
|
||||
: NotFound("Document is empty.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns sender document metadata and element details (without binary payload) for editor loading.
|
||||
/// </summary>
|
||||
/// <param name="query">Document query with EnvelopeId.</param>
|
||||
/// <param name="cancel">Cancellation token.</param>
|
||||
[HttpGet("details")]
|
||||
[Authorize(AuthenticationSchemes = AuthScheme.Sender)]
|
||||
public async Task<IActionResult> GetDocumentDetails(CancellationToken cancel, [FromQuery] ReadDocumentDetailsQuery query)
|
||||
{
|
||||
var senderDoc = await mediator.Send(query with { UserId = User.GetId() }, cancel);
|
||||
if (senderDoc is null)
|
||||
return NotFound("Document is empty.");
|
||||
|
||||
return Ok(senderDoc);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the document for the specified envelope key.
|
||||
/// </summary>
|
||||
|
||||
@@ -57,6 +57,14 @@ public class EnvelopeReceiverPageDataService(
|
||||
return await mediator.Send(new ReadDocumentQuery(EnvelopeId: envelopeId, UserId: user.SenderId()), cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads document metadata/elements for the authenticated sender without binary payload.
|
||||
/// </summary>
|
||||
public async Task<DocumentDetailsDto?> GetDocumentMetadataOfSenderAsync(ClaimsPrincipal user, int envelopeId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await mediator.Send(new ReadDocumentDetailsQuery(EnvelopeId: envelopeId, UserId: user.SenderId()), cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the current receiver's signature placeholders.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user