Add scoping to VariantsStockLevels when variant is not in the order

This commit is contained in:
Matt-Yorkley
2020-03-27 14:15:15 +01:00
parent 857cacb74b
commit 7d33a237d0
2 changed files with 14 additions and 11 deletions

View File

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

View File

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