Fix duplication issue

When a product had mutiple variant assigned to the same category it
should only return the product one
This commit is contained in:
Gaetan Craig-Riou
2024-02-21 16:12:33 +11:00
parent 8c05838080
commit 678fa37df9
2 changed files with 23 additions and 2 deletions

View File

@@ -152,7 +152,7 @@ class ProductsReflex < ApplicationReflex
def fetch_products
product_query = OpenFoodNetwork::Permissions.new(current_user)
.editable_products.merge(product_scope).ransack(ransack_query).result
.editable_products.merge(product_scope).ransack(ransack_query).result(distinct: true)
@pagy, @products = pagy(product_query.order(:name), items: @per_page, page: @page,
size: [1, 2, 2, 1])
end

View File

@@ -15,7 +15,7 @@ describe ProductsReflex, type: :reflex, feature: :admin_style_v3 do
end
describe '#fetch' do
subject{ build_reflex(method_name: :fetch, **context) }
subject { build_reflex(method_name: :fetch, **context) }
describe "sorting" do
let!(:product_z) { create(:simple_product, name: "Zucchini") }
@@ -34,6 +34,27 @@ describe ProductsReflex, type: :reflex, feature: :admin_style_v3 do
end
end
describe '#filter' do
context "when filtering by category" do
let!(:product_a) { create(:simple_product, name: "Apples") }
let!(:product_z) do
create(:simple_product, name: "Zucchini").tap do |p|
p.variants.first.update(primary_taxon: category_c)
end
end
let(:category_c) { create(:taxon, name: "Category 1") }
it "returns product with a variant matching the given category" do
# Add a second variant to test we are not returning duplicate product
product_z.variants << create(:variant, primary_taxon: category_c)
reflex = run_reflex(:filter, params: { category_id: category_c.id } )
expect(reflex.get(:products).to_a).to eq([product_z])
end
end
end
describe '#bulk_update' do
let!(:variant_a1) {
product_a.variants.first.tap{ |v|