Only left join variant overrides belonging to the distributor when searching for in stock variants.

Before the :scope_to_distributor call was filtering out variants from other distributors anyway via the :exchange_variants table but it is clearer if we filter by distributor on the :variant_overrides left join too.
This commit is contained in:
Cillian O'Ruanaidh
2021-12-17 15:25:55 +00:00
parent 2462d71ab5
commit 9bc7922734
2 changed files with 12 additions and 6 deletions

View File

@@ -16,7 +16,7 @@ module OpenFoodNetwork
def search
@variants = query_scope
scope_to_in_stock_only if params[:distributor_id] && params[:include_out_of_stock] != "1"
scope_to_in_stock_only if scope_to_in_stock_only?
scope_to_schedule if params[:schedule_id]
scope_to_order_cycle if params[:order_cycle_id]
scope_to_distributor if params[:distributor_id]
@@ -72,7 +72,8 @@ module OpenFoodNetwork
def scope_to_in_stock_only
@variants = @variants.joins(
"INNER JOIN spree_stock_items ON spree_stock_items.variant_id = spree_variants.id
LEFT JOIN variant_overrides ON variant_overrides.variant_id = spree_variants.id"
LEFT JOIN variant_overrides ON variant_overrides.variant_id = spree_variants.id AND
variant_overrides.hub_id = #{distributor.id}"
).where("
variant_overrides.on_demand IS TRUE OR
variant_overrides.count_on_hand > 0 OR
@@ -81,6 +82,10 @@ module OpenFoodNetwork
")
end
def scope_to_in_stock_only?
params[:distributor_id] && params[:include_out_of_stock] != "1"
end
def scope_variants_to_distributor(variants, distributor)
scoper = OpenFoodNetwork::ScopeVariantToHub.new(distributor)
# Perform scoping after all filtering is done.

View File

@@ -1,5 +1,6 @@
# frozen_string_literal: true
require 'spec_helper'
require 'open_food_network/scope_variants_for_search'
describe OpenFoodNetwork::ScopeVariantsForSearch do
@@ -85,7 +86,7 @@ describe OpenFoodNetwork::ScopeVariantsForSearch do
create(:variant_override, variant: variant, hub: d1, on_demand: false, count_on_hand: 0)
variant
end
let!(:distributor1_variant_with_override_without_stock_level_set_and_producer_not_in_stock) do
let!(:distributor1_variant_with_override_without_stock_level_set_and_no_producer_stock) do
variant = create(:simple_product).variants.first
variant.stock_items.first.update!(backorderable: false, count_on_hand: 0)
create(:simple_order_cycle, distributors: [d1], variants: [variant])
@@ -100,7 +101,7 @@ describe OpenFoodNetwork::ScopeVariantsForSearch do
variant
end
let!(:distributor2_variant_with_override_in_stock) do
create_variant_with_variant_override_for(d2, count_on_hand: 1)
create_variant_with_variant_override_for(d2, on_demand: true, count_on_hand: nil)
end
context "when :include_out_of_stock is not specified" do
@@ -120,7 +121,7 @@ describe OpenFoodNetwork::ScopeVariantsForSearch do
distributor1_variant_not_backorderable_and_not_on_hand,
distributor1_variant_with_override_not_on_demand_and_not_on_hand,
distributor1_variant_with_override_not_in_stock_but_producer_in_stock,
distributor1_variant_with_override_without_stock_level_set_and_producer_not_in_stock,
distributor1_variant_with_override_without_stock_level_set_and_no_producer_stock,
distributor2_variant_with_override_in_stock
)
end
@@ -136,7 +137,7 @@ describe OpenFoodNetwork::ScopeVariantsForSearch do
distributor1_variant_with_override_on_demand_but_not_on_hand,
distributor1_variant_with_override_on_hand_but_not_on_demand,
distributor1_variant_with_override_without_stock_level_set_but_producer_in_stock,
distributor1_variant_with_override_without_stock_level_set_and_producer_not_in_stock,
distributor1_variant_with_override_without_stock_level_set_and_no_producer_stock,
distributor1_variant_not_backorderable_and_not_on_hand,
distributor1_variant_with_override_not_on_demand_and_not_on_hand,
distributor1_variant_with_override_not_in_stock_but_producer_in_stock