fix: arreglo bug porque dolibar devuelve un objeto anidado
This commit is contained in:
parent
283a9e7f5f
commit
9a06cc945e
|
|
@ -2,5 +2,13 @@ namespace DoliMiddlewareApi.Dtos.Dolibarr;
|
||||||
|
|
||||||
public class TokenResponse
|
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; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -22,11 +22,11 @@ public sealed class DolibarrAuthService(IDolibarrApiClient apiClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
var response = JsonSerializer.Deserialize<TokenResponse>(responseString);
|
var response = JsonSerializer.Deserialize<TokenResponse>(responseString);
|
||||||
if (response == null || string.IsNullOrEmpty(response.AccessToken))
|
if (response == null || string.IsNullOrEmpty(response.success.token))
|
||||||
{
|
{
|
||||||
throw new UnauthorizedException("Respuesta inválida de Dolibarr");
|
throw new UnauthorizedException("Respuesta inválida de Dolibarr");
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.AccessToken;
|
return response.success.token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -9,9 +9,8 @@ public class DolibarrTokenCacheService(IHttpContextAccessor httpContextAccessor,
|
||||||
public string? GetDolibarrToken()
|
public string? GetDolibarrToken()
|
||||||
{
|
{
|
||||||
var context = httpContextAccessor.HttpContext;
|
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))
|
if (sessionIdClaim != null && cache.TryGetValue(sessionIdClaim.Value, out string? dolibarrToken))
|
||||||
{
|
{
|
||||||
return dolibarrToken;
|
return dolibarrToken;
|
||||||
|
|
|
||||||
|
|
@ -4,24 +4,14 @@ using DoliMiddlewareApi.Services.Auth;
|
||||||
|
|
||||||
namespace DoliMiddlewareApi.Services.Clients;
|
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<T> GetResourceAsync<T>(string endpoint) where T : class
|
public async Task<T> GetResourceAsync<T>(string endpoint) where T : class
|
||||||
{
|
{
|
||||||
var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
|
var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
|
||||||
AddDolibarrTokenHeader(request);
|
AddDolibarrTokenHeader(request);
|
||||||
var response = await _httpClient.SendAsync(request);
|
var response = await httpClient.SendAsync(request);
|
||||||
await EnsureSuccessOrThrowAsync(response, endpoint);
|
await EnsureSuccessOrThrowAsync(response, endpoint);
|
||||||
|
|
||||||
return await response.Content.ReadFromJsonAsync<T>()
|
return await response.Content.ReadFromJsonAsync<T>()
|
||||||
|
|
@ -32,7 +22,7 @@ public class DolibarrApiClient : IDolibarrApiClient
|
||||||
{
|
{
|
||||||
var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
|
var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
|
||||||
AddDolibarrTokenHeader(request);
|
AddDolibarrTokenHeader(request);
|
||||||
var response = await _httpClient.SendAsync(request);
|
var response = await httpClient.SendAsync(request);
|
||||||
await EnsureSuccessOrThrowAsync(response, endpoint);
|
await EnsureSuccessOrThrowAsync(response, endpoint);
|
||||||
|
|
||||||
return await response.Content.ReadFromJsonAsync<List<T>>()
|
return await response.Content.ReadFromJsonAsync<List<T>>()
|
||||||
|
|
@ -47,7 +37,7 @@ public class DolibarrApiClient : IDolibarrApiClient
|
||||||
Content = JsonContent.Create(requestBody)
|
Content = JsonContent.Create(requestBody)
|
||||||
};
|
};
|
||||||
AddDolibarrTokenHeader(request);
|
AddDolibarrTokenHeader(request);
|
||||||
var response = await _httpClient.SendAsync(request);
|
var response = await httpClient.SendAsync(request);
|
||||||
await EnsureSuccessOrThrowAsync(response, endpoint);
|
await EnsureSuccessOrThrowAsync(response, endpoint);
|
||||||
|
|
||||||
// porque devuelve el id como response
|
// porque devuelve el id como response
|
||||||
|
|
@ -61,7 +51,7 @@ public class DolibarrApiClient : IDolibarrApiClient
|
||||||
Content = JsonContent.Create(requestBody)
|
Content = JsonContent.Create(requestBody)
|
||||||
};
|
};
|
||||||
AddDolibarrTokenHeader(request);
|
AddDolibarrTokenHeader(request);
|
||||||
var response = await _httpClient.SendAsync(request);
|
var response = await httpClient.SendAsync(request);
|
||||||
await EnsureSuccessOrThrowAsync(response, endpoint);
|
await EnsureSuccessOrThrowAsync(response, endpoint);
|
||||||
|
|
||||||
return await response.Content.ReadAsStringAsync();
|
return await response.Content.ReadAsStringAsync();
|
||||||
|
|
@ -69,7 +59,7 @@ public class DolibarrApiClient : IDolibarrApiClient
|
||||||
|
|
||||||
private void AddDolibarrTokenHeader(HttpRequestMessage request)
|
private void AddDolibarrTokenHeader(HttpRequestMessage request)
|
||||||
{
|
{
|
||||||
var dolibarrToken = _tokenCacheService.GetDolibarrToken();
|
var dolibarrToken = tokenCacheService.GetDolibarrToken();
|
||||||
if (!string.IsNullOrEmpty(dolibarrToken))
|
if (!string.IsNullOrEmpty(dolibarrToken))
|
||||||
{
|
{
|
||||||
request.Headers.Add("DOLAPIKEY", dolibarrToken);
|
request.Headers.Add("DOLAPIKEY", dolibarrToken);
|
||||||
|
|
|
||||||
|
|
@ -8,18 +8,11 @@ using DoliMiddlewareApi.Services.Clients;
|
||||||
|
|
||||||
namespace DoliMiddlewareApi.Services;
|
namespace DoliMiddlewareApi.Services;
|
||||||
|
|
||||||
public class InvoiceService
|
public class InvoiceService(IDolibarrApiClient apiClient)
|
||||||
{
|
{
|
||||||
private readonly IDolibarrApiClient _apiClient;
|
|
||||||
|
|
||||||
public InvoiceService(IDolibarrApiClient apiClient)
|
|
||||||
{
|
|
||||||
_apiClient = apiClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<InvoiceDetailDto> GetInvoiceAsync(int id)
|
public async Task<InvoiceDetailDto> GetInvoiceAsync(int id)
|
||||||
{
|
{
|
||||||
var data = await _apiClient.GetResourceAsync<InvoiceDetailResponse>($"invoices/{id}");
|
var data = await apiClient.GetResourceAsync<InvoiceDetailResponse>($"invoices/{id}");
|
||||||
return InvoiceMapper.MapToInvoiceDetailDto(data);
|
return InvoiceMapper.MapToInvoiceDetailDto(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,7 +29,7 @@ public class InvoiceService
|
||||||
endpoint += $"&status={status}";
|
endpoint += $"&status={status}";
|
||||||
}
|
}
|
||||||
|
|
||||||
var dataList = await _apiClient.GetCollectionAsync<InvoiceResponse>(endpoint);
|
var dataList = await apiClient.GetCollectionAsync<InvoiceResponse>(endpoint);
|
||||||
return dataList.Select(InvoiceMapper.MapToInvoiceDto).ToList();
|
return dataList.Select(InvoiceMapper.MapToInvoiceDto).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,14 +56,14 @@ public class InvoiceService
|
||||||
}).ToArray()
|
}).ToArray()
|
||||||
};
|
};
|
||||||
|
|
||||||
var responseBody = await _apiClient.PostAsync("invoices", requestBody);
|
var responseBody = await apiClient.PostAsync("invoices", requestBody);
|
||||||
|
|
||||||
return int.Parse(responseBody);
|
return int.Parse(responseBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> AddInvoiceLineAsync(int invoiceId, CreateInvoiceLineDto lineDto)
|
public async Task<string> AddInvoiceLineAsync(int invoiceId, CreateInvoiceLineDto lineDto)
|
||||||
{
|
{
|
||||||
var invoice = await _apiClient.GetResourceAsync<InvoiceDetailResponse>($"invoices/{invoiceId}");
|
var invoice = await apiClient.GetResourceAsync<InvoiceDetailResponse>($"invoices/{invoiceId}");
|
||||||
if (invoice.statut != "0") throw new ForbiddenException("Solo se pueden añadir líneas a facturas en borrador");
|
if (invoice.statut != "0") throw new ForbiddenException("Solo se pueden añadir líneas a facturas en borrador");
|
||||||
|
|
||||||
var requestBody = new
|
var requestBody = new
|
||||||
|
|
@ -81,13 +74,13 @@ public class InvoiceService
|
||||||
tva_tx = lineDto.TaxRate.ToString(CultureInfo.InvariantCulture)
|
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)
|
public async Task UpdateInvoiceAsync(int id, UpdateInvoiceDto dto)
|
||||||
{
|
{
|
||||||
// GET el JSON completo
|
// GET el JSON completo
|
||||||
var current = await _apiClient.GetResourceAsync<InvoiceDetailResponse>($"invoices/{id}");
|
var current = await apiClient.GetResourceAsync<InvoiceDetailResponse>($"invoices/{id}");
|
||||||
if (current.statut != "0") throw new ForbiddenException("Solo drafts");
|
if (current.statut != "0") throw new ForbiddenException("Solo drafts");
|
||||||
|
|
||||||
// Modifica solo campos que Dolibarr permite en PUT
|
// 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)
|
// 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