Modifico el endpoint de descargas de pdf , convirtiendo el base64 a binario

This commit is contained in:
JM 2026-04-16 09:41:46 +02:00
parent 6bac2c63fb
commit 98312846fa
5 changed files with 34 additions and 34 deletions

View File

@ -9,18 +9,12 @@ namespace DoliMiddlewareApi.Controllers
public class DocumentController(DocumentService documentService) : ControllerBase
{
[HttpPut("invoice/{invoiceRef}/build")]
public async Task<IActionResult> BuildInvoicePdf(string invoiceRef)
[HttpGet("invoice/{invoiceRef}/pdf")]
public async Task<IActionResult> GetInvoicePdf(string invoiceRef)
{
await documentService.BuildInvoicePdfAsync(invoiceRef);
var (content, filename) = await documentService.BuildInvoicePdfAsync(invoiceRef);
var exists = await documentService.ExistsAsync(invoiceRef);
return Ok(new
{
generated = exists,
invoiceRef
});
return File(content, "application/pdf", filename);
}
}

View File

@ -0,0 +1,8 @@
namespace DoliMiddlewareApi.Dtos.Dolibarr
{
public class DolibarrPdfResponse
{
public string filename { get; set; } = default!;
public string content { get; set; } = default!;
}
}

View File

@ -103,6 +103,8 @@ builder.Services.AddScoped<InvoiceService>();
// Servicio de negocio (clientes)
builder.Services.AddScoped<ClientService>();
builder.Services.AddScoped<DocumentService>();
// Servicio de aplicación (orquesta login + cache)
builder.Services.AddScoped<AuthApplicationService>();

View File

@ -1,12 +1,11 @@
using DoliMiddlewareApi.Dtos.command;
using DoliMiddlewareApi.Dtos.Dolibarr;
using DoliMiddlewareApi.Services.Clients;
using System.Text.Json;
namespace DoliMiddlewareApi.Services
public class DocumentService(IDolibarrApiClient apiClient)
{
public class DocumentService(IDolibarrApiClient apiClient)
{
public async Task BuildInvoicePdfAsync(string invoiceRef)
public async Task<(byte[] content, string filename)> BuildInvoicePdfAsync(string invoiceRef)
{
var request = new BuildDocumentRequest
{
@ -16,19 +15,16 @@ namespace DoliMiddlewareApi.Services
langcode = "es_ES"
};
await apiClient.PutAsync("documents/builddoc",request);
var response = await apiClient.PutAsync("documents/builddoc", request);
var result = JsonSerializer.Deserialize<DolibarrPdfResponse>(response);
if (result == null || string.IsNullOrEmpty(result.content))
throw new Exception("Dolibarr no devolvió PDF");
var bytes = Convert.FromBase64String(result.content);
return (bytes, result.filename);
}
public async Task<bool> ExistsAsync(string invoiceRef)
{
var file = $"¨{invoiceRef}/{invoiceRef}.pdf";
var endpoint = $"documents?modulepart=invoice&id={invoiceRef}";
var docs = await apiClient.GetCollectionAsync<string>(endpoint);
return docs.Contains(file);
}
}
}

View File

@ -8,8 +8,8 @@
},
"AllowedHosts": "*",
"Dolibarr": {
"ApiUrl": "",
"ApiKey": ""
"ApiUrl": "http://localhost/api/index.php/explorer",
"ApiKey": "f5da1cef3f7edd4f9f305f950530599e18daae91"
},
"Jwt": {
"Secret": "mi-super-secreto-jwt-aqui-cambiar-en-produccion",