diff --git a/app/services/variants_stock_levels.rb b/app/services/variants_stock_levels.rb index 2d0628f5cb..44e2611f04 100644 --- a/app/services/variants_stock_levels.rb +++ b/app/services/variants_stock_levels.rb @@ -12,7 +12,7 @@ class VariantsStockLevels order_variant_ids = variant_stock_levels.keys missing_variant_ids = requested_variant_ids - order_variant_ids missing_variant_ids.each do |variant_id| - variant = Spree::Variant.find(variant_id) + variant = scoped_variant(order.distributor, Spree::Variant.find(variant_id)) variant_stock_levels[variant_id] = { quantity: 0, max_quantity: 0, on_hand: variant.on_hand, on_demand: variant.on_demand } end @@ -36,7 +36,7 @@ class VariantsStockLevels def variant_stock_levels(line_items) Hash[ line_items.map do |line_item| - variant = scoped_variant(line_item) + variant = scoped_variant(line_item.order.distributor, line_item.variant) [variant.id, { quantity: line_item.quantity, @@ -47,10 +47,7 @@ class VariantsStockLevels ] end - def scoped_variant(line_item) - distributor = line_item.order.distributor - variant = line_item.variant - + def scoped_variant(distributor, variant) return variant if distributor.blank? OpenFoodNetwork::ScopeVariantToHub.new(distributor).scope(variant) diff --git a/spec/services/variants_stock_levels_spec.rb b/spec/services/variants_stock_levels_spec.rb index dc90cf3f9a..5977845665 100644 --- a/spec/services/variants_stock_levels_spec.rb +++ b/spec/services/variants_stock_levels_spec.rb @@ -64,7 +64,7 @@ describe VariantsStockLevels do let!(:variant_override_not_in_order) { create(:variant_override, hub: distributor, variant: variant_not_in_the_order, - count_on_hand: 404) + count_on_hand: 201) } before do @@ -76,17 +76,23 @@ describe VariantsStockLevels do context "when the variant is in the order" do it "returns the on_hand value of the override" do expect(variant_stock_levels.call(order, [variant_in_the_order.id])).to eq( - variant_in_the_order.id => { quantity: 2, max_quantity: 3, on_hand: 200, on_demand: false } + variant_in_the_order.id => { + quantity: 2, max_quantity: 3, on_hand: 200, on_demand: false + } ) end end context "with variants that are not in the order" do - xit "returns the on_hand value of the override" do + it "returns the on_hand value of the override" do variant_ids = [variant_in_the_order.id, variant_not_in_the_order.id] expect(variant_stock_levels.call(order, variant_ids)).to eq( - variant_in_the_order.id => { quantity: 2, max_quantity: 3, on_hand: 200, on_demand: false }, - variant_not_in_the_order.id => { quantity: 0, max_quantity: 0, on_hand: 404, on_demand: false } + variant_in_the_order.id => { + quantity: 2, max_quantity: 3, on_hand: 200, on_demand: false + }, + variant_not_in_the_order.id => { + quantity: 0, max_quantity: 0, on_hand: 201, on_demand: false + } ) end end