Merge pull request #9768 from ashwini-seshadri/admin_permissions_granting_permission_to_a_shop_displays_it_in_producers_list_as_if_it_was_a_supplier

Fix for: [Admin][Permissions] Granting permission to a shop displays it in "Producers" list as if it was a supplier #9589
This commit is contained in:
Maikel
2022-10-17 09:58:03 +11:00
committed by GitHub
5 changed files with 89 additions and 14 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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') }

View File

@@ -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