-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
WIP: Introduce new middleware stock coordinator #6484
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
Draft
sofiabesenski4
wants to merge
11
commits into
solidusio:main
Choose a base branch
from
SuperGoodSoft:introduce-new-middleware-stock-coordinator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b67737d
Make SimpleCoordinator inventory_units keyword arg
sofiabesenski4 8b98fa7
Port SimpleCoordinator code into new Coordinator
sofiabesenski4 dbb8066
Update to use the @ instance variable syntax
sofiabesenski4 b762d99
Introduce InventoryUnit Stock middleware
sofiabesenski4 f861dcd
Delegate InventoryUnit grouping to middleware
sofiabesenski4 7b98c26
Delegate Stock StockLocation logic to middleware
sofiabesenski4 a4a3a6d
Delegate Desired logic to middleware
sofiabesenski4 c6f14a4
Delegate Availability logic to middleware
sofiabesenski4 2feb441
Delegate Allocate logic to middleware
sofiabesenski4 26e9893
Delegate Package logic to middleware
sofiabesenski4 ca9ae0d
Delegate Shipment logic to middleware
sofiabesenski4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Spree | ||
| module Stock | ||
| class Coordinator | ||
| def initialize(order, inventory_units: nil) | ||
| @context = {order:, inventory_units:} | ||
| @order = order | ||
|
|
||
| Middleware::InventoryUnit.new.call(@context) | ||
| Middleware::InventoryUnitGroup.new.call(@context) | ||
| Middleware::StockLocation.new.call(@context) | ||
| Middleware::Desired.new.call(@context) | ||
| Middleware::Availability.new.call(@context) | ||
| end | ||
|
|
||
| def shipments | ||
| @shipments ||= begin | ||
| Middleware::Allocate.new.call(@context) | ||
| Middleware::Package.new.call(@context) | ||
| Middleware::Shipment.new.call(@context) | ||
|
|
||
| shipments = @context[:shipments] | ||
|
|
||
| # Make sure we don't add the proposed shipments to the order | ||
| @order.shipments = @order.shipments - shipments | ||
|
|
||
| shipments | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| module Spree | ||
| module Stock | ||
| module Middleware | ||
| class Allocate | ||
| def call(context) | ||
| allocator = Spree::Config.stock.allocator_class.new(context[:availability]) | ||
| on_hand_packages, backordered_packages, leftover = allocator.allocate_inventory(context[:desired]) | ||
|
|
||
| raise Spree::Order::InsufficientStock.new(items: leftover.quantities) unless leftover.empty? | ||
|
|
||
| context[:on_hand_packages] = on_hand_packages | ||
| context[:backordered_packages] = backordered_packages | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| module Spree | ||
| module Stock | ||
| module Middleware | ||
| class Availability | ||
| def call(context) | ||
| context[:availability] = Spree::Stock::Availability.new( | ||
| variants: context[:desired].variants, | ||
| stock_locations: context[:stock_locations] | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module Spree | ||
| module Stock | ||
| module Middleware | ||
| class Desired | ||
| def call(context) | ||
| context[:desired] = Spree::StockQuantities.new(context[:inventory_unit_groups].transform_values(&:count)) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| module Spree | ||
| module Stock | ||
| module Middleware | ||
| class InventoryUnit | ||
| def call(context) | ||
| order = context[:order] | ||
|
|
||
| context[:inventory_units] = context[:inventory_units] || | ||
| Spree::Config.stock.inventory_unit_builder_class.new(order).units | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
11 changes: 11 additions & 0 deletions
11
core/app/models/spree/stock/middleware/inventory_unit_group.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module Spree | ||
| module Stock | ||
| module Middleware | ||
| class InventoryUnitGroup | ||
| def call(context) | ||
| context[:inventory_unit_groups] = context[:inventory_units].group_by(&:variant) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| module Spree | ||
| module Stock | ||
| module Middleware | ||
| class Package | ||
| def call(context) | ||
| packages = context[:stock_locations].map do |stock_location| | ||
| on_hand = context[:on_hand_packages][stock_location.id] || Spree::StockQuantities.new | ||
| backordered = context[:backordered_packages][stock_location.id] || Spree::StockQuantities.new | ||
|
|
||
| next if on_hand.empty? && backordered.empty? | ||
|
|
||
| package = Spree::Stock::Package.new(stock_location) | ||
| package.add_multiple(get_units(context, on_hand), :on_hand) | ||
| package.add_multiple(get_units(context, backordered), :backordered) | ||
|
|
||
| package | ||
| end.compact | ||
|
|
||
| context[:packages] = split_packages(packages) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def get_units(context, quantities) | ||
| quantities.flat_map do |variant, quantity| | ||
| context[:inventory_unit_groups][variant].shift(quantity) | ||
| end | ||
| end | ||
|
|
||
| def split_packages(initial_packages) | ||
| splitters = Spree::Config.environment.stock_splitters | ||
|
|
||
| initial_packages.flat_map do |initial_package| | ||
| stock_location = initial_package.stock_location | ||
| Spree::Stock::SplitterChain.new(stock_location, splitters).split([initial_package]) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| module Spree | ||
| module Stock | ||
| module Middleware | ||
| class Shipment | ||
| def call(context) | ||
| context[:shipments] = context[:packages].map do |package| | ||
| shipment = package.shipment = package.to_shipment | ||
| shipment.shipping_rates = Spree::Config.stock.estimator_class.new.shipping_rates(package) | ||
| shipment | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| module Spree | ||
| module Stock | ||
| module Middleware | ||
| class StockLocation | ||
| def call(context) | ||
| filtered_stock_locations = Spree::Config.stock.location_filter_class.new( | ||
| load_stock_locations, context[:order] | ||
| ).filter | ||
| sorted_stock_locations = Spree::Config.stock.location_sorter_class.new( | ||
| filtered_stock_locations | ||
| ).sort | ||
|
|
||
| context[:stock_locations] = sorted_stock_locations | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def load_stock_locations | ||
| Spree::StockLocation.all | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is definitely an example of how overusing this approach makes the code worse. This has seriously obfuscated the dependency chain of the availability and desired variants objects. If we have some dependent computed stuff like this, maybe we just make this methods on the context object or something? It's worth considering this further.
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.
Agreed! I didn't put too much thought into how things shook out entirely at this point in the draft. Will definitely revisit before opening for review.