From 9a06cc945e77b47d8172af043fd3722dfc612ca2 Mon Sep 17 00:00:00 2001 From: javiermengual Date: Sat, 3 Jan 2026 20:31:53 +0100 Subject: [PATCH] fix: arreglo bug porque dolibar devuelve un objeto anidado --- .../Dtos/Dolibarr/TokenResponse.cs | 10 +++++++- .../Services/Auth/DolibarrAuthService.cs | 4 ++-- .../Auth/DolibarrTokenCacheService.cs | 3 +-- .../Services/Clients/DolibarrApiClient.cs | 24 ++++++------------- DoliMiddlewareApi/Services/InvoiceService.cs | 23 +++++++----------- 5 files changed, 27 insertions(+), 37 deletions(-) diff --git a/DoliMiddlewareApi/Dtos/Dolibarr/TokenResponse.cs b/DoliMiddlewareApi/Dtos/Dolibarr/TokenResponse.cs index 813e9d9..db25d17 100644 --- a/DoliMiddlewareApi/Dtos/Dolibarr/TokenResponse.cs +++ b/DoliMiddlewareApi/Dtos/Dolibarr/TokenResponse.cs @@ -2,5 +2,13 @@ namespace DoliMiddlewareApi.Dtos.Dolibarr; public class TokenResponse { - public required string AccessToken { get; set; } + public required SuccessData success { get; set; } + + public class SuccessData + { + public int code { get; set; } + public required string token { get; set; } + public string? entity { get; set; } + public string? message { get; set; } + } } \ No newline at end of file diff --git a/DoliMiddlewareApi/Services/Auth/DolibarrAuthService.cs b/DoliMiddlewareApi/Services/Auth/DolibarrAuthService.cs index b55d16b..27a2f24 100644 --- a/DoliMiddlewareApi/Services/Auth/DolibarrAuthService.cs +++ b/DoliMiddlewareApi/Services/Auth/DolibarrAuthService.cs @@ -22,11 +22,11 @@ public sealed class DolibarrAuthService(IDolibarrApiClient apiClient) } var response = JsonSerializer.Deserialize(responseString); - if (response == null || string.IsNullOrEmpty(response.AccessToken)) + if (response == null || string.IsNullOrEmpty(response.success.token)) { throw new UnauthorizedException("Respuesta inválida de Dolibarr"); } - return response.AccessToken; + return response.success.token; } } \ No newline at end of file diff --git a/DoliMiddlewareApi/Services/Auth/DolibarrTokenCacheService.cs b/DoliMiddlewareApi/Services/Auth/DolibarrTokenCacheService.cs index 530c1bd..aa0829b 100644 --- a/DoliMiddlewareApi/Services/Auth/DolibarrTokenCacheService.cs +++ b/DoliMiddlewareApi/Services/Auth/DolibarrTokenCacheService.cs @@ -9,9 +9,8 @@ public class DolibarrTokenCacheService(IHttpContextAccessor httpContextAccessor, public string? GetDolibarrToken() { var context = httpContextAccessor.HttpContext; - if (context?.User.Identity?.IsAuthenticated == true) { - var sessionIdClaim = context.User.Claims.FirstOrDefault(c => c.Type == "sessionId"); + var sessionIdClaim = context?.User.Claims.FirstOrDefault(c => c.Type == "sessionId"); if (sessionIdClaim != null && cache.TryGetValue(sessionIdClaim.Value, out string? dolibarrToken)) { return dolibarrToken; diff --git a/DoliMiddlewareApi/Services/Clients/DolibarrApiClient.cs b/DoliMiddlewareApi/Services/Clients/DolibarrApiClient.cs index ba7f756..c0c8ed7 100644 --- a/DoliMiddlewareApi/Services/Clients/DolibarrApiClient.cs +++ b/DoliMiddlewareApi/Services/Clients/DolibarrApiClient.cs @@ -4,24 +4,14 @@ using DoliMiddlewareApi.Services.Auth; namespace DoliMiddlewareApi.Services.Clients; -public class DolibarrApiClient : IDolibarrApiClient +public class DolibarrApiClient(HttpClient httpClient, DolibarrTokenCacheService tokenCacheService) + : IDolibarrApiClient { - private readonly HttpClient _httpClient; - private readonly DolibarrTokenCacheService _tokenCacheService; - - public DolibarrApiClient(HttpClient httpClient, DolibarrTokenCacheService tokenCacheService) - { - _httpClient = httpClient; - _tokenCacheService = tokenCacheService; - } - - - // ===== MÉTODOS GENÉRICOS ===== public async Task GetResourceAsync(string endpoint) where T : class { var request = new HttpRequestMessage(HttpMethod.Get, endpoint); AddDolibarrTokenHeader(request); - var response = await _httpClient.SendAsync(request); + var response = await httpClient.SendAsync(request); await EnsureSuccessOrThrowAsync(response, endpoint); return await response.Content.ReadFromJsonAsync() @@ -32,7 +22,7 @@ public class DolibarrApiClient : IDolibarrApiClient { var request = new HttpRequestMessage(HttpMethod.Get, endpoint); AddDolibarrTokenHeader(request); - var response = await _httpClient.SendAsync(request); + var response = await httpClient.SendAsync(request); await EnsureSuccessOrThrowAsync(response, endpoint); return await response.Content.ReadFromJsonAsync>() @@ -47,7 +37,7 @@ public class DolibarrApiClient : IDolibarrApiClient Content = JsonContent.Create(requestBody) }; AddDolibarrTokenHeader(request); - var response = await _httpClient.SendAsync(request); + var response = await httpClient.SendAsync(request); await EnsureSuccessOrThrowAsync(response, endpoint); // porque devuelve el id como response @@ -61,7 +51,7 @@ public class DolibarrApiClient : IDolibarrApiClient Content = JsonContent.Create(requestBody) }; AddDolibarrTokenHeader(request); - var response = await _httpClient.SendAsync(request); + var response = await httpClient.SendAsync(request); await EnsureSuccessOrThrowAsync(response, endpoint); return await response.Content.ReadAsStringAsync(); @@ -69,7 +59,7 @@ public class DolibarrApiClient : IDolibarrApiClient private void AddDolibarrTokenHeader(HttpRequestMessage request) { - var dolibarrToken = _tokenCacheService.GetDolibarrToken(); + var dolibarrToken = tokenCacheService.GetDolibarrToken(); if (!string.IsNullOrEmpty(dolibarrToken)) { request.Headers.Add("DOLAPIKEY", dolibarrToken); diff --git a/DoliMiddlewareApi/Services/InvoiceService.cs b/DoliMiddlewareApi/Services/InvoiceService.cs index 26a2aa9..cd8aa3c 100644 --- a/DoliMiddlewareApi/Services/InvoiceService.cs +++ b/DoliMiddlewareApi/Services/InvoiceService.cs @@ -8,18 +8,11 @@ using DoliMiddlewareApi.Services.Clients; namespace DoliMiddlewareApi.Services; -public class InvoiceService +public class InvoiceService(IDolibarrApiClient apiClient) { - private readonly IDolibarrApiClient _apiClient; - - public InvoiceService(IDolibarrApiClient apiClient) - { - _apiClient = apiClient; - } - public async Task GetInvoiceAsync(int id) { - var data = await _apiClient.GetResourceAsync($"invoices/{id}"); + var data = await apiClient.GetResourceAsync($"invoices/{id}"); return InvoiceMapper.MapToInvoiceDetailDto(data); } @@ -36,7 +29,7 @@ public class InvoiceService endpoint += $"&status={status}"; } - var dataList = await _apiClient.GetCollectionAsync(endpoint); + var dataList = await apiClient.GetCollectionAsync(endpoint); return dataList.Select(InvoiceMapper.MapToInvoiceDto).ToList(); } @@ -63,14 +56,14 @@ public class InvoiceService }).ToArray() }; - var responseBody = await _apiClient.PostAsync("invoices", requestBody); + var responseBody = await apiClient.PostAsync("invoices", requestBody); return int.Parse(responseBody); } public async Task AddInvoiceLineAsync(int invoiceId, CreateInvoiceLineDto lineDto) { - var invoice = await _apiClient.GetResourceAsync($"invoices/{invoiceId}"); + var invoice = await apiClient.GetResourceAsync($"invoices/{invoiceId}"); if (invoice.statut != "0") throw new ForbiddenException("Solo se pueden añadir líneas a facturas en borrador"); var requestBody = new @@ -81,13 +74,13 @@ public class InvoiceService tva_tx = lineDto.TaxRate.ToString(CultureInfo.InvariantCulture) }; - return await _apiClient.PostAsync($"invoices/{invoiceId}/lines", requestBody); + return await apiClient.PostAsync($"invoices/{invoiceId}/lines", requestBody); } public async Task UpdateInvoiceAsync(int id, UpdateInvoiceDto dto) { // GET el JSON completo - var current = await _apiClient.GetResourceAsync($"invoices/{id}"); + var current = await apiClient.GetResourceAsync($"invoices/{id}"); if (current.statut != "0") throw new ForbiddenException("Solo drafts"); // Modifica solo campos que Dolibarr permite en PUT @@ -99,7 +92,7 @@ public class InvoiceService // No tocar: date, socid, lines (Dolibarr no los cambia en PUT) - await _apiClient.PutAsync($"invoices/{id}", current); + await apiClient.PutAsync($"invoices/{id}", current); } } \ No newline at end of file