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:
2026-10-02 10:56:47 +02:00
parent f558d03bdf
commit ec79d7622e
5 changed files with 95 additions and 12 deletions

View File

@@ -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>

View File

@@ -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>

View File

@@ -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>