From 70c4b15a23fbfaded56d367a3ace5ff3e73cc9be Mon Sep 17 00:00:00 2001 From: masoodafar-web Date: Sun, 4 Jan 2026 22:58:25 +0330 Subject: [PATCH] fix: Refactor AdjustStock method to streamline inventory updates and improve response structure --- .../Services/InventoryService.cs | 110 +++--------------- 1 file changed, 17 insertions(+), 93 deletions(-) diff --git a/src/CMSMicroservice.WebApi/Services/InventoryService.cs b/src/CMSMicroservice.WebApi/Services/InventoryService.cs index 601d125..e26b0ca 100644 --- a/src/CMSMicroservice.WebApi/Services/InventoryService.cs +++ b/src/CMSMicroservice.WebApi/Services/InventoryService.cs @@ -103,8 +103,7 @@ public class InventoryService : InventoryContract.InventoryContractBase new GetInventoryByProductQuery { ProductId = request.ProductId, - ProductType = (int)request.ProductType, - WarehouseId = request.WarehouseId?.Value + ProductType = (Domain.Enums.ProductType)request.ProductType }, context.CancellationToken); @@ -120,14 +119,13 @@ public class InventoryService : InventoryContract.InventoryContractBase { Id = inventoryItem.Id, Quantity = request.Quantity, - ReferenceNumber = request.ReferenceNumber, - Note = request.Note + ReferenceNumber = request.ReferenceNumber }, context.CancellationToken); return new AddStockResponse { - InventoryItemId = response.Id, + InventoryItemId = inventoryItem.Id, NewQuantity = response.NewQuantity }; } @@ -139,8 +137,7 @@ public class InventoryService : InventoryContract.InventoryContractBase new GetInventoryByProductQuery { ProductId = request.ProductId, - ProductType = (int)request.ProductType, - WarehouseId = request.WarehouseId?.Value + ProductType = (Domain.Enums.ProductType)request.ProductType }, context.CancellationToken); @@ -155,50 +152,48 @@ public class InventoryService : InventoryContract.InventoryContractBase if (difference > 0) { - var response = await _mediator.Send( + await _mediator.Send( new IncreaseInventoryCommand { Id = inventoryItem.Id, Quantity = difference, - ReferenceNumber = request.ReferenceNumber, - Note = request.Reason + ReferenceNumber = request.ReferenceNumber }, context.CancellationToken); return new AdjustStockResponse { - InventoryItemId = response.Id, - OldQuantity = inventoryItem.Quantity, - NewQuantity = response.NewQuantity + PreviousQuantity = inventoryItem.Quantity, + NewQuantity = request.NewQuantity, + Difference = difference }; } else if (difference < 0) { - var response = await _mediator.Send( + await _mediator.Send( new ReduceInventoryCommand { Id = inventoryItem.Id, Quantity = Math.Abs(difference), FromReserved = false, - ReferenceNumber = request.ReferenceNumber, - Note = request.Reason + ReferenceNumber = request.ReferenceNumber }, context.CancellationToken); return new AdjustStockResponse { - InventoryItemId = response.Id, - OldQuantity = inventoryItem.Quantity, - NewQuantity = response.NewQuantity + PreviousQuantity = inventoryItem.Quantity, + NewQuantity = request.NewQuantity, + Difference = difference }; } else { return new AdjustStockResponse { - InventoryItemId = inventoryItem.Id, - OldQuantity = inventoryItem.Quantity, - NewQuantity = inventoryItem.Quantity + PreviousQuantity = inventoryItem.Quantity, + NewQuantity = inventoryItem.Quantity, + Difference = 0 }; } } @@ -241,77 +236,6 @@ public class InventoryService : InventoryContract.InventoryContractBase throw new RpcException(new Status(StatusCode.Unimplemented, "BulkAddStock requires product lookup - not yet implemented")); } - public override async Task AdjustStock(AdjustStockRequest request, ServerCallContext context) - { - // 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 ========== public override async Task GetStockMovements(GetStockMovementsRequest request, ServerCallContext context)