From a1fd7c6418955b6e7db1076c1a3ffb3b03d1903e Mon Sep 17 00:00:00 2001 From: javiermengual Date: Sat, 3 Jan 2026 20:46:13 +0100 Subject: [PATCH] =?UTF-8?q?Arreglar=20bug=20de=20validaci=C3=B3n=20de=20es?= =?UTF-8?q?tado=20y=20limpiar=20c=C3=B3digo=20de=20cache?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Añadir método ConvertStatusToDolibarr en InvoiceMapper para mapear todos los estados correctamente (draft, unpaid, paid, cancelled) - Mover lógica de mapeo de estado desde InvoiceService a InvoiceMapper para seguir principios de SRP - Arreglar bug donde el estado 'paid' y 'cancelled' se mapeaban incorrectamente a '0' (draft) - Añadir validación de estado con mensaje de error claro para valores inválidos - Limpiar código de DolibarrTokenCacheService eliminando bloque anidado innecesario --- DoliMiddlewareApi/Mappers/InvoiceMapper.cs | 13 +++++++++++++ .../Services/Auth/DolibarrTokenCacheService.cs | 14 ++++++-------- DoliMiddlewareApi/Services/InvoiceService.cs | 5 ++--- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/DoliMiddlewareApi/Mappers/InvoiceMapper.cs b/DoliMiddlewareApi/Mappers/InvoiceMapper.cs index 88486e9..67b4547 100644 --- a/DoliMiddlewareApi/Mappers/InvoiceMapper.cs +++ b/DoliMiddlewareApi/Mappers/InvoiceMapper.cs @@ -1,6 +1,7 @@ using System.Globalization; using DoliMiddlewareApi.Dtos; using DoliMiddlewareApi.Dtos.Dolibarr; +using DoliMiddlewareApi.Exceptions; namespace DoliMiddlewareApi.Mappers; @@ -93,4 +94,16 @@ public static class InvoiceMapper _ => "unknown" }; } + + public static string ConvertStatusToDolibarr(string status) + { + return status.ToLower() switch + { + "draft" => "0", + "unpaid" => "1", + "paid" => "2", + "cancelled" => "3", + _ => throw new BadRequestException($"Estado inválido: {status}. Valores válidos: draft, unpaid, paid, cancelled") + }; + } } \ No newline at end of file diff --git a/DoliMiddlewareApi/Services/Auth/DolibarrTokenCacheService.cs b/DoliMiddlewareApi/Services/Auth/DolibarrTokenCacheService.cs index aa0829b..06797f7 100644 --- a/DoliMiddlewareApi/Services/Auth/DolibarrTokenCacheService.cs +++ b/DoliMiddlewareApi/Services/Auth/DolibarrTokenCacheService.cs @@ -1,24 +1,22 @@ -using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Caching.Memory; namespace DoliMiddlewareApi.Services.Auth; public class DolibarrTokenCacheService(IHttpContextAccessor httpContextAccessor, IMemoryCache cache) { - public string? GetDolibarrToken() { var context = httpContextAccessor.HttpContext; + + var sessionIdClaim = context?.User.Claims.FirstOrDefault(c => c.Type == "sessionId"); + if (sessionIdClaim != null && cache.TryGetValue(sessionIdClaim.Value, out string? dolibarrToken)) { - var sessionIdClaim = context?.User.Claims.FirstOrDefault(c => c.Type == "sessionId"); - if (sessionIdClaim != null && cache.TryGetValue(sessionIdClaim.Value, out string? dolibarrToken)) - { - return dolibarrToken; - } + return dolibarrToken; } + return null; } - + public void SetDolibarrToken(string sessionId, string dolibarrToken, TimeSpan expiration) { cache.Set(sessionId, dolibarrToken, expiration); diff --git a/DoliMiddlewareApi/Services/InvoiceService.cs b/DoliMiddlewareApi/Services/InvoiceService.cs index cd8aa3c..d907283 100644 --- a/DoliMiddlewareApi/Services/InvoiceService.cs +++ b/DoliMiddlewareApi/Services/InvoiceService.cs @@ -39,7 +39,7 @@ public class InvoiceService(IDolibarrApiClient apiClient) { socid = dto.ClientId.ToString(), type = "0", - statut = dto.Status == "unpaid" ? "1" : "0", + statut = InvoiceMapper.ConvertStatusToDolibarr(dto.Status), date = ((DateTimeOffset)dto.Date).ToUnixTimeSeconds().ToString(), date_lim_reglement = dto.ExpireDate.HasValue ? ((DateTimeOffset)dto.ExpireDate.Value).ToUnixTimeSeconds().ToString() @@ -87,12 +87,11 @@ public class InvoiceService(IDolibarrApiClient apiClient) if (dto.Number != null) current.@ref = dto.Number; if (dto.NotePublic != null) current.note_public = dto.NotePublic; if (dto.NotePrivate != null) current.note_private = dto.NotePrivate; - current.statut = dto.Status == "unpaid" ? "1" : "0"; + current.statut = InvoiceMapper.ConvertStatusToDolibarr(dto.Status); if (dto.ExpireDate.HasValue) current.date_lim_reglement = ((DateTimeOffset)dto.ExpireDate.Value).ToUnixTimeSeconds(); // No tocar: date, socid, lines (Dolibarr no los cambia en PUT) await apiClient.PutAsync($"invoices/{id}", current); } - } \ No newline at end of file