Arreglar bug de validación de estado y limpiar código de cache
- 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
This commit is contained in:
parent
9a06cc945e
commit
a1fd7c6418
|
|
@ -1,6 +1,7 @@
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using DoliMiddlewareApi.Dtos;
|
using DoliMiddlewareApi.Dtos;
|
||||||
using DoliMiddlewareApi.Dtos.Dolibarr;
|
using DoliMiddlewareApi.Dtos.Dolibarr;
|
||||||
|
using DoliMiddlewareApi.Exceptions;
|
||||||
|
|
||||||
namespace DoliMiddlewareApi.Mappers;
|
namespace DoliMiddlewareApi.Mappers;
|
||||||
|
|
||||||
|
|
@ -93,4 +94,16 @@ public static class InvoiceMapper
|
||||||
_ => "unknown"
|
_ => "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")
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,24 +1,22 @@
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
|
||||||
namespace DoliMiddlewareApi.Services.Auth;
|
namespace DoliMiddlewareApi.Services.Auth;
|
||||||
|
|
||||||
public class DolibarrTokenCacheService(IHttpContextAccessor httpContextAccessor, IMemoryCache cache)
|
public class DolibarrTokenCacheService(IHttpContextAccessor httpContextAccessor, IMemoryCache cache)
|
||||||
{
|
{
|
||||||
|
|
||||||
public string? GetDolibarrToken()
|
public string? GetDolibarrToken()
|
||||||
{
|
{
|
||||||
var context = httpContextAccessor.HttpContext;
|
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");
|
return dolibarrToken;
|
||||||
if (sessionIdClaim != null && cache.TryGetValue(sessionIdClaim.Value, out string? dolibarrToken))
|
|
||||||
{
|
|
||||||
return dolibarrToken;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetDolibarrToken(string sessionId, string dolibarrToken, TimeSpan expiration)
|
public void SetDolibarrToken(string sessionId, string dolibarrToken, TimeSpan expiration)
|
||||||
{
|
{
|
||||||
cache.Set(sessionId, dolibarrToken, expiration);
|
cache.Set(sessionId, dolibarrToken, expiration);
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ public class InvoiceService(IDolibarrApiClient apiClient)
|
||||||
{
|
{
|
||||||
socid = dto.ClientId.ToString(),
|
socid = dto.ClientId.ToString(),
|
||||||
type = "0",
|
type = "0",
|
||||||
statut = dto.Status == "unpaid" ? "1" : "0",
|
statut = InvoiceMapper.ConvertStatusToDolibarr(dto.Status),
|
||||||
date = ((DateTimeOffset)dto.Date).ToUnixTimeSeconds().ToString(),
|
date = ((DateTimeOffset)dto.Date).ToUnixTimeSeconds().ToString(),
|
||||||
date_lim_reglement = dto.ExpireDate.HasValue
|
date_lim_reglement = dto.ExpireDate.HasValue
|
||||||
? ((DateTimeOffset)dto.ExpireDate.Value).ToUnixTimeSeconds().ToString()
|
? ((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.Number != null) current.@ref = dto.Number;
|
||||||
if (dto.NotePublic != null) current.note_public = dto.NotePublic;
|
if (dto.NotePublic != null) current.note_public = dto.NotePublic;
|
||||||
if (dto.NotePrivate != null) current.note_private = dto.NotePrivate;
|
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();
|
if (dto.ExpireDate.HasValue) current.date_lim_reglement = ((DateTimeOffset)dto.ExpireDate.Value).ToUnixTimeSeconds();
|
||||||
|
|
||||||
// No tocar: date, socid, lines (Dolibarr no los cambia en PUT)
|
// No tocar: date, socid, lines (Dolibarr no los cambia en PUT)
|
||||||
|
|
||||||
await apiClient.PutAsync($"invoices/{id}", current);
|
await apiClient.PutAsync($"invoices/{id}", current);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue