45 lines
1.5 KiB
C#
Raw Normal View History

2025-08-18 00:47:37 -03:00
using System.Net.Http.Json;
using Domain.Dtos.Stock;
using Domain.Generics;
2025-08-18 01:15:14 -03:00
2025-08-18 00:47:37 -03:00
public class StockScanService : IStockScanService
{
private readonly HttpClient _http;
public StockScanService(HttpClient http)
{
_http = http;
}
2025-08-18 14:34:24 -03:00
/// <summary>
/// Envía el RAW al backend (parsea + busca) y devuelve el primer match mapeado a la UI.
/// </summary>
2025-08-18 00:47:37 -03:00
public async Task<StockItemSelectionDto?> ParseAndMatchAsync(string rawInput, int locationId)
{
2025-08-18 14:34:24 -03:00
if (string.IsNullOrWhiteSpace(rawInput))
return null;
2025-08-18 00:47:37 -03:00
2025-08-18 14:34:24 -03:00
var payload = new StockScanRawRequest(rawInput, locationId, Page: 1, PageSize: 10);
using var resp = await _http.PostAsJsonAsync("/api/lsstockscan/parse-and-search", payload);
if (!resp.IsSuccessStatusCode)
return null;
2025-08-18 00:47:37 -03:00
2025-08-18 09:28:05 -03:00
var pr = await resp.Content.ReadFromJsonAsync<PagedResult<StockItemScanResultDto>>();
var first = pr?.Items?.FirstOrDefault();
if (first is null) return null;
return new StockItemSelectionDto
{
StockItemId = first.StockItemId,
ProductId = first.ProductId,
ProductName = first.ProductName,
Batch = first.Batch ?? string.Empty,
Expiration = first.Expiration?.ToDateTime(TimeOnly.MinValue),
Quantity = first.AvailableQty,
LocationId = first.LocationId ?? locationId,
2025-08-24 02:31:12 -03:00
TraceabilityType = first.TraceabilityType,
2025-08-18 09:28:05 -03:00
Serial = first.Serial // si lo devolvés en el DTO de scan
};
2025-08-18 00:47:37 -03:00
}
}