-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make AvailabilityValidator check inventory per StockLocation instead of per Shipment #1693
Conversation
@@ -54,39 +54,85 @@ module Stock | |||
context "line_item is part of a shipment" do | |||
let!(:order) { create(:order_with_line_items) } | |||
|
|||
context "has stock in all stock locations" do | |||
context "has stock in one stock locations" do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: "location"
This is really neat. However, I see some commits do not pass CI. :( |
57210f6
to
f0777d4
Compare
I believe that was just the second to last commit which I've rebased and amended the last commit fixing the spec failure. I'm running a |
We can use ActiveRecord to build queries instead of a hash, which allows us to reuse StockLocation.active and makes it a little more readable.
These specs exibit two issues when a line item is split across multiple shipments. If the line item is split across shipments, any availabilty errors will be reported twice. If a shipment is split across shipments with the same stock location, it won't detect insufficient stock if both shipments are able to separately fulfill the inventory.
Previously stock was checked per-shipment which could lead to incorrect results as the same inventory from one stock location could be double-counted across multiple shipments. This also greatly reduces the number of queries, by replacing one Shipment load per inventory_unit with a single query to get the quantity of inventory units per stock location.
We can avoid selecting StockLocation records by just passing their id to Stock::Quantifier
Instead of relying on the behaviour of stock_location_quantities and the entire order state machine, we should just create shipments and inventory units in the state we expect. This adds tests for the line item needing some stock from each stock location. This adds one skipped test which fails because the error on the line item is added twice.
We shouldn't report the same error twice on the same item.
f0777d4
to
a2e4ad6
Compare
This fixes a few issues with
AvailabilityValidator
Stock was being checked per Shipment rather than per StockLocation. This meant that available inventory could be double counted towards the same order. Quantities is now checked per StockLocation.
If multiple Shipments (or now StockLocations) on one LineItem had insufficient inventory, the same error would be added multiple times to the line item. ex.
["Quantity selected of \"Product #20 - 5904 (Size: S)\" is not available.", "Quantity selected of \"Product #20 - 5904 (Size: S)\" is not available."]
One Shipment record was being queried and loaded for each inventory attached to the line item, which is quite slow and unnecessary. This has been replaced with a single query per LineItem.
Before:
After: