fix: las facturas ahora devuelven tmb el nombre del cliente.

This commit is contained in:
javiermengual 2026-01-30 16:52:28 +01:00
parent e729388981
commit c674ac66d9
2 changed files with 25 additions and 5 deletions

View File

@ -10,7 +10,5 @@ public class InvoiceDto
public decimal? Total { get; set; }
public decimal? RemainToPay { get; set; }
public string Status { get; set; }
public string? ClientName { get; set; }
}

View File

@ -38,7 +38,20 @@ public class InvoiceService(IDolibarrApiClient apiClient)
}
var dataList = await apiClient.GetCollectionAsync<InvoiceResponse>(endpoint);
return dataList.Select(InvoiceMapper.MapToInvoiceDto).ToList();
var invoices = dataList.Select(InvoiceMapper.MapToInvoiceDto).ToList();
if (invoices.Count == 0)
return invoices;
var clientIds = invoices.Select(i => i.ClientId).Distinct().ToList();
var clientNames = await GetClientNamesAsync(clientIds);
foreach (var invoice in invoices)
{
invoice.ClientName = clientNames.GetValueOrDefault(invoice.ClientId);
}
return invoices;
}
public async Task<int> CreateInvoiceAsync(CreateInvoiceDto dto)
@ -102,4 +115,13 @@ public class InvoiceService(IDolibarrApiClient apiClient)
await apiClient.PutAsync($"invoices/{id}", current);
}
private async Task<Dictionary<int, string>> GetClientNamesAsync(List<int> clientIds)
{
var uniqueIds = clientIds.Distinct().ToList();
var tasks = uniqueIds.Select(id => apiClient.GetResourceAsync<ClientResponse>($"thirdparties/{id}"));
var clients = await Task.WhenAll(tasks);
return clients.Where(c => c is { id: not null })
.ToDictionary(c => int.Parse(c!.id!), c => c!.name ?? "");
}
}