Fix order cycle controller spec, product properties filtering

And fix the underlying product renderer
This commit is contained in:
Gaetan Craig-Riou
2024-05-27 15:11:37 +10:00
parent 3dc7c2bf56
commit feb7e173b1
4 changed files with 42 additions and 18 deletions

View File

@@ -86,8 +86,9 @@ module Api
end
def distributed_products
OrderCycles::DistributedProductsService.new(distributor, order_cycle,
customer).products_relation
OrderCycles::DistributedProductsService.new(
distributor, order_cycle, customer
).products_supplier_relation.pluck(:id)
end
end
end

View File

@@ -73,7 +73,8 @@ class ProductsRenderer
results = query.joins('JOIN enterprises ON enterprises.id = first_variant.supplier_id
LEFT OUTER JOIN producer_properties
ON producer_properties.producer_id = enterprises.id').
where(producer_properties: { property_id: property_ids })
where(producer_properties: { property_id: property_ids }).
where(inherits_properties: true)
return results
end

View File

@@ -107,14 +107,15 @@ module Api
let!(:supplier) { create(:supplier_enterprise, properties: [supplier_property]) }
before do
product1.update!(supplier:)
product2.update!(supplier:)
product3.update!(supplier:, inherits_properties: false)
product1.variants.first.update!(supplier:)
product2.variants.first.update!(supplier:)
product3.update!(inherits_properties: false)
product3.variants.first.update!(supplier:)
end
it "filter out the product that don't inherits from supplier properties" do
api_get :products, id: order_cycle.id, distributor: distributor.id,
q: { with_properties: [supplier_property.id] }
q: { with_variants_supplier_properties: [supplier_property.id] }
expect(response.status).to eq 200
expect(product_ids).to match_array [product1.id, product2.id]
@@ -263,16 +264,20 @@ module Api
context "with custom taxon ordering applied and duplicate product names in the order cycle" do
let!(:supplier) { create(:supplier_enterprise) }
let!(:product5) {
create(:product, name: "Duplicate name", primary_taxon: taxon3, supplier_id: supplier.id)
create(:product, name: "Duplicate name", primary_taxon_id: taxon3.id,
supplier_id: supplier.id)
}
let!(:product6) {
create(:product, name: "Duplicate name", primary_taxon: taxon3, supplier_id: supplier.id)
create(:product, name: "Duplicate name", primary_taxon_id: taxon3.id,
supplier_id: supplier.id)
}
let!(:product7) {
create(:product, name: "Duplicate name", primary_taxon: taxon2, supplier_id: supplier.id)
create(:product, name: "Duplicate name", primary_taxon_id: taxon2.id,
supplier_id: supplier.id)
}
let!(:product8) {
create(:product, name: "Duplicate name", primary_taxon: taxon2, supplier_id: supplier.id)
create(:product, name: "Duplicate name", primary_taxon_id: taxon2.id,
supplier_id: supplier.id)
}
before do

View File

@@ -15,21 +15,24 @@ RSpec.describe ProductsRenderer do
let(:fruits_supplier) { create(:supplier_enterprise) }
let(:cakes_supplier) { create(:supplier_enterprise) }
let!(:product_apples) {
create(:product, name: "apples", primary_taxon_id: fruits.id, supplier_id: fruits_supplier.id)
create(:product, name: "apples", primary_taxon_id: fruits.id,
supplier_id: fruits_supplier.id, inherits_properties: true)
}
let!(:product_banana_bread) {
create(:product, name: "banana bread", variants: [
create(:variant, supplier: cakes_supplier, primary_taxon: cakes),
create(:variant, supplier: fruits_supplier, primary_taxon: cakes)
])
create(:product, name: "banana bread", inherits_properties: true,
variants: [
create(:variant, supplier: cakes_supplier, primary_taxon: cakes),
create(:variant, supplier: fruits_supplier, primary_taxon: cakes)
]
)
}
let!(:product_cherries) {
create(:product, name: "cherries", primary_taxon_id: fruits.id,
supplier_id: fruits_supplier.id)
supplier_id: fruits_supplier.id, inherits_properties: true)
}
let!(:product_doughnuts) {
create(:product, name: "doughnuts", primary_taxon_id: cakes.id,
supplier_id: cakes_supplier.id)
supplier_id: cakes_supplier.id, inherits_properties: true)
}
before do
@@ -112,6 +115,20 @@ RSpec.describe ProductsRenderer do
expect(products).to eq([product_apples, product_cherries])
end
it "filters out products with inherits_properties set to false" do
product_cherries.update!(inherits_properties: false)
product_banana_bread.update!(inherits_properties: false)
fruits_supplier.producer_properties.create!({ property_id: property_organic.id,
value: '1', position: 1 })
search_param = { q: { "with_variants_supplier_properties" => [property_organic.id] } }
products_renderer = ProductsRenderer.new(distributor, order_cycle, customer, search_param)
products = products_renderer.send(:products)
expect(products).to eq([product_apples])
end
it "filters products with property when sorting is enabled" do
allow(distributor).to receive(:preferred_shopfront_taxon_order) {
"#{fruits.id},#{cakes.id}"