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.

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.rb
index 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 Spree
def 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?
end
def permitted_resource_params
diff --git a/spree/core/app/models/spree/market.rb b/spree/core/app/models/spree/market.rb
index cce5b28542..97b9686444 100644
--- a/spree/core/app/models/spree/market.rb
+++ b/spree/core/app/models/spree/market.rb
@@ -44,8 +44,7 @@ module Spree
joins(:market_countries)
.where(store_id: store.id)
.where(spree_market_countries: { country_id: country.id })
- .order(:position)
- .first
+ .take
end
# Returns the default market for a store, or falls back to the first by position
diff --git a/spree/core/app/models/spree/market_country.rb b/spree/core/app/models/spree/market_country.rb
index 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 Spree
validates :market, :country, presence: true
validates :country_id, uniqueness: { scope: :market_id }
validate :country_covered_by_shipping_zone
+ validate :country_unique_per_store
private
@@ -21,5 +22,21 @@ module Spree
errors.add(:country, :not_in_shipping_zone)
end
end
+
+ 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
+ end
end
end
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index 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 store
not_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.rb
index 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 do
end
end
end
+
+ 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
+ end
end
end