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
|
||||
{
|
||||
|
||||
[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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
builder.Services.AddScoped<ClientService>();
|
||||
|
||||
builder.Services.AddScoped<DocumentService>();
|
||||
|
||||
// Servicio de aplicación (orquesta login + cache)
|
||||
builder.Services.AddScoped<AuthApplicationService>();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,34 +1,30 @@
|
|||
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<(byte[] content, string filename)> BuildInvoicePdfAsync(string invoiceRef)
|
||||
{
|
||||
|
||||
public async Task BuildInvoicePdfAsync(string invoiceRef)
|
||||
var request = new BuildDocumentRequest
|
||||
{
|
||||
var request = new BuildDocumentRequest
|
||||
{
|
||||
modulepart = "invoice",
|
||||
original_file = $"{invoiceRef}/{invoiceRef}.pdf",
|
||||
doctemplate = "crabe",
|
||||
langcode = "es_ES"
|
||||
};
|
||||
modulepart = "invoice",
|
||||
original_file = $"{invoiceRef}/{invoiceRef}.pdf",
|
||||
doctemplate = "crabe",
|
||||
langcode = "es_ES"
|
||||
};
|
||||
|
||||
await apiClient.PutAsync("documents/builddoc",request);
|
||||
}
|
||||
var response = await apiClient.PutAsync("documents/builddoc", request);
|
||||
|
||||
public async Task<bool> ExistsAsync(string invoiceRef)
|
||||
{
|
||||
var file = $"¨{invoiceRef}/{invoiceRef}.pdf";
|
||||
var result = JsonSerializer.Deserialize<DolibarrPdfResponse>(response);
|
||||
|
||||
var endpoint = $"documents?modulepart=invoice&id={invoiceRef}";
|
||||
if (result == null || string.IsNullOrEmpty(result.content))
|
||||
throw new Exception("Dolibarr no devolvió PDF");
|
||||
|
||||
var docs = await apiClient.GetCollectionAsync<string>(endpoint);
|
||||
var bytes = Convert.FromBase64String(result.content);
|
||||
|
||||
return docs.Contains(file);
|
||||
|
||||
}
|
||||
return (bytes, result.filename);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Reference in New Issue