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
|
new GetInventoryByProductQuery
|
||||||
{
|
{
|
||||||
ProductId = request.ProductId,
|
ProductId = request.ProductId,
|
||||||
ProductType = (int)request.ProductType,
|
ProductType = (Domain.Enums.ProductType)request.ProductType
|
||||||
WarehouseId = request.WarehouseId?.Value
|
|
||||||
},
|
},
|
||||||
context.CancellationToken);
|
context.CancellationToken);
|
||||||
|
|
||||||
@@ -120,14 +119,13 @@ public class InventoryService : InventoryContract.InventoryContractBase
|
|||||||
{
|
{
|
||||||
Id = inventoryItem.Id,
|
Id = inventoryItem.Id,
|
||||||
Quantity = request.Quantity,
|
Quantity = request.Quantity,
|
||||||
ReferenceNumber = request.ReferenceNumber,
|
ReferenceNumber = request.ReferenceNumber
|
||||||
Note = request.Note
|
|
||||||
},
|
},
|
||||||
context.CancellationToken);
|
context.CancellationToken);
|
||||||
|
|
||||||
return new AddStockResponse
|
return new AddStockResponse
|
||||||
{
|
{
|
||||||
InventoryItemId = response.Id,
|
InventoryItemId = inventoryItem.Id,
|
||||||
NewQuantity = response.NewQuantity
|
NewQuantity = response.NewQuantity
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -139,8 +137,7 @@ public class InventoryService : InventoryContract.InventoryContractBase
|
|||||||
new GetInventoryByProductQuery
|
new GetInventoryByProductQuery
|
||||||
{
|
{
|
||||||
ProductId = request.ProductId,
|
ProductId = request.ProductId,
|
||||||
ProductType = (int)request.ProductType,
|
ProductType = (Domain.Enums.ProductType)request.ProductType
|
||||||
WarehouseId = request.WarehouseId?.Value
|
|
||||||
},
|
},
|
||||||
context.CancellationToken);
|
context.CancellationToken);
|
||||||
|
|
||||||
@@ -155,50 +152,48 @@ public class InventoryService : InventoryContract.InventoryContractBase
|
|||||||
|
|
||||||
if (difference > 0)
|
if (difference > 0)
|
||||||
{
|
{
|
||||||
var response = await _mediator.Send(
|
await _mediator.Send(
|
||||||
new IncreaseInventoryCommand
|
new IncreaseInventoryCommand
|
||||||
{
|
{
|
||||||
Id = inventoryItem.Id,
|
Id = inventoryItem.Id,
|
||||||
Quantity = difference,
|
Quantity = difference,
|
||||||
ReferenceNumber = request.ReferenceNumber,
|
ReferenceNumber = request.ReferenceNumber
|
||||||
Note = request.Reason
|
|
||||||
},
|
},
|
||||||
context.CancellationToken);
|
context.CancellationToken);
|
||||||
|
|
||||||
return new AdjustStockResponse
|
return new AdjustStockResponse
|
||||||
{
|
{
|
||||||
InventoryItemId = response.Id,
|
PreviousQuantity = inventoryItem.Quantity,
|
||||||
OldQuantity = inventoryItem.Quantity,
|
NewQuantity = request.NewQuantity,
|
||||||
NewQuantity = response.NewQuantity
|
Difference = difference
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
else if (difference < 0)
|
else if (difference < 0)
|
||||||
{
|
{
|
||||||
var response = await _mediator.Send(
|
await _mediator.Send(
|
||||||
new ReduceInventoryCommand
|
new ReduceInventoryCommand
|
||||||
{
|
{
|
||||||
Id = inventoryItem.Id,
|
Id = inventoryItem.Id,
|
||||||
Quantity = Math.Abs(difference),
|
Quantity = Math.Abs(difference),
|
||||||
FromReserved = false,
|
FromReserved = false,
|
||||||
ReferenceNumber = request.ReferenceNumber,
|
ReferenceNumber = request.ReferenceNumber
|
||||||
Note = request.Reason
|
|
||||||
},
|
},
|
||||||
context.CancellationToken);
|
context.CancellationToken);
|
||||||
|
|
||||||
return new AdjustStockResponse
|
return new AdjustStockResponse
|
||||||
{
|
{
|
||||||
InventoryItemId = response.Id,
|
PreviousQuantity = inventoryItem.Quantity,
|
||||||
OldQuantity = inventoryItem.Quantity,
|
NewQuantity = request.NewQuantity,
|
||||||
NewQuantity = response.NewQuantity
|
Difference = difference
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return new AdjustStockResponse
|
return new AdjustStockResponse
|
||||||
{
|
{
|
||||||
InventoryItemId = inventoryItem.Id,
|
PreviousQuantity = inventoryItem.Quantity,
|
||||||
OldQuantity = inventoryItem.Quantity,
|
NewQuantity = 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"));
|
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 ==========
|
// ========== Stock Movements ==========
|
||||||
|
|
||||||
public override async Task<GetStockMovementsResponse> GetStockMovements(GetStockMovementsRequest request, ServerCallContext context)
|
public override async Task<GetStockMovementsResponse> GetStockMovements(GetStockMovementsRequest request, ServerCallContext context)
|
||||||
|
|||||||
Reference in New Issue
Block a user