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.

Keep a commission rule inside its own store's catalog

Damian Legawiec182bcdc63ddf
diff --git a/spree/core/app/models/spree/commission_rules/category_rule.rb b/spree/core/app/models/spree/commission_rules/category_rule.rb
index 40fc5e9f12..7359465b59 100644
--- a/spree/core/app/models/spree/commission_rules/category_rule.rb
+++ b/spree/core/app/models/spree/commission_rules/category_rule.rb
@@ -12,7 +12,7 @@ module Spree
preference :category_ids, :array, default: [],
parse_on_set: normalize_id_preference(
klass: Spree::Category,
- scope: ->(rule) { rule.store&.categories || Spree::Category.none }
+ scope: ->(rule) { rule.store.categories }
)
# @return [Array<Spree::Category>]
diff --git a/spree/core/app/models/spree/commission_rules/product_rule.rb b/spree/core/app/models/spree/commission_rules/product_rule.rb
index c9c0583986..a9b9568073 100644
--- a/spree/core/app/models/spree/commission_rules/product_rule.rb
+++ b/spree/core/app/models/spree/commission_rules/product_rule.rb
@@ -12,6 +12,12 @@ module Spree
foreign_key: :commission_rule_id, dependent: :destroy
has_many :products, class_name: 'Spree::Product', through: :commission_rule_products
+ # Refused rather than filtered: unlike the id preferences, which raise on
+ # a foreign id, this list arrives as records. A rate narrowed to another
+ # marketplace's catalog would read those products' ids straight back out
+ # of the serializer.
+ validate :products_belong_to_the_store
+
def self.additional_permitted_attributes
[product_ids: []]
end
@@ -33,6 +39,19 @@ module Spree
commission_rule_products.exists?(product_id: context.product.id)
end
+
+ private
+
+ # Counted rather than compared: a rate covering much of the catalog
+ # would otherwise load every product to check them.
+ def products_belong_to_the_store
+ chosen_ids = products.map(&:id).compact
+ return if chosen_ids.empty?
+
+ return if store.products.where(id: chosen_ids).count == chosen_ids.uniq.size
+
+ errors.add(:products, :invalid, message: Spree.t('errors.messages.product_not_in_store'))
+ end
end
end
end
diff --git a/spree/core/app/models/spree/commission_rules/vendor_rule.rb b/spree/core/app/models/spree/commission_rules/vendor_rule.rb
index d63a34eac4..84184a7d5b 100644
--- a/spree/core/app/models/spree/commission_rules/vendor_rule.rb
+++ b/spree/core/app/models/spree/commission_rules/vendor_rule.rb
@@ -10,7 +10,7 @@ module Spree
preference :vendor_ids, :array, default: [],
parse_on_set: normalize_id_preference(
klass: Spree::Vendor,
- scope: ->(rule) { rule.store&.vendors || Spree::Vendor.none }
+ scope: ->(rule) { rule.store.vendors }
)
# @return [Array<Spree::Vendor>]
diff --git a/spree/core/app/models/spree/promotion/actions/create_line_items.rb b/spree/core/app/models/spree/promotion/actions/create_line_items.rb
index 7494e000c7..a762124f83 100644
--- a/spree/core/app/models/spree/promotion/actions/create_line_items.rb
+++ b/spree/core/app/models/spree/promotion/actions/create_line_items.rb
@@ -98,10 +98,8 @@ module Spree
private
- # An action built before its promotion is attached has no store to
- # read, so it resolves nothing rather than the whole catalog.
def promotion_variants_scope
- promotion&.store&.variants || Spree::Variant.none
+ promotion.store.variants
end
# Handles the creation, updating, and pruning of promotion action
diff --git a/spree/core/app/models/spree/promotion/rules/category.rb b/spree/core/app/models/spree/promotion/rules/category.rb
index b971d4a968..cfc2155f5e 100644
--- a/spree/core/app/models/spree/promotion/rules/category.rb
+++ b/spree/core/app/models/spree/promotion/rules/category.rb
@@ -138,10 +138,8 @@ module Spree
private
- # A rule built before its promotion is attached has no store to read,
- # so it resolves nothing rather than falling back to every category.
def promotion_categories_scope
- promotion&.store&.categories || Spree::Category.none
+ promotion.store.categories
end
# IDs of categories in rule including all their children
diff --git a/spree/core/app/models/spree/promotion/rules/channel.rb b/spree/core/app/models/spree/promotion/rules/channel.rb
index a695ddb54b..a91b88c351 100644
--- a/spree/core/app/models/spree/promotion/rules/channel.rb
+++ b/spree/core/app/models/spree/promotion/rules/channel.rb
@@ -11,7 +11,7 @@ module Spree
preference :channel_ids, :array, default: [],
parse_on_set: normalize_id_preference(
klass: Spree::Channel,
- scope: ->(rule) { rule.promotion&.store&.channels || Spree::Channel.none }
+ scope: ->(rule) { rule.promotion.store.channels }
)
def applicable?(promotable)
diff --git a/spree/core/app/models/spree/promotion/rules/customer_group.rb b/spree/core/app/models/spree/promotion/rules/customer_group.rb
index 6cc55bcdfd..d2b945a2b6 100644
--- a/spree/core/app/models/spree/promotion/rules/customer_group.rb
+++ b/spree/core/app/models/spree/promotion/rules/customer_group.rb
@@ -12,7 +12,7 @@ module Spree
preference :customer_group_ids, :array, default: [],
parse_on_set: normalize_id_preference(
klass: Spree::CustomerGroup,
- scope: ->(rule) { rule.promotion&.store&.customer_groups || Spree::CustomerGroup.none }
+ scope: ->(rule) { rule.promotion.store.customer_groups }
)
def applicable?(promotable)
diff --git a/spree/core/app/models/spree/promotion/rules/market.rb b/spree/core/app/models/spree/promotion/rules/market.rb
index 0992322d22..b8e55ec4ac 100644
--- a/spree/core/app/models/spree/promotion/rules/market.rb
+++ b/spree/core/app/models/spree/promotion/rules/market.rb
@@ -11,7 +11,7 @@ module Spree
preference :market_ids, :array, default: [],
parse_on_set: normalize_id_preference(
klass: Spree::Market,
- scope: ->(rule) { rule.promotion&.store&.markets || Spree::Market.none }
+ scope: ->(rule) { rule.promotion.store.markets }
)
def applicable?(promotable)
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index 0157514170..738309570c 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -922,6 +922,7 @@ en:
oidc_missing_code: The identity provider did not return an authorization code.
oidc_requires_redirect: This provider requires signing in through its own login page.
pickup_point_not_found: pickup point could not be found with this provider
+ product_not_in_store: must all belong to this store
profile_does_not_accept_provider: "%{provider} deliveries cannot join the %{profile} profile"
promotion_discount_not_editable: promotion discounts are managed by recalculation and cannot be edited
rate_provider_requires_shipping: "%{provider} quotes shipments, so this method must ship to an address"
diff --git a/spree/core/spec/models/spree/commission_rules/product_rule_spec.rb b/spree/core/spec/models/spree/commission_rules/product_rule_spec.rb
new file mode 100644
index 0000000000..b03fed9685
--- /dev/null
+++ b/spree/core/spec/models/spree/commission_rules/product_rule_spec.rb
@@ -0,0 +1,43 @@
+require 'spec_helper'
+
+RSpec.describe Spree::CommissionRules::ProductRule do
+ let(:store) { @default_store }
+ let(:rate) { create(:commission_rate, store: store) }
+ let(:rule) { described_class.create!(commission_rate: rate) }
+
+ # The sibling rules name their records through an id preference, which
+ # raises on a foreign id. This one holds records, so it refuses on save
+ # instead — a rate must not be narrowed to another marketplace's catalog.
+ it 'refuses a product from another store' do
+ rule.products = [create(:product, store: create(:store))]
+
+ expect(rule).not_to be_valid
+ expect(rule.errors[:products].first).to match(/belong to this store/)
+ end
+
+ it 'accepts a product from its own store' do
+ rule.products = [create(:product, store: store)]
+
+ expect(rule).to be_valid
+ end
+
+ it 'refuses a mixed list' do
+ rule.products = [create(:product, store: store), create(:product, store: create(:store))]
+
+ expect(rule).not_to be_valid
+ end
+
+ it 'matches a sale of a chosen product' do
+ product = create(:product, store: store)
+ rule.products = [product]
+ rule.save!
+
+ context = Spree::Commissions::Context.new(
+ vendor: create(:vendor, :approved, store: store),
+ order: create(:order, store: store),
+ line_item: create(:line_item, variant: product.default_variant)
+ )
+
+ expect(rule.applicable?(context)).to be true
+ end
+end