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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Enforce country exclusivity per market (#13697)
Damian Legawiec064f6a25814b
diff --git a/spree/admin/app/controllers/spree/admin/markets_controller.rb b/spree/admin/app/controllers/spree/admin/markets_controller.rbindex b2fab1fca7..beb1a0b9a3 100644--- a/spree/admin/app/controllers/spree/admin/markets_controller.rb+++ b/spree/admin/app/controllers/spree/admin/markets_controller.rb@@ -10,6 +10,20 @@ module Spreedef load_data@countries = current_store.countries_with_shipping_coverage++ # Exclude countries already assigned to other markets in the store+ if @object&.persisted?+ taken_country_ids = Spree::MarketCountry.joins(:market)+ .where(spree_markets: { store_id: current_store.id, deleted_at: nil })+ .where.not(market_id: @object.id)+ .pluck(:country_id)+ else+ taken_country_ids = Spree::MarketCountry.joins(:market)+ .where(spree_markets: { store_id: current_store.id, deleted_at: nil })+ .pluck(:country_id)+ end++ @countries = @countries.where.not(id: taken_country_ids) if taken_country_ids.any?enddef permitted_resource_paramsdiff --git a/spree/core/app/models/spree/market.rb b/spree/core/app/models/spree/market.rbindex cce5b28542..97b9686444 100644--- a/spree/core/app/models/spree/market.rb+++ b/spree/core/app/models/spree/market.rb@@ -44,8 +44,7 @@ module Spreejoins(:market_countries).where(store_id: store.id).where(spree_market_countries: { country_id: country.id })- .order(:position)- .first+ .takeend# Returns the default market for a store, or falls back to the first by positiondiff --git a/spree/core/app/models/spree/market_country.rb b/spree/core/app/models/spree/market_country.rbindex 6ffb47c04c..77b3ffa036 100644--- a/spree/core/app/models/spree/market_country.rb+++ b/spree/core/app/models/spree/market_country.rb@@ -8,6 +8,7 @@ module Spreevalidates :market, :country, presence: truevalidates :country_id, uniqueness: { scope: :market_id }validate :country_covered_by_shipping_zone+ validate :country_unique_per_storeprivate@@ -21,5 +22,21 @@ module Spreeerrors.add(:country, :not_in_shipping_zone)endend++ def country_unique_per_store+ return if market.blank? || country.blank?++ store = market.store+ return if store.blank?++ existing = self.class.joins(:market)+ .where(country_id: country_id)+ .where(spree_markets: { store_id: store.id, deleted_at: nil })+ .where.not(id: id)++ if existing.exists?+ errors.add(:country, :already_in_market)+ end+ endendenddiff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.ymlindex 1aef7e4371..2df63c972d 100644--- a/spree/core/config/locales/en.yml+++ b/spree/core/config/locales/en.yml@@ -292,6 +292,7 @@ en:spree/market_country:attributes:country:+ already_in_market: is already assigned to another market in this storenot_in_shipping_zone: is not covered by any shipping zone. Please set up a shipping zone first.spree/payment:attributes:diff --git a/spree/core/spec/models/spree/market_country_spec.rb b/spree/core/spec/models/spree/market_country_spec.rbindex 945182e03a..0be1a84e6c 100644--- a/spree/core/spec/models/spree/market_country_spec.rb+++ b/spree/core/spec/models/spree/market_country_spec.rb@@ -62,5 +62,26 @@ RSpec.describe Spree::MarketCountry, type: :model doendendend++ describe '#country_unique_per_store' do+ let(:store) { create(:store) }+ let(:country) { create(:country) }+ let(:market1) { create(:market, store: store, countries: [country]) }++ it 'prevents assigning same country to another market in the same store' do+ market1 # ensure it exists+ market2 = create(:market, store: store)+ market_country = Spree::MarketCountry.new(market: market2, country: country)+ expect(market_country).not_to be_valid+ expect(market_country.errors[:country]).to include(/already assigned to another market/)+ end++ it 'allows same country in markets of different stores' do+ market1 # ensure it exists+ other_store = create(:store)+ market2 = create(:market, store: other_store, countries: [country])+ expect(market2).to be_valid+ end+ endendend