fix: Refactor AdjustStock method to streamline inventory updates and improve response structure
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 2m18s
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 2m18s
This commit is contained in:
@@ -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<AdjustStockResponse> 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<GetStockMovementsResponse> GetStockMovements(GetStockMovementsRequest request, ServerCallContext context)
|
||||
|
||||
Reference in New Issue
Block a user