Modifico el endpoint de descargas de pdf , convirtiendo el base64 a binario
This commit is contained in:
parent
6bac2c63fb
commit
98312846fa
|
|
@ -9,18 +9,12 @@ namespace DoliMiddlewareApi.Controllers
|
||||||
public class DocumentController(DocumentService documentService) : ControllerBase
|
public class DocumentController(DocumentService documentService) : ControllerBase
|
||||||
{
|
{
|
||||||
|
|
||||||
[HttpPut("invoice/{invoiceRef}/build")]
|
[HttpGet("invoice/{invoiceRef}/pdf")]
|
||||||
public async Task<IActionResult> BuildInvoicePdf(string invoiceRef)
|
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 File(content, "application/pdf", filename);
|
||||||
|
|
||||||
return Ok(new
|
|
||||||
{
|
|
||||||
generated = exists,
|
|
||||||
invoiceRef
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
namespace DoliMiddlewareApi.Dtos.Dolibarr
|
||||||
|
{
|
||||||
|
public class DolibarrPdfResponse
|
||||||
|
{
|
||||||
|
public string filename { get; set; } = default!;
|
||||||
|
public string content { get; set; } = default!;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -103,6 +103,8 @@ builder.Services.AddScoped<InvoiceService>();
|
||||||
// Servicio de negocio (clientes)
|
// Servicio de negocio (clientes)
|
||||||
builder.Services.AddScoped<ClientService>();
|
builder.Services.AddScoped<ClientService>();
|
||||||
|
|
||||||
|
builder.Services.AddScoped<DocumentService>();
|
||||||
|
|
||||||
// Servicio de aplicación (orquesta login + cache)
|
// Servicio de aplicación (orquesta login + cache)
|
||||||
builder.Services.AddScoped<AuthApplicationService>();
|
builder.Services.AddScoped<AuthApplicationService>();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,11 @@
|
||||||
using DoliMiddlewareApi.Dtos.command;
|
using DoliMiddlewareApi.Dtos.command;
|
||||||
|
using DoliMiddlewareApi.Dtos.Dolibarr;
|
||||||
using DoliMiddlewareApi.Services.Clients;
|
using DoliMiddlewareApi.Services.Clients;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace DoliMiddlewareApi.Services
|
public class DocumentService(IDolibarrApiClient apiClient)
|
||||||
{
|
{
|
||||||
public class DocumentService(IDolibarrApiClient apiClient)
|
public async Task<(byte[] content, string filename)> BuildInvoicePdfAsync(string invoiceRef)
|
||||||
{
|
|
||||||
|
|
||||||
public async Task BuildInvoicePdfAsync(string invoiceRef)
|
|
||||||
{
|
{
|
||||||
var request = new BuildDocumentRequest
|
var request = new BuildDocumentRequest
|
||||||
{
|
{
|
||||||
|
|
@ -16,19 +15,16 @@ namespace DoliMiddlewareApi.Services
|
||||||
langcode = "es_ES"
|
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);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
@ -8,8 +8,8 @@
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"Dolibarr": {
|
"Dolibarr": {
|
||||||
"ApiUrl": "",
|
"ApiUrl": "http://localhost/api/index.php/explorer",
|
||||||
"ApiKey": ""
|
"ApiKey": "f5da1cef3f7edd4f9f305f950530599e18daae91"
|
||||||
},
|
},
|
||||||
"Jwt": {
|
"Jwt": {
|
||||||
"Secret": "mi-super-secreto-jwt-aqui-cambiar-en-produccion",
|
"Secret": "mi-super-secreto-jwt-aqui-cambiar-en-produccion",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue