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.

Fixed: added unique index on `spree_order_promotions` and uniqueness … (#13032)

Damian Legawieca98e5d76cadb
diff --git a/core/app/models/spree/order_promotion.rb b/core/app/models/spree/order_promotion.rb
index 0373f2ec69..e5bcaedd44 100644
--- a/core/app/models/spree/order_promotion.rb
+++ b/core/app/models/spree/order_promotion.rb
@@ -6,6 +6,9 @@ module Spree
delegate :name, :description, :code, :public_metadata, to: :promotion
delegate :currency, to: :order
+ validates :order, :promotion, presence: true
+ validates :order, uniqueness: { scope: :promotion }
+
extend Spree::DisplayMoney
money_methods :amount
diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rb
index 2f0344ce38..e452ca4821 100644
--- a/core/app/models/spree/promotion.rb
+++ b/core/app/models/spree/promotion.rb
@@ -147,8 +147,8 @@ module Spree
if action_taken
# connect to the order
# create the join_table entry.
- orders << order
- save
+ order.promotions << self unless order.promotions.include?(self)
+ order.save
end
action_taken
@@ -174,8 +174,8 @@ module Spree
if action_taken
# connect to the order
# create the join_table entry.
- orders << order
- save
+ order.promotions << self unless order.promotions.include?(self)
+ order.save
end
action_taken
diff --git a/core/app/models/spree/promotion_handler/cart.rb b/core/app/models/spree/promotion_handler/cart.rb
index c4a30d5e9b..9efdc4cdb8 100644
--- a/core/app/models/spree/promotion_handler/cart.rb
+++ b/core/app/models/spree/promotion_handler/cart.rb
@@ -39,7 +39,7 @@ module Spree
end
def promotion_scope
- ::Spree::Promotion.for_store(store)
+ store.promotions
end
end
end
diff --git a/core/app/models/spree/promotion_handler/coupon.rb b/core/app/models/spree/promotion_handler/coupon.rb
index 87c75f8f0d..6441b125b7 100644
--- a/core/app/models/spree/promotion_handler/coupon.rb
+++ b/core/app/models/spree/promotion_handler/coupon.rb
@@ -62,7 +62,7 @@ module Spree
promotion = order.promotions.with_coupon_code(coupon_code)
if promotion.present?
# Order promotion has to be destroyed before line item removing
- order.order_promotions.where(promotion_id: promotion.id).destroy_all
+ order.promotions.delete(promotion)
if promotion.multi_codes?
coupon_code = promotion.coupon_codes.find_by(order: order)
diff --git a/core/db/migrate/20250821211705_fix_unique_index_on_spree_order_promotions.rb b/core/db/migrate/20250821211705_fix_unique_index_on_spree_order_promotions.rb
new file mode 100644
index 0000000000..f642f6576c
--- /dev/null
+++ b/core/db/migrate/20250821211705_fix_unique_index_on_spree_order_promotions.rb
@@ -0,0 +1,20 @@
+class FixUniqueIndexOnSpreeOrderPromotions < ActiveRecord::Migration[7.2]
+ def change
+ remove_index :spree_order_promotions, name: 'index_spree_order_promotions_on_promotion_id_and_order_id', if_exists: true
+
+ # Remove duplicate records before adding unique index
+ duplicates = Spree::OrderPromotion.select(:promotion_id, :order_id).group(:promotion_id, :order_id).having('COUNT(*) > 1').reorder('').pluck(:promotion_id, :order_id)
+
+ duplicates.each do |duplicate_promotion_id, duplicate_order_id|
+ order_promotions = Spree::OrderPromotion.where(promotion_id: duplicate_promotion_id, order_id: duplicate_order_id)
+
+ order_promotions.each_with_index do |order_promotion, index|
+ next if index == 0 # Keep the first one unchanged
+
+ order_promotion.destroy
+ end
+ end
+
+ add_index :spree_order_promotions, [:promotion_id, :order_id], unique: true, name: 'index_spree_order_promotions_on_promotion_id_and_order_id'
+ end
+end
diff --git a/core/spec/models/spree/order_promotion_spec.rb b/core/spec/models/spree/order_promotion_spec.rb
index a9abf315a7..9df86a07ce 100644
--- a/core/spec/models/spree/order_promotion_spec.rb
+++ b/core/spec/models/spree/order_promotion_spec.rb
@@ -1,18 +1,16 @@
require 'spec_helper'
describe Spree::OrderPromotion, type: :model do
- subject { create(:order_promotion, order: order, promotion: promotion) }
+ subject { order.order_promotions.find_by(promotion: promotion) }
let(:order) { create(:order_with_line_items) }
let(:promotion) { create(:promotion_with_item_adjustment, code: 'test') }
- shared_context 'apply promo' do
- before do
- order.coupon_code = promotion.code
- Spree::PromotionHandler::Coupon.new(order).apply
- order.save!
- order.all_adjustments.promotion.update_all(amount: -5.0)
- end
+ before do
+ order.coupon_code = promotion.code
+ Spree::PromotionHandler::Coupon.new(order).apply
+ order.save!
+ order.all_adjustments.promotion.update_all(amount: -5.0)
end
context '#name' do
@@ -28,16 +26,12 @@ describe Spree::OrderPromotion, type: :model do
end
context '#amount' do
- include_context 'apply promo'
-
it 'equals sum of adjustments created by promotion' do
expect(subject.amount).to eq(-5.0)
end
end
context '#display_amount' do
- include_context 'apply promo'
-
it 'returns Spree::Money instance with amount value and proper currency' do
expect(subject.display_amount.to_s).to eq('-$5.00')
end
diff --git a/storefront/spec/features/checkout_steps_spec.rb b/storefront/spec/features/checkout_steps_spec.rb
index f361c371e0..ac90cec889 100644
--- a/storefront/spec/features/checkout_steps_spec.rb
+++ b/storefront/spec/features/checkout_steps_spec.rb
@@ -69,7 +69,7 @@ describe 'Checkout steps (address and delivery)' do
end
context 'with promotion', js: true do
- let(:promotion) { create(:promotion, code: :welcomepromo) }
+ let(:promotion) { create(:promotion, stores: [store], code: :welcomepromo) }
before do
order.update!(email: nil, user: nil)
@@ -110,7 +110,7 @@ describe 'Checkout steps (address and delivery)' do
end
context 'with coupon codes' do
- let!(:promotion) { create(:promotion, name: '10% OFF', code: nil, multi_codes: true, number_of_codes: 1) }
+ let!(:promotion) { create(:promotion, stores: [store], name: '10% OFF', code: nil, multi_codes: true, number_of_codes: 1) }
let!(:action) { Spree::Promotion::Actions::CreateAdjustment.create(promotion: promotion, calculator: calculator) }
let(:calculator) { create(:flat_percent_item_total_calculator, preferred_flat_percent: 10) }