Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Scope catalog resolution to the store and bulk-write assortment adds

Damian Legawiecbc1a24594749
diff --git a/spree/core/app/models/spree/catalog.rb b/spree/core/app/models/spree/catalog.rb
index ab1d564a23..7d08e05fd8 100644
--- a/spree/core/app/models/spree/catalog.rb
+++ b/spree/core/app/models/spree/catalog.rb
@@ -43,19 +43,22 @@ module Spree
# ties within a node. A parent's assignment covers the whole subtree, so a
# branch adds its own extra catalog without re-assigning the shared one.
#
+ # Always call this through the store — +current_store.catalogs.for_company+
+ # — so the tenant comes from the caller rather than from the argument.
+ #
# @param company [Spree::Company, nil]
# @return [Array<Spree::Catalog>]
- def self.effective_for_company(company)
+ def self.for_company(company)
return [] if company.nil?
nodes = company.self_and_ancestors
- # Constrained to the company's own store as well as its nodes: the
- # assignment validates same-store on write, but a read that trusts the
- # write path would hand another tenant's catalog to this buyer if a row
- # ever arrived past it (raw SQL, an import, a bypass).
+ # Constrained to the receiving relation as well as the company's nodes:
+ # the assignment validates same-store on write, but a read that trusts
+ # the write path would hand another tenant's catalog to this buyer if a
+ # row ever arrived past it (raw SQL, an import, a bypass).
assignments = Spree::CatalogAssignment.
where(assignable_type: 'Spree::Company', assignable_id: nodes.map(&:id)).
- where(catalog_id: for_store(company.store).select(:id)).
+ where(catalog_id: all.select(:id)).
includes(:catalog).
group_by(&:assignable_id)
@@ -66,34 +69,63 @@ module Spree
# The catalogs assigned to any of the given customer groups, by position.
#
+ # Always call this through the store —
+ # +current_store.catalogs.for_customer_groups+ — so the tenant comes from
+ # the caller rather than from the argument.
+ #
# @param customer_groups [Enumerable<Spree::CustomerGroup>]
- # @param store [Spree::Store, nil] narrows to that store's catalogs;
- # defaults to the groups' own store, which is the same thing whenever
- # the caller passed one store's groups
# @return [Array<Spree::Catalog>]
- def self.effective_for_customer_groups(customer_groups, store: nil)
- groups = Array(customer_groups)
- ids = groups.map(&:id)
+ def self.for_customer_groups(customer_groups)
+ ids = Array(customer_groups).map(&:id)
return [] if ids.empty?
- store ||= groups.first.store
-
Spree::CatalogAssignment.
where(assignable_type: 'Spree::CustomerGroup', assignable_id: ids).
- where(catalog_id: for_store(store).select(:id)).
+ where(catalog_id: all.select(:id)).
includes(:catalog).
map(&:catalog).select(&:active?).sort_by { |catalog| catalog.position.to_i }.uniq
end
# Adds products to the assortment, appending to the manual order and
- # skipping ones already present.
+ # skipping ones already present. Written in one statement, so importing a
+ # whole price list is a single insert rather than a row per product.
#
# @param product_ids [Array<Integer>] raw product PKs
# @return [Integer] how many were added
def add_products(product_ids)
- product_ids = store.products.where(id: product_ids).ids - catalog_products.pluck(:product_id)
- product_ids.each { |product_id| catalog_products.create!(product_id: product_id) }
- product_ids.size
+ return 0 if product_ids.blank?
+
+ # Scoped to this store's products, so an id from another tenant adds
+ # nothing rather than being inserted past the row-level validation the
+ # bulk write skips.
+ new_ids = store.products.where(id: product_ids).ids - catalog_products.pluck(:product_id)
+ return 0 if new_ids.empty?
+
+ now = Time.current
+ next_position = (catalog_products.maximum(:position) || 0) + 1
+
+ rows = new_ids.map.with_index do |product_id, index|
+ {
+ catalog_id: id,
+ product_id: product_id,
+ position: next_position + index,
+ created_at: now,
+ updated_at: now
+ }
+ end
+
+ # `acts_as_list` assigns positions in a callback the bulk write skips,
+ # so they are numbered above; :skip keeps a concurrent add from raising
+ # on the unique (catalog_id, product_id) index.
+ opts = { on_duplicate: :skip }
+ # MySQL infers the conflict target from the table's unique constraints
+ # and rejects an explicit +unique_by+; PostgreSQL/SQLite require it.
+ opts[:unique_by] = %i[catalog_id product_id] unless mysql_adapter?
+
+ Spree::CatalogProduct.upsert_all(rows, **opts)
+ touch
+
+ new_ids.size
end
# Copies the price list's products into the assortment — the explicit
diff --git a/spree/core/app/models/spree/pricing_provider/internal.rb b/spree/core/app/models/spree/pricing_provider/internal.rb
index fcb327633d..a34403ea8a 100644
--- a/spree/core/app/models/spree/pricing_provider/internal.rb
+++ b/spree/core/app/models/spree/pricing_provider/internal.rb
@@ -62,10 +62,13 @@ module Spree
# @return [Array<Spree::PriceList>]
def catalog_price_lists
@catalog_price_lists ||= begin
- catalogs = Spree::Catalog.effective_for_company(context.company)
- if catalogs.empty? && context.user && context.store
- groups = context.user.try(:customer_groups)&.where(store_id: context.store.id) || []
- catalogs = Spree::Catalog.effective_for_customer_groups(groups)
+ # Every lookup starts from the context's store, so a catalog is
+ # only ever reached through the tenant being priced for.
+ store = context.store
+ catalogs = store ? store.catalogs.for_company(context.company) : []
+ if catalogs.empty? && context.user && store
+ groups = context.user.try(:customer_groups)&.where(store_id: store.id) || []
+ catalogs = store.catalogs.for_customer_groups(groups)
end
if catalogs.empty?
catalogs = [context.channel&.default_catalog].compact.select(&:active?)
diff --git a/spree/core/app/services/spree/products/for_context.rb b/spree/core/app/services/spree/products/for_context.rb
index 0392ab935a..3eefa62d7a 100644
--- a/spree/core/app/services/spree/products/for_context.rb
+++ b/spree/core/app/services/spree/products/for_context.rb
@@ -27,7 +27,7 @@ module Spree
base ||= store.products.for_channel(channel)
company ||= sole_standing_company(store, customer)
- catalogs = Spree::Catalog.effective_for_company(company)
+ catalogs = store.catalogs.for_company(company)
catalogs = customer_group_catalogs(store, customer) if catalogs.empty?
catalogs = [channel&.default_catalog].compact.select(&:active?) if catalogs.empty?
@@ -74,7 +74,7 @@ module Spree
return [] if customer.nil?
groups = customer.customer_groups.where(store_id: store.id)
- Spree::Catalog.effective_for_customer_groups(groups, store: store)
+ store.catalogs.for_customer_groups(groups)
end
end
end
diff --git a/spree/core/spec/models/spree/catalog_spec.rb b/spree/core/spec/models/spree/catalog_spec.rb
index 8f68d4c801..2b5dbf18ec 100644
--- a/spree/core/spec/models/spree/catalog_spec.rb
+++ b/spree/core/spec/models/spree/catalog_spec.rb
@@ -27,14 +27,38 @@ describe Spree::Catalog, type: :model do
expect(catalog.add_products([product.id])).to eq(0)
expect(catalog.products.reload).to contain_exactly(product)
end
+
+ # `acts_as_list` numbers rows in a callback the bulk insert skips, so the
+ # appended positions are assigned by hand and worth asserting.
+ it 'appends after the existing assortment in one statement' do
+ first = create(:product, store: store)
+ catalog.add_products([first.id])
+ later = create_list(:product, 2, store: store)
+
+ expect { catalog.add_products(later.map(&:id)) }.
+ to change { catalog.catalog_products.count }.by(2)
+
+ positions = catalog.catalog_products.reload.order(:position).pluck(:product_id, :position)
+ expect(positions.map(&:first)).to eq([first.id, *later.map(&:id)])
+ expect(positions.map(&:last)).to eq([1, 2, 3])
+ end
+
+ it 'adds a batch alongside an existing row without raising on the duplicate' do
+ existing = create(:product, store: store)
+ fresh = create(:product, store: store)
+ catalog.add_products([existing.id])
+
+ expect(catalog.add_products([existing.id, fresh.id])).to eq(1)
+ expect(catalog.products.reload).to contain_exactly(existing, fresh)
+ end
end
- describe '.effective_for_company' do
+ describe '.for_company' do
let(:root) { create(:company, store: store) }
let(:division) { create(:company, store: store, kind: 'division', parent: root) }
it 'is empty with no node' do
- expect(described_class.effective_for_company(nil)).to eq([])
+ expect(store.catalogs.for_company(nil)).to eq([])
end
it 'collects assignments up the ancestor chain, nearest node first' do
@@ -43,9 +67,9 @@ describe Spree::Catalog, type: :model do
create(:catalog_assignment, catalog: parent_catalog, assignable: root)
create(:catalog_assignment, catalog: own_catalog, assignable: division)
- expect(described_class.effective_for_company(division)).to eq([own_catalog, parent_catalog])
+ expect(store.catalogs.for_company(division)).to eq([own_catalog, parent_catalog])
# The parent's own resolution sees only its own assignment.
- expect(described_class.effective_for_company(root)).to eq([parent_catalog])
+ expect(store.catalogs.for_company(root)).to eq([parent_catalog])
end
it 'ignores inactive catalogs and sibling assignments' do
@@ -54,7 +78,7 @@ describe Spree::Catalog, type: :model do
sibling = create(:company, store: store, kind: 'division', parent: root)
create(:catalog_assignment, catalog: create(:catalog, store: store), assignable: sibling)
- expect(described_class.effective_for_company(division)).to eq([])
+ expect(store.catalogs.for_company(division)).to eq([])
end
it 'orders several catalogs on one node by position' do
@@ -65,7 +89,7 @@ describe Spree::Catalog, type: :model do
create(:catalog_assignment, catalog: second, assignable: division)
create(:catalog_assignment, catalog: first, assignable: division)
- expect(described_class.effective_for_company(division)).to eq([first, second])
+ expect(store.catalogs.for_company(division)).to eq([first, second])
end
end
@@ -107,7 +131,7 @@ describe Spree::Catalog, type: :model do
foreign_catalog = create(:catalog, store: create(:store))
Spree::CatalogAssignment.new(catalog: foreign_catalog, assignable: company).save(validate: false)
- expect(described_class.effective_for_company(company)).to eq([])
+ expect(store.catalogs.for_company(company)).to eq([])
end
it 'ignores a foreign catalog assigned to a customer group' do
@@ -115,7 +139,20 @@ describe Spree::Catalog, type: :model do
foreign_catalog = create(:catalog, store: create(:store))
Spree::CatalogAssignment.new(catalog: foreign_catalog, assignable: group).save(validate: false)
- expect(described_class.effective_for_customer_groups([group], store: store)).to eq([])
+ expect(store.catalogs.for_customer_groups([group])).to eq([])
+ end
+
+ # The tenant comes from the relation the caller starts at, never from the
+ # company handed in — so asking one store about another store's node
+ # resolves nothing, whatever that node is assigned.
+ it 'resolves against the receiving store rather than the company own store' do
+ other_store = create(:store)
+ company = create(:company, store: other_store)
+ catalog = create(:catalog, store: other_store)
+ create(:catalog_assignment, catalog: catalog, assignable: company)
+
+ expect(other_store.catalogs.for_company(company)).to eq([catalog])
+ expect(store.catalogs.for_company(company)).to eq([])
end
end