From f6704eaa99031ad798e981582d4ece7ae7e98446 Mon Sep 17 00:00:00 2001 From: masoodafar-web Date: Mon, 5 Jan 2026 00:58:52 +0330 Subject: [PATCH] fix: Implement product lookup in RecordLoss method to accurately reduce inventory on loss --- .../Services/InventoryService.cs | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/CMSMicroservice.WebApi/Services/InventoryService.cs b/src/CMSMicroservice.WebApi/Services/InventoryService.cs index e26b0ca..72f206f 100644 --- a/src/CMSMicroservice.WebApi/Services/InventoryService.cs +++ b/src/CMSMicroservice.WebApi/Services/InventoryService.cs @@ -222,10 +222,35 @@ public class InventoryService : InventoryContract.InventoryContractBase throw new RpcException(new Status(StatusCode.Unimplemented, "ProcessReturn requires product lookup - not yet implemented")); } - public override Task RecordLoss(RecordLossRequest request, ServerCallContext context) + public override async Task RecordLoss(RecordLossRequest request, ServerCallContext context) { - // TODO: Implement with product lookup - throw new RpcException(new Status(StatusCode.Unimplemented, "RecordLoss requires product lookup - not yet implemented")); + // Lookup InventoryItem by ProductId + ProductType + var inventoryItem = await _mediator.Send( + new GetInventoryByProductQuery + { + ProductId = request.ProductId, + ProductType = (Domain.Enums.ProductType)request.ProductType + }, + context.CancellationToken); + + if (inventoryItem == null) + { + throw new RpcException(new Status(StatusCode.NotFound, + $"Inventory item not found for ProductId={request.ProductId}, ProductType={request.ProductType}")); + } + + // Execute ReduceInventoryCommand to record the loss + await _mediator.Send( + new ReduceInventoryCommand + { + Id = inventoryItem.Id, + Quantity = request.Quantity, + FromReserved = false, + ReferenceNumber = request.ReferenceNumber ?? $"LOSS-{DateTime.UtcNow:yyyyMMddHHmmss}" + }, + context.CancellationToken); + + return new Empty(); } // ========== Bulk Operations ==========