From 5e662b598488e7cfaa29db1517a3db4f5171f175 Mon Sep 17 00:00:00 2001 From: masoodafar-web Date: Sun, 4 Jan 2026 22:38:46 +0330 Subject: [PATCH] fix: Implement product lookup for AddStock and AdjustStock methods in InventoryService --- .../Services/InventoryService.cs | 186 ++++++++++++++++-- 1 file changed, 174 insertions(+), 12 deletions(-) diff --git a/src/CMSMicroservice.WebApi/Services/InventoryService.cs b/src/CMSMicroservice.WebApi/Services/InventoryService.cs index 71a9b81..601d125 100644 --- a/src/CMSMicroservice.WebApi/Services/InventoryService.cs +++ b/src/CMSMicroservice.WebApi/Services/InventoryService.cs @@ -19,16 +19,20 @@ using CMSMicroservice.Application.StockMovementCQ.Commands.CreateStockMovement; using CMSMicroservice.Application.StockMovementCQ.Queries.GetStockMovements; using CMSMicroservice.Application.StockMovementCQ.Queries.GetStockMovementsByInventoryItem; using Google.Protobuf.WellKnownTypes; +using Grpc.Core; +using MediatR; namespace CMSMicroservice.WebApi.Services; public class InventoryService : InventoryContract.InventoryContractBase { private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS; + private readonly IMediator _mediator; - public InventoryService(IDispatchRequestToCQRS dispatchRequestToCQRS) + public InventoryService(IDispatchRequestToCQRS dispatchRequestToCQRS, IMediator mediator) { _dispatchRequestToCQRS = dispatchRequestToCQRS; + _mediator = mediator; } // ========== Warehouse Management ========== @@ -91,19 +95,112 @@ public class InventoryService : InventoryContract.InventoryContractBase } // ========== Stock Operations ========== - // Note: These operations need lookup by ProductId/ProductType which requires additional implementation - // For now, returning stub responses - actual implementation needs custom handlers - public override Task AddStock(AddStockRequest request, ServerCallContext context) + public override async Task AddStock(AddStockRequest request, ServerCallContext context) { - // TODO: Implement with product lookup - throw new RpcException(new Status(StatusCode.Unimplemented, "AddStock requires product lookup - not yet implemented")); + // Lookup InventoryItem by ProductId + ProductType + var inventoryItem = await _mediator.Send( + new GetInventoryByProductQuery + { + ProductId = request.ProductId, + ProductType = (int)request.ProductType, + WarehouseId = request.WarehouseId?.Value + }, + context.CancellationToken); + + if (inventoryItem == null) + { + throw new RpcException(new Status(StatusCode.NotFound, + $"Inventory item not found for ProductId={request.ProductId}, ProductType={request.ProductType}")); + } + + // Execute IncreaseInventoryCommand + var response = await _mediator.Send( + new IncreaseInventoryCommand + { + Id = inventoryItem.Id, + Quantity = request.Quantity, + ReferenceNumber = request.ReferenceNumber, + Note = request.Note + }, + context.CancellationToken); + + return new AddStockResponse + { + InventoryItemId = response.Id, + NewQuantity = response.NewQuantity + }; } - public override Task AdjustStock(AdjustStockRequest request, ServerCallContext context) + public override async Task AdjustStock(AdjustStockRequest request, ServerCallContext context) { - // TODO: Implement with product lookup - throw new RpcException(new Status(StatusCode.Unimplemented, "AdjustStock requires product lookup - not yet implemented")); + // Lookup InventoryItem + var inventoryItem = await _mediator.Send( + new GetInventoryByProductQuery + { + ProductId = request.ProductId, + ProductType = (int)request.ProductType, + WarehouseId = request.WarehouseId?.Value + }, + context.CancellationToken); + + if (inventoryItem == null) + { + throw new RpcException(new Status(StatusCode.NotFound, + $"Inventory item not found for ProductId={request.ProductId}, ProductType={request.ProductType}")); + } + + // Determine if increase or decrease + var difference = request.NewQuantity - inventoryItem.Quantity; + + if (difference > 0) + { + var response = await _mediator.Send( + new IncreaseInventoryCommand + { + Id = inventoryItem.Id, + Quantity = difference, + ReferenceNumber = request.ReferenceNumber, + Note = request.Reason + }, + context.CancellationToken); + + return new AdjustStockResponse + { + InventoryItemId = response.Id, + OldQuantity = inventoryItem.Quantity, + NewQuantity = response.NewQuantity + }; + } + else if (difference < 0) + { + var response = await _mediator.Send( + new ReduceInventoryCommand + { + Id = inventoryItem.Id, + Quantity = Math.Abs(difference), + FromReserved = false, + ReferenceNumber = request.ReferenceNumber, + Note = request.Reason + }, + context.CancellationToken); + + return new AdjustStockResponse + { + InventoryItemId = response.Id, + OldQuantity = inventoryItem.Quantity, + NewQuantity = response.NewQuantity + }; + } + else + { + return new AdjustStockResponse + { + InventoryItemId = inventoryItem.Id, + OldQuantity = inventoryItem.Quantity, + NewQuantity = inventoryItem.Quantity + }; + } } public override Task ReserveStock(ReserveStockRequest request, ServerCallContext context) @@ -144,10 +241,75 @@ public class InventoryService : InventoryContract.InventoryContractBase throw new RpcException(new Status(StatusCode.Unimplemented, "BulkAddStock requires product lookup - not yet implemented")); } - public override Task BulkAdjustStock(BulkAdjustStockRequest request, ServerCallContext context) + public override async Task AdjustStock(AdjustStockRequest request, ServerCallContext context) { - // TODO: Implement with product lookup - throw new RpcException(new Status(StatusCode.Unimplemented, "BulkAdjustStock requires product lookup - not yet implemented")); + // Lookup InventoryItem + var inventoryItem = await _mediator.Send( + new GetInventoryByProductQuery + { + ProductId = request.ProductId, + ProductType = (int)request.ProductType, + WarehouseId = request.WarehouseId?.Value + }, + context.CancellationToken); + + if (inventoryItem == null) + { + throw new RpcException(new Status(StatusCode.NotFound, + $"Inventory item not found for ProductId={request.ProductId}, ProductType={request.ProductType}")); + } + + // Determine if increase or decrease + var difference = request.NewQuantity - inventoryItem.Quantity; + + if (difference > 0) + { + var response = await _mediator.Send( + new IncreaseInventoryCommand + { + Id = inventoryItem.Id, + Quantity = difference, + ReferenceNumber = request.ReferenceNumber, + Note = request.Reason + }, + context.CancellationToken); + + return new AdjustStockResponse + { + InventoryItemId = response.Id, + OldQuantity = inventoryItem.Quantity, + NewQuantity = response.NewQuantity + }; + } + else if (difference < 0) + { + var response = await _mediator.Send( + new ReduceInventoryCommand + { + Id = inventoryItem.Id, + Quantity = Math.Abs(difference), + FromReserved = false, + ReferenceNumber = request.ReferenceNumber, + Note = request.Reason + }, + context.CancellationToken); + + return new AdjustStockResponse + { + InventoryItemId = response.Id, + OldQuantity = inventoryItem.Quantity, + NewQuantity = response.NewQuantity + }; + } + else + { + return new AdjustStockResponse + { + InventoryItemId = inventoryItem.Id, + OldQuantity = inventoryItem.Quantity, + NewQuantity = inventoryItem.Quantity + }; + } } // ========== Stock Movements ==========