diff --git a/DoliMiddlewareApi/Controllers/DocumentController.cs b/DoliMiddlewareApi/Controllers/DocumentController.cs index 82351c8..575fced 100644 --- a/DoliMiddlewareApi/Controllers/DocumentController.cs +++ b/DoliMiddlewareApi/Controllers/DocumentController.cs @@ -9,18 +9,12 @@ namespace DoliMiddlewareApi.Controllers public class DocumentController(DocumentService documentService) : ControllerBase { - [HttpPut("invoice/{invoiceRef}/build")] - public async Task BuildInvoicePdf(string invoiceRef) + [HttpGet("invoice/{invoiceRef}/pdf")] + public async Task 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); } } diff --git a/DoliMiddlewareApi/Dtos/Dolibarr/DolibarrPdfResponse.cs b/DoliMiddlewareApi/Dtos/Dolibarr/DolibarrPdfResponse.cs new file mode 100644 index 0000000..c73379f --- /dev/null +++ b/DoliMiddlewareApi/Dtos/Dolibarr/DolibarrPdfResponse.cs @@ -0,0 +1,8 @@ +namespace DoliMiddlewareApi.Dtos.Dolibarr +{ + public class DolibarrPdfResponse + { + public string filename { get; set; } = default!; + public string content { get; set; } = default!; + } +} diff --git a/DoliMiddlewareApi/Program.cs b/DoliMiddlewareApi/Program.cs index 8e99b5c..ba25023 100644 --- a/DoliMiddlewareApi/Program.cs +++ b/DoliMiddlewareApi/Program.cs @@ -103,6 +103,8 @@ builder.Services.AddScoped(); // Servicio de negocio (clientes) builder.Services.AddScoped(); +builder.Services.AddScoped(); + // Servicio de aplicación (orquesta login + cache) builder.Services.AddScoped(); diff --git a/DoliMiddlewareApi/Services/DocumentService.cs b/DoliMiddlewareApi/Services/DocumentService.cs index 0d762e5..6eb1880 100644 --- a/DoliMiddlewareApi/Services/DocumentService.cs +++ b/DoliMiddlewareApi/Services/DocumentService.cs @@ -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 ExistsAsync(string invoiceRef) - { - var file = $"¨{invoiceRef}/{invoiceRef}.pdf"; + var result = JsonSerializer.Deserialize(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(endpoint); + var bytes = Convert.FromBase64String(result.content); - return docs.Contains(file); - - } + return (bytes, result.filename); } -} + +} \ No newline at end of file diff --git a/DoliMiddlewareApi/appsettings.json b/DoliMiddlewareApi/appsettings.json index c4da293..103fda1 100644 --- a/DoliMiddlewareApi/appsettings.json +++ b/DoliMiddlewareApi/appsettings.json @@ -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",