Only scope variant when inventory is enabled

This commit is contained in:
Gaetan Craig-Riou
2025-06-13 23:35:23 +10:00
parent 6212cd4d07
commit 8829f6ad03
5 changed files with 56 additions and 20 deletions

View File

@@ -44,7 +44,6 @@ class ProductsRenderer
paginated_products = paginate(results)
# TODO test this ?
if options[:inventory_enabled]
# Scope results with variant_overrides
paginated_products.each { |product| product_scoper.scope(product) }

View File

@@ -81,16 +81,25 @@ module OpenFoodNetwork
end
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 AND
variant_overrides.hub_id = #{distributor.id}"
).where("
variant_overrides.on_demand IS TRUE OR
variant_overrides.count_on_hand > 0 OR
(variant_overrides.on_demand IS NULL AND spree_stock_items.backorderable IS TRUE) OR
(variant_overrides.count_on_hand IS NULL AND spree_stock_items.count_on_hand > 0)
")
if OpenFoodNetwork::FeatureToggle.enabled?(:inventory, distributor)
@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 AND
variant_overrides.hub_id = #{distributor.id}"
).where("
variant_overrides.on_demand IS TRUE OR
variant_overrides.count_on_hand > 0 OR
(variant_overrides.on_demand IS NULL AND spree_stock_items.backorderable IS TRUE) OR
(variant_overrides.count_on_hand IS NULL AND spree_stock_items.count_on_hand > 0)
")
else
@variants = @variants.joins(
"INNER JOIN spree_stock_items ON spree_stock_items.variant_id = spree_variants.id"
).where("
spree_stock_items.backorderable IS TRUE OR
spree_stock_items.count_on_hand > 0
")
end
end
def scope_to_in_stock_only?

View File

@@ -80,10 +80,16 @@ module Reporting
def variant_scoper_for(distributor_id)
@variant_scopers_by_distributor_id ||= {}
variant_overrides = {}
if OpenFoodNetwork::FeatureToggle.enabled?(:inventory,
Enterprise.find_by(id: distributor_id))
variant_overrides = report_variant_overrides[distributor_id]
end
@variant_scopers_by_distributor_id[distributor_id] ||=
OpenFoodNetwork::ScopeVariantToHub.new(
distributor_id,
report_variant_overrides[distributor_id] || {},
variant_overrides,
)
end

View File

@@ -126,20 +126,42 @@ RSpec.describe OpenFoodNetwork::ScopeVariantsForSearch do
context "when :include_out_of_stock is not specified" do
let(:params) { { distributor_id: d1.id } }
it "returns variants for the given distributor if they have a variant override which is
in stock, or if they have a variant override with no stock level set but the producer
has stock, or if they don't have a variant override and the producer has stock" do
context "with inventory enabled", feature: :inventory do
it "returns variants for the given distributor if they have a variant override
which is in stock, or if they have a variant override with no stock level set
but the producer has stock, or if they don't have a variant override
and the producer has stock" do
expect(result).to include(
distributor1_variant_on_hand_but_not_backorderable,
distributor1_variant_backorderable_but_not_on_hand,
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
)
expect(result).not_to include(
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_no_producer_stock,
distributor2_variant_with_override_in_stock
)
end
end
it "returns variants for the given distributor if the producer has stock" do
# variant with override are returned here because the associated variant has stock
expect(result).to include(
distributor1_variant_on_hand_but_not_backorderable,
distributor1_variant_backorderable_but_not_on_hand,
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_but_producer_in_stock,
distributor1_variant_with_override_not_in_stock_but_producer_in_stock,
)
expect(result).not_to include(
distributor1_variant_not_backorderable_and_not_on_hand,
distributor1_variant_with_override_on_demand_but_not_on_hand,
distributor1_variant_with_override_on_hand_but_not_on_demand,
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_no_producer_stock,
distributor2_variant_with_override_in_stock
)

View File

@@ -132,7 +132,7 @@ RSpec.describe Reporting::Reports::OrdersAndFulfillment::OrderCycleCustomerTotal
end
end
context 'when a variant override applies' do
context 'when a variant override applies', feature: :inventory do
let!(:order) do
create(
:completed_order_with_totals,