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.

Touch `Spree::Promotion` record when the promotion is activated/deactivated (#12194)

Łukasz Adamczyk40814ef9616b
diff --git a/core/app/models/spree/adjustment.rb b/core/app/models/spree/adjustment.rb
index e03b950ebb..b61a940f45 100644
--- a/core/app/models/spree/adjustment.rb
+++ b/core/app/models/spree/adjustment.rb
@@ -101,7 +101,10 @@ module Spree
amount = source.compute_amount(target)
attributes = { amount: amount, updated_at: Time.current }
attributes[:eligible] = source.promotion.eligible?(target) if promotion?
+
update_columns(attributes)
+ source.promotion.touch if promotion?
+
amount
end
diff --git a/core/app/models/spree/coupon_code.rb b/core/app/models/spree/coupon_code.rb
index 9e9feea30b..74055f82a5 100644
--- a/core/app/models/spree/coupon_code.rb
+++ b/core/app/models/spree/coupon_code.rb
@@ -11,7 +11,7 @@ module Spree
scope :in_promotions, ->(promotion_ids) { where(promotion_id: promotion_ids) }
scope :not_in_promotions, ->(promotion_ids) { where.not(promotion_id: promotion_ids) }
- belongs_to :promotion, class_name: 'Spree::Promotion'
+ belongs_to :promotion, class_name: 'Spree::Promotion', touch: true
belongs_to :order, class_name: 'Spree::Order'
validates :code, presence: true, uniqueness: { scope: spree_base_uniqueness_scope }
diff --git a/core/app/models/spree/promotion_handler/coupon.rb b/core/app/models/spree/promotion_handler/coupon.rb
index b28a68cb03..c5ae20428a 100644
--- a/core/app/models/spree/promotion_handler/coupon.rb
+++ b/core/app/models/spree/promotion_handler/coupon.rb
@@ -34,6 +34,8 @@ module Spree
if promotion.multi_codes?
coupon_code = promotion.coupon_codes.find_by(order: order)
coupon_code&.remove_from_order
+ else
+ promotion.touch
end
remove_promotion_adjustments(promotion)
diff --git a/core/spec/models/spree/adjustment_spec.rb b/core/spec/models/spree/adjustment_spec.rb
index 1c3a4c3244..16913798e7 100644
--- a/core/spec/models/spree/adjustment_spec.rb
+++ b/core/spec/models/spree/adjustment_spec.rb
@@ -207,6 +207,10 @@ describe Spree::Adjustment, type: :model do
subject
expect(adjustment.eligible).to eq true
end
+
+ it 'touches the promotion' do
+ expect { subject }.to change { promotion.reload.updated_at }
+ end
end
context "the promotion is not eligible" do
@@ -216,6 +220,10 @@ describe Spree::Adjustment, type: :model do
subject
expect(adjustment.eligible).to eq false
end
+
+ it 'touches the promotion' do
+ expect { subject }.to change { promotion.reload.updated_at }
+ end
end
end
end
diff --git a/core/spec/models/spree/promotion_handler/coupon_spec.rb b/core/spec/models/spree/promotion_handler/coupon_spec.rb
index f497e4aebf..c28f21e515 100644
--- a/core/spec/models/spree/promotion_handler/coupon_spec.rb
+++ b/core/spec/models/spree/promotion_handler/coupon_spec.rb
@@ -367,6 +367,10 @@ module Spree
expect(order.promotions).to be_empty
expect(order.line_item_adjustments).to be_empty
end
+
+ it 'touches the promotion' do
+ expect { subject.remove(coupon_code.code) }.to change { promotion.reload.updated_at }
+ end
end
end
end
@@ -404,13 +408,21 @@ module Spree
let!(:order_2) { create(:order_with_line_items, line_items_count: 3, user: user) }
let!(:subject_2) { Spree::PromotionHandler::Coupon.new(order_2) }
let!(:promotion) do
- create(:promotion, name: 'promo',
- code: nil,
- usage_limit: 1,
- multi_codes: true,
- number_of_codes: 2,
- coupon_codes: [create(:coupon_code, code: 'first_one_time_code'), create(:coupon_code, code: 'second_one_time_code')])
+ create(
+ :promotion,
+ name: 'promo',
+ code: nil,
+ usage_limit: 1,
+ multi_codes: true,
+ number_of_codes: 2
+ )
end
+
+ before do
+ promotion.coupon_codes[0].update!(code: 'first_one_time_code')
+ promotion.coupon_codes[1].update!(code: 'second_one_time_code')
+ end
+
let!(:action) do
Spree::Promotion::Actions::CreateItemAdjustments.create(promotion: promotion,
calculator: calculator)
diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rb
index 1ba9701902..2c986b2be8 100644
--- a/core/spec/models/spree/promotion_spec.rb
+++ b/core/spec/models/spree/promotion_spec.rb
@@ -275,22 +275,33 @@ describe Spree::Promotion, type: :model do
expect(promotion.activate(payload)).to be true
end
- context 'keeps track of the orders' do
- context 'when activated' do
- it 'assigns the order' do
- expect(promotion.orders).to be_empty
- expect(promotion.activate(payload)).to be true
- expect(promotion.orders.first).to eql order
- end
+ context 'when activated' do
+ it 'assigns the order' do
+ expect(promotion.orders).to be_empty
+ expect(promotion.activate(payload)).to be true
+ expect(promotion.orders.first).to eql order
end
- context 'when not activated' do
- it 'will not assign the order' do
- order.state = 'complete'
- expect(promotion.orders).to be_empty
- expect(promotion.activate(payload)).to be_falsey
- expect(promotion.orders).to be_empty
- end
+ it 'touches the promotion' do
+ previous_updated_at = promotion.updated_at
+ expect(promotion.activate(payload)).to be true
+ expect(promotion.reload.updated_at).not_to eq(previous_updated_at)
+ end
+ end
+
+ context 'when not activated' do
+ before { order.state = 'complete' }
+
+ it "doesn't assign the order" do
+ expect(promotion.orders).to be_empty
+ expect(promotion.activate(payload)).to be_falsey
+ expect(promotion.orders).to be_empty
+ end
+
+ it "doesn't the promotion" do
+ previous_updated_at = promotion.updated_at
+ expect(promotion.activate(payload)).to be_falsey
+ expect(promotion.reload.updated_at).to eq(previous_updated_at)
end
end
end