Add variant scoping to VariantStockLevels

This commit is contained in:
Matt-Yorkley
2020-03-27 13:38:57 +01:00
parent 83b90f3167
commit fbfe663ebc
2 changed files with 20 additions and 6 deletions

View File

@@ -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

View File

@@ -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