86 lines
3.1 KiB
C#
86 lines
3.1 KiB
C#
using DoliMiddlewareApi.Dtos.command;
|
|
using DoliMiddlewareApi.Dtos.Dolibarr;
|
|
using DoliMiddlewareApi.Exceptions;
|
|
using DoliMiddlewareApi.Services.Clients;
|
|
using System.Text.Json;
|
|
|
|
namespace DoliMiddlewareApi.Services;
|
|
|
|
public class DocumentService(IDolibarrApiClient apiClient)
|
|
{
|
|
public async Task<(byte[] content, string filename)> BuildInvoicePdfAsync(string invoiceRef)
|
|
{
|
|
var request = new BuildDocumentRequest
|
|
{
|
|
modulepart = "invoice",
|
|
original_file = $"{invoiceRef}/{invoiceRef}.pdf",
|
|
doctemplate = "crabe",
|
|
langcode = "es_ES"
|
|
};
|
|
|
|
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<List<DocumentItem>> GetDocumentsAsync(string modulePart, string refOrSocid)
|
|
{
|
|
var endpoint = $"documents?modulepart={modulePart}&ref={Uri.EscapeDataString(refOrSocid)}";
|
|
var responseString = await apiClient.GetAsyncRaw(endpoint);
|
|
|
|
var response = JsonSerializer.Deserialize<List<Dictionary<string, JsonElement>>>(responseString);
|
|
|
|
if (response == null)
|
|
return new List<DocumentItem>();
|
|
|
|
return response.Select(d =>
|
|
{
|
|
var name = d.TryGetValue("name", out var nameProp) && nameProp.ValueKind == JsonValueKind.String
|
|
? nameProp.GetString() : null;
|
|
var path = d.TryGetValue("path", out var pathProp) && pathProp.ValueKind == JsonValueKind.String
|
|
? pathProp.GetString() : null;
|
|
var size = d.TryGetValue("size", out var sizeProp) && sizeProp.ValueKind == JsonValueKind.Number
|
|
? sizeProp.GetInt64() : 0;
|
|
|
|
return new DocumentItem
|
|
{
|
|
Name = name,
|
|
Path = path,
|
|
Size = size
|
|
};
|
|
}).ToList();
|
|
}
|
|
|
|
public async Task<(byte[] content, string filename, string contentType)> DownloadDocumentAsync(string modulePart, string fileRef)
|
|
{
|
|
var endpoint = $"documents/download?modulepart={modulePart}&file={Uri.EscapeDataString(fileRef)}";
|
|
var responseString = await apiClient.GetAsyncRaw(endpoint);
|
|
|
|
var response = JsonSerializer.Deserialize<DolibarrPdfResponse>(responseString);
|
|
|
|
if (response == null || string.IsNullOrEmpty(response.content))
|
|
throw new NotFoundException("Documento no encontrado");
|
|
|
|
var bytes = Convert.FromBase64String(response.content);
|
|
var filename = response.filename ?? "document";
|
|
var contentType = filename.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase)
|
|
? "application/pdf"
|
|
: "application/octet-stream";
|
|
|
|
return (bytes, filename, contentType);
|
|
}
|
|
}
|
|
|
|
public class DocumentItem
|
|
{
|
|
public string? Name { get; set; }
|
|
public string? Path { get; set; }
|
|
public long Size { get; set; }
|
|
} |