From fbfe663ebcc1991571507e7e43311e6bce2dc271 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 27 Mar 2020 13:38:57 +0100 Subject: [PATCH] Add variant scoping to VariantStockLevels --- app/services/variants_stock_levels.rb | 20 +++++++++++++++++--- spec/services/variants_stock_levels_spec.rb | 6 +++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/services/variants_stock_levels.rb b/app/services/variants_stock_levels.rb index 69dbc49dee..2d0628f5cb 100644 --- a/app/services/variants_stock_levels.rb +++ b/app/services/variants_stock_levels.rb @@ -1,6 +1,8 @@ # Report the stock levels of: # - all variants in the order # - all requested variant ids +require 'open_food_network/scope_variant_to_hub' + class VariantsStockLevels def call(order, requested_variant_ids) variant_stock_levels = variant_stock_levels(order.line_items) @@ -34,12 +36,24 @@ class VariantsStockLevels def variant_stock_levels(line_items) Hash[ line_items.map do |line_item| - [line_item.variant.id, + variant = scoped_variant(line_item) + + [variant.id, { quantity: line_item.quantity, max_quantity: line_item.max_quantity, - on_hand: line_item.variant.on_hand, - on_demand: line_item.variant.on_demand }] + on_hand: variant.on_hand, + on_demand: variant.on_demand }] end ] end + + def scoped_variant(line_item) + distributor = line_item.order.distributor + variant = line_item.variant + + return variant if distributor.blank? + + OpenFoodNetwork::ScopeVariantToHub.new(distributor).scope(variant) + variant + end end diff --git a/spec/services/variants_stock_levels_spec.rb b/spec/services/variants_stock_levels_spec.rb index 8aaa6503bf..ecd0656bc0 100644 --- a/spec/services/variants_stock_levels_spec.rb +++ b/spec/services/variants_stock_levels_spec.rb @@ -59,7 +59,7 @@ describe VariantsStockLevels do let!(:variant_override) { create(:variant_override, hub: distributor, variant: variant_in_the_order, - count_on_hand: 404) + count_on_hand: 200) } before do @@ -68,9 +68,9 @@ describe VariantsStockLevels do order.save end - xit "returns the on_hand value of the override" 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: 404, on_demand: false } + variant_in_the_order.id => { quantity: 2, max_quantity: 3, on_hand: 200, on_demand: false } ) end end