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
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.rbindex e03b950ebb..b61a940f45 100644--- a/core/app/models/spree/adjustment.rb+++ b/core/app/models/spree/adjustment.rb@@ -101,7 +101,10 @@ module Spreeamount = 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?+amountenddiff --git a/core/app/models/spree/coupon_code.rb b/core/app/models/spree/coupon_code.rbindex 9e9feea30b..74055f82a5 100644--- a/core/app/models/spree/coupon_code.rb+++ b/core/app/models/spree/coupon_code.rb@@ -11,7 +11,7 @@ module Spreescope :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: truebelongs_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.rbindex 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 Spreeif promotion.multi_codes?coupon_code = promotion.coupon_codes.find_by(order: order)coupon_code&.remove_from_order+ else+ promotion.touchendremove_promotion_adjustments(promotion)diff --git a/core/spec/models/spree/adjustment_spec.rb b/core/spec/models/spree/adjustment_spec.rbindex 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 dosubjectexpect(adjustment.eligible).to eq trueend++ it 'touches the promotion' do+ expect { subject }.to change { promotion.reload.updated_at }+ endendcontext "the promotion is not eligible" do@@ -216,6 +220,10 @@ describe Spree::Adjustment, type: :model dosubjectexpect(adjustment.eligible).to eq falseend++ it 'touches the promotion' do+ expect { subject }.to change { promotion.reload.updated_at }+ endendendenddiff --git a/core/spec/models/spree/promotion_handler/coupon_spec.rb b/core/spec/models/spree/promotion_handler/coupon_spec.rbindex 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 Spreeexpect(order.promotions).to be_emptyexpect(order.line_item_adjustments).to be_emptyend++ it 'touches the promotion' do+ expect { subject.remove(coupon_code.code) }.to change { promotion.reload.updated_at }+ endendendend@@ -404,13 +408,21 @@ module Spreelet!(: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) doSpree::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.rbindex 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 doexpect(promotion.activate(payload)).to be trueend- 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 orderend- 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)endendend