Add low stock endpoint
This commit is contained in:
@@ -10,6 +10,7 @@ public interface IStockRepository
|
|||||||
Task<List<StockBalance>> GetBalancesAsync(Guid? locationId, LocationType? locationType, Guid? itemId, int page, int pageSize);
|
Task<List<StockBalance>> GetBalancesAsync(Guid? locationId, LocationType? locationType, Guid? itemId, int page, int pageSize);
|
||||||
Task<int> GetBalancesCountAsync(Guid? locationId, LocationType? locationType, Guid? itemId);
|
Task<int> GetBalancesCountAsync(Guid? locationId, LocationType? locationType, Guid? itemId);
|
||||||
Task<List<StockBalance>> GetBalancesByItemAsync(Guid itemId);
|
Task<List<StockBalance>> GetBalancesByItemAsync(Guid itemId);
|
||||||
|
Task<List<StockBalance>> GetLowStockAsync(Guid? locationId);
|
||||||
Task AddBalanceAsync(StockBalance balance);
|
Task AddBalanceAsync(StockBalance balance);
|
||||||
void UpdateBalance(StockBalance balance);
|
void UpdateBalance(StockBalance balance);
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,13 @@ public class StockController(StockService service, ICurrentUserService currentUs
|
|||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("low-stock")]
|
||||||
|
public async Task<IActionResult> GetLowStock([FromQuery] Guid? locationId)
|
||||||
|
{
|
||||||
|
var result = await service.GetLowStockAsync(locationId);
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
|
||||||
[HttpGet("balances/{itemId}/total")]
|
[HttpGet("balances/{itemId}/total")]
|
||||||
public async Task<IActionResult> GetItemTotal(Guid itemId)
|
public async Task<IActionResult> GetItemTotal(Guid itemId)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -21,6 +21,14 @@ public record StockTotalResponse(
|
|||||||
decimal TotalQuantity,
|
decimal TotalQuantity,
|
||||||
List<StockBalanceResponse> Breakdown);
|
List<StockBalanceResponse> Breakdown);
|
||||||
|
|
||||||
|
public record LowStockItemResponse(
|
||||||
|
Guid ItemId,
|
||||||
|
string ItemName,
|
||||||
|
Guid LocationId,
|
||||||
|
string LocationName,
|
||||||
|
decimal Quantity,
|
||||||
|
decimal ReorderLevel);
|
||||||
|
|
||||||
// Movement
|
// Movement
|
||||||
public record StockMovementResponse(
|
public record StockMovementResponse(
|
||||||
Guid Id,
|
Guid Id,
|
||||||
|
|||||||
@@ -41,6 +41,17 @@ public class StockRepository(AppDBContext db) : IStockRepository
|
|||||||
.Where(b => b.FK_ItemId == itemId)
|
.Where(b => b.FK_ItemId == itemId)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
|
public Task<List<StockBalance>> GetLowStockAsync(Guid? locationId)
|
||||||
|
{
|
||||||
|
var query = db.StockBalances
|
||||||
|
.Include(b => b.Item)
|
||||||
|
.Where(b => b.LocationType == LocationType.Warehouse
|
||||||
|
&& b.Item.ReorderLevel != null
|
||||||
|
&& b.Quantity < b.Item.ReorderLevel);
|
||||||
|
if (locationId.HasValue) query = query.Where(b => b.LocationId == locationId.Value);
|
||||||
|
return query.OrderBy(b => b.Quantity).ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
public async Task AddBalanceAsync(StockBalance balance) =>
|
public async Task AddBalanceAsync(StockBalance balance) =>
|
||||||
await db.StockBalances.AddAsync(balance);
|
await db.StockBalances.AddAsync(balance);
|
||||||
|
|
||||||
|
|||||||
@@ -87,6 +87,15 @@ public class StockService(
|
|||||||
items.Select(b => MapBalance(b, names.GetValueOrDefault(b.LocationId, "—"))).ToList());
|
items.Select(b => MapBalance(b, names.GetValueOrDefault(b.LocationId, "—"))).ToList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<List<LowStockItemResponse>> GetLowStockAsync(Guid? locationId)
|
||||||
|
{
|
||||||
|
var balances = await repo.GetLowStockAsync(locationId);
|
||||||
|
var names = await ResolveLocationNamesAsync(balances.Select(b => (b.LocationId, b.LocationType)));
|
||||||
|
return balances.Select(b => new LowStockItemResponse(
|
||||||
|
b.FK_ItemId, b.Item.Name, b.LocationId, names.GetValueOrDefault(b.LocationId, "—"),
|
||||||
|
b.Quantity, b.Item.ReorderLevel!.Value)).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<StockTotalResponse?> GetItemTotalAsync(Guid itemId)
|
public async Task<StockTotalResponse?> GetItemTotalAsync(Guid itemId)
|
||||||
{
|
{
|
||||||
var item = await itemRepo.GetByIdAsync(itemId);
|
var item = await itemRepo.GetByIdAsync(itemId);
|
||||||
|
|||||||
Reference in New Issue
Block a user