diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index f25d5f7f1b..f60ac4500f 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -210,15 +210,14 @@ class Enterprise < ApplicationRecord joins(:enterprise_roles).where('enterprise_roles.user_id = ?', user.id) end } - scope :relatives_of_one_union_others, lambda { |one, others| + + scope :parents_of_one_union_others, lambda { |one, others| where(" enterprises.id IN - (SELECT child_id FROM enterprise_relationships WHERE enterprise_relationships.parent_id=?) - OR enterprises.id IN (SELECT parent_id FROM enterprise_relationships WHERE enterprise_relationships.child_id=?) OR enterprises.id IN (?) - ", one, one, others) + ", one, others) } def business_address_empty?(attributes) @@ -273,9 +272,9 @@ class Enterprise < ApplicationRecord ", id, id) end - def plus_relatives_and_oc_producers(order_cycles) + def plus_parents_and_order_cycle_producers(order_cycles) oc_producer_ids = Exchange.in_order_cycle(order_cycles).incoming.pluck :sender_id - Enterprise.not_hidden.is_primary_producer.relatives_of_one_union_others(id, oc_producer_ids | [id]) + Enterprise.not_hidden.is_primary_producer.parents_of_one_union_others(id, oc_producer_ids | [id]) end def relatives_including_self diff --git a/app/serializers/api/enterprise_shopfront_serializer.rb b/app/serializers/api/enterprise_shopfront_serializer.rb index d4bc4bc266..ac0fed09f2 100644 --- a/app/serializers/api/enterprise_shopfront_serializer.rb +++ b/app/serializers/api/enterprise_shopfront_serializer.rb @@ -54,7 +54,7 @@ module Api def producers ActiveModel::ArraySerializer.new( - enterprise.plus_relatives_and_oc_producers( + enterprise.plus_parents_and_order_cycle_producers( OrderCycle.not_closed.with_distributor(enterprise) ), each_serializer: Api::EnterpriseThinSerializer diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 47b7d41c0f..e5892cd1e6 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -695,8 +695,35 @@ describe Enterprise do end end - describe "#plus_relatives_and_oc_producers" do - it "does not find non-produders " do + describe "#parents_of_one_union_others" do + it "should return only parent producers" do + supplier = create(:supplier_enterprise) + distributor = create(:distributor_enterprise, is_primary_producer: false) + permission = EnterpriseRelationshipPermission.create(name: "add_to_order_cycle") + create(:enterprise_relationship, parent: distributor, child: supplier, permissions: [permission]) + expect(Enterprise.parents_of_one_union_others(supplier, nil)).to include(distributor) + end + + it "should return other enterprise if it is passed as a second argument" do + another_enterprise = create(:enterprise) + supplier = create(:supplier_enterprise) + distributor = create(:distributor_enterprise, is_primary_producer: false) + permission = EnterpriseRelationshipPermission.create(name: "add_to_order_cycle") + create(:enterprise_relationship, parent: distributor, child: supplier, permissions: [permission]) + expect(Enterprise.parents_of_one_union_others(supplier, another_enterprise)).to include(another_enterprise) + end + + it "does not find child in the relationship" do + supplier = create(:supplier_enterprise) + distributor = create(:distributor_enterprise, is_primary_producer: false) + permission = EnterpriseRelationshipPermission.create(name: "add_to_order_cycle") + create(:enterprise_relationship, parent: distributor, child: supplier, permissions: [permission]) + expect(Enterprise.parents_of_one_union_others(distributor, nil)).not_to include(supplier) + end + end + + describe "#plus_parents_and_order_cycle_producers" do + it "does not find non-producers" do supplier = create(:supplier_enterprise) distributor = create(:distributor_enterprise, is_primary_producer: false) product = create(:product) @@ -706,7 +733,55 @@ describe Enterprise do distributors: [distributor], variants: [product.master] ) - expect(distributor.plus_relatives_and_oc_producers(order_cycle)).to eq([supplier]) + expect(distributor.plus_parents_and_order_cycle_producers(order_cycle)).to eq([supplier]) + end + + it "finds parent in the relationship" do + supplier = create(:supplier_enterprise) + distributor = create(:distributor_enterprise, is_primary_producer: false) + permission = EnterpriseRelationshipPermission.create(name: "add_to_order_cycle") + product = create(:product) + order_cycle = create( + :simple_order_cycle, + distributors: [distributor], + suppliers: [supplier], + variants: [product.master] + ) + create(:enterprise_relationship, parent: distributor, child: supplier, permissions: [permission]) + expect(distributor.plus_parents_and_order_cycle_producers(order_cycle)).to include(supplier) + end + + it "does not find child in the relationship" do + supplier = create(:supplier_enterprise) + distributor = create(:distributor_enterprise, is_primary_producer: false) + permission = EnterpriseRelationshipPermission.create(name: "add_to_order_cycle") + create(:enterprise_relationship, parent: distributor, child: supplier, permissions: [permission]) + product = create(:product) + order_cycle = create( + :simple_order_cycle, + suppliers: [supplier], + distributors: [distributor], + variants: [product.master] + ) + expected = supplier.plus_parents_and_order_cycle_producers(order_cycle) + expect(expected).not_to include(distributor) + end + + it "it finds sender enterprises for order cycles that are passed" do + supplier = create(:supplier_enterprise) + sender = create(:supplier_enterprise) + distributor = create(:distributor_enterprise, is_primary_producer: false) + permission = EnterpriseRelationshipPermission.create(name: "add_to_order_cycle") + create(:enterprise_relationship, parent: distributor, child: supplier, permissions: [permission]) + product = create(:product) + order_cycle = create( + :simple_order_cycle, + suppliers: [sender], + distributors: [distributor], + variants: [product.master] + ) + expected = supplier.plus_parents_and_order_cycle_producers(order_cycle) + expect(expected).to include(sender) end end end diff --git a/spec/serializers/api/enterprise_shopfront_serializer_spec.rb b/spec/serializers/api/enterprise_shopfront_serializer_spec.rb index e1863ddc55..a2f4358ce1 100644 --- a/spec/serializers/api/enterprise_shopfront_serializer_spec.rb +++ b/spec/serializers/api/enterprise_shopfront_serializer_spec.rb @@ -6,8 +6,8 @@ describe Api::EnterpriseShopfrontSerializer do let!(:hub) { create(:distributor_enterprise, with_payment_and_shipping: true) } let!(:producer) { create(:supplier_enterprise) } let!(:producer_hidden) { create(:supplier_enterprise_hidden) } - let!(:relationship) { create(:enterprise_relationship, parent: hub, child: producer) } - let!(:relationship2) { create(:enterprise_relationship, parent: hub, child: producer_hidden) } + let!(:relationship) { create(:enterprise_relationship, parent: producer, child: hub) } + let!(:relationship2) { create(:enterprise_relationship, parent: producer_hidden, child: hub) } let!(:taxon1) { create(:taxon, name: 'Meat') } let!(:taxon2) { create(:taxon, name: 'Veg') } diff --git a/spec/system/consumer/shops_spec.rb b/spec/system/consumer/shops_spec.rb index 5b220e39a7..6ab6b03106 100644 --- a/spec/system/consumer/shops_spec.rb +++ b/spec/system/consumer/shops_spec.rb @@ -17,7 +17,7 @@ describe 'Shops', js: true do coordinator: create(:distributor_enterprise)) } let!(:producer) { create(:supplier_enterprise) } - let!(:er) { create(:enterprise_relationship, parent: distributor, child: producer) } + let!(:er) { create(:enterprise_relationship, parent: producer, child: distributor) } before do producer.set_producer_property 'Organic', 'NASAA 12345' @@ -42,6 +42,7 @@ describe 'Shops', js: true do it "by URL" do pending("#9649") visit shops_path(anchor: "/?query=xyzzy") + sleep 1 expect(page).not_to have_content distributor.name expect(page).to have_content "Sorry, no results found for xyzzy. Try another search?" end @@ -96,7 +97,7 @@ describe 'Shops', js: true do let!(:hub) { create(:distributor_enterprise, with_payment_and_shipping: false) } let!(:order_cycle) { create(:simple_order_cycle, distributors: [hub], coordinator: hub) } let!(:producer) { create(:supplier_enterprise) } - let!(:er) { create(:enterprise_relationship, parent: hub, child: producer) } + let!(:er) { create(:enterprise_relationship, parent: producer, child: hub) } it "does not show hubs that are not ready for checkout" do visit shops_path