From e87573186e6c3b99dfa42fa31e2fed9e68834bb5 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 16 May 2024 17:11:59 +0200 Subject: [PATCH 1/4] Add subscriber: OrderPromotionSubscriber This adds a subscriber that removes all connected order promotions upon emptying the order. A future commit will emit the event instead of directly removing the join table records. --- .../spree/order_promotion_subscriber.rb | 20 +++++++++++++++ .../spree/order_promotion_subscriber_spec.rb | 25 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 core/app/subscribers/spree/order_promotion_subscriber.rb create mode 100644 core/spec/subscribers/spree/order_promotion_subscriber_spec.rb diff --git a/core/app/subscribers/spree/order_promotion_subscriber.rb b/core/app/subscribers/spree/order_promotion_subscriber.rb new file mode 100644 index 00000000000..56156a74bb7 --- /dev/null +++ b/core/app/subscribers/spree/order_promotion_subscriber.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Spree + # Clears promotions from an emptied order + class OrderPromotionSubscriber + include Omnes::Subscriber + + handle :order_emptied, + with: :clear_order_promotions, + id: :spree_order_promotion_clear_order_promotions + + # Clears all promotions from the order + # + # @param event [Omnes::UnstructuredEvent] + def clear_order_promotions(event) + order = event[:order] + order.order_promotions.destroy_all + end + end +end diff --git a/core/spec/subscribers/spree/order_promotion_subscriber_spec.rb b/core/spec/subscribers/spree/order_promotion_subscriber_spec.rb new file mode 100644 index 00000000000..131b83a6bd7 --- /dev/null +++ b/core/spec/subscribers/spree/order_promotion_subscriber_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Spree::OrderPromotionSubscriber do + let(:bus) { Omnes::Bus.new } + + before do + bus.register(:order_emptied) + + described_class.new.subscribe_to(bus) + end + + describe 'on :order_emptied' do + it 'clears connected promotions' do + promotion = create(:promotion) + order = create(:order) + order.promotions << promotion + expect(order.order_promotions).not_to be_empty + + bus.publish(:order_emptied, order: order) + expect(order.order_promotions).to be_empty + end + end +end From ea2a4c0b6a462165ddae5565365876a143dbd962 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 16 May 2024 17:21:17 +0200 Subject: [PATCH 2/4] Emit Event and handle emptying the order using Omnes --- core/app/models/spree/order.rb | 2 +- core/lib/spree/core/engine.rb | 2 ++ core/spec/models/spree/order_spec.rb | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb index 85c8621c533..d33a6ab3358 100644 --- a/core/app/models/spree/order.rb +++ b/core/app/models/spree/order.rb @@ -450,7 +450,7 @@ def empty! line_items.destroy_all adjustments.destroy_all shipments.destroy_all - order_promotions.destroy_all + Spree::Bus.publish :order_emptied, order: self recalculate end diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb index 58525660adf..31084acc8b4 100644 --- a/core/lib/spree/core/engine.rb +++ b/core/lib/spree/core/engine.rb @@ -56,6 +56,7 @@ class Engine < ::Rails::Engine Spree::Bus.clear %i[ + order_emptied order_finalized order_recalculated reimbursement_reimbursed @@ -63,6 +64,7 @@ class Engine < ::Rails::Engine ].each { |event_name| Spree::Bus.register(event_name) } Spree::OrderMailerSubscriber.new.subscribe_to(Spree::Bus) + Spree::OrderPromotionSubscriber.new.subscribe_to(Spree::Bus) end end diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb index 7d8d65bb80a..cc4a9a5c3f6 100644 --- a/core/spec/models/spree/order_spec.rb +++ b/core/spec/models/spree/order_spec.rb @@ -269,7 +269,7 @@ expect(order.line_items).to be_empty expect(order.shipments).to be_empty expect(order.adjustments).to be_empty - expect(order.promotions).to be_empty + expect(order.reload.promotions).to be_empty expect(order.item_total).to eq 0 expect(order.item_count).to eq 0 expect(order.shipment_total).to eq 0 From 64b6e994674bd185f2ce3f9209359c2394b576ad Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Tue, 27 Feb 2024 10:52:47 +0100 Subject: [PATCH 3/4] Register OrderPromotionSubscriber in LegacyPromotions This is where it belongs. --- core/lib/spree/core/engine.rb | 1 - core/spec/models/spree/order_spec.rb | 13 +--- .../lib/solidus_legacy_promotions/engine.rb | 6 ++ .../spec/models/spree/order_spec.rb | 62 ++++++++++++++++++- 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb index 31084acc8b4..b6aa20b36cd 100644 --- a/core/lib/spree/core/engine.rb +++ b/core/lib/spree/core/engine.rb @@ -64,7 +64,6 @@ class Engine < ::Rails::Engine ].each { |event_name| Spree::Bus.register(event_name) } Spree::OrderMailerSubscriber.new.subscribe_to(Spree::Bus) - Spree::OrderPromotionSubscriber.new.subscribe_to(Spree::Bus) end end diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb index cc4a9a5c3f6..a53ece6279f 100644 --- a/core/spec/models/spree/order_spec.rb +++ b/core/spec/models/spree/order_spec.rb @@ -6,14 +6,6 @@ let(:store) { create(:store) } let(:user) { create(:user, email: "solidus@example.com") } let(:order) { create(:order, user: user, store: store) } - let(:promotion) do - FactoryBot.create( - :promotion, - :with_order_adjustment, - code: "discount" - ) - end - let(:code) { promotion.codes.first } context '#store' do it { is_expected.to respond_to(:store) } @@ -249,15 +241,13 @@ before do create(:line_item, order: order) create(:shipment, order: order) - create(:adjustment, adjustable: order, order: order) - promotion.activate(order: order, promotion_code: code) + create(:adjustment, source: nil, adjustable: order, order: order) order.recalculate # Make sure we are asserting changes expect(order.line_items).not_to be_empty expect(order.shipments).not_to be_empty expect(order.adjustments).not_to be_empty - expect(order.promotions).not_to be_empty expect(order.item_total).not_to eq 0 expect(order.item_count).not_to eq 0 expect(order.shipment_total).not_to eq 0 @@ -269,7 +259,6 @@ expect(order.line_items).to be_empty expect(order.shipments).to be_empty expect(order.adjustments).to be_empty - expect(order.reload.promotions).to be_empty expect(order.item_total).to eq 0 expect(order.item_count).to eq 0 expect(order.shipment_total).to eq 0 diff --git a/legacy_promotions/lib/solidus_legacy_promotions/engine.rb b/legacy_promotions/lib/solidus_legacy_promotions/engine.rb index 2f897653ee9..3157d5e5583 100644 --- a/legacy_promotions/lib/solidus_legacy_promotions/engine.rb +++ b/legacy_promotions/lib/solidus_legacy_promotions/engine.rb @@ -42,6 +42,12 @@ class Engine < ::Rails::Engine end end + initializer 'solidus_legacy_promotions.core.pub_sub', after: 'spree.core.pub_sub' do |app| + app.reloader.to_prepare do + Spree::OrderPromotionSubscriber.new.subscribe_to(Spree::Bus) + end + end + initializer "solidus_legacy_promotions.assets" do |app| app.config.assets.precompile << "solidus_legacy_promotions/manifest.js" end diff --git a/legacy_promotions/spec/models/spree/order_spec.rb b/legacy_promotions/spec/models/spree/order_spec.rb index f4f1c1dfb5b..2b24c6a0889 100644 --- a/legacy_promotions/spec/models/spree/order_spec.rb +++ b/legacy_promotions/spec/models/spree/order_spec.rb @@ -3,9 +3,13 @@ require "rails_helper" RSpec.describe Spree::Order do - let(:order) { create(:order) } + subject { described_class.new } + it { is_expected.to respond_to(:order_promotions) } + it { is_expected.to respond_to(:promotions) } context "#apply_shipping_promotions" do + let(:order) { build(:order) } + it "calls out to the Shipping promotion handler" do expect_any_instance_of(Spree::PromotionHandler::Shipping).to( receive(:activate) @@ -16,4 +20,60 @@ order.apply_shipping_promotions end end + + context "empty!" do + let!(:order) { create(:order) } + let(:promotion) do + FactoryBot.create( + :promotion, + :with_order_adjustment, + code: "discount" + ) + end + let(:code) { promotion.codes.first } + + before do + create(:line_item, order: order) + create(:shipment, order: order) + create(:adjustment, adjustable: order, order: order) + promotion.activate(order: order, promotion_code: code) + order.recalculate + + # Make sure we are asserting changes + expect(order.line_items).not_to be_empty + expect(order.shipments).not_to be_empty + expect(order.adjustments).not_to be_empty + expect(order.promotions).not_to be_empty + expect(order.item_total).not_to eq 0 + expect(order.item_count).not_to eq 0 + expect(order.shipment_total).not_to eq 0 + expect(order.adjustment_total).not_to eq 0 + end + + it "clears out line items, adjustments and update totals" do + order.empty! + expect(order.line_items).to be_empty + expect(order.shipments).to be_empty + expect(order.adjustments).to be_empty + expect(order.promotions).to be_empty + expect(order.item_total).to eq 0 + expect(order.item_count).to eq 0 + expect(order.shipment_total).to eq 0 + expect(order.adjustment_total).to eq 0 + end + end + + describe "order deletion" do + let(:order) { create(:order) } + let(:promotion) { create(:promotion) } + + subject { order.destroy } + before do + order.promotions << promotion + end + + it "deletes join table entries when deleting an order" do + expect { subject }.to change { Spree::OrderPromotion.count }.from(1).to(0) + end + end end From 914d1aaa1b68d1a659de867b01d1647db094a0d8 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 16 May 2024 17:30:30 +0200 Subject: [PATCH 4/4] Move OrderPromotionSubscriber to Legacy Promotions gem --- .../app/subscribers/spree/order_promotion_subscriber.rb | 0 .../spec/subscribers/spree/order_promotion_subscriber_spec.rb | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {core => legacy_promotions}/app/subscribers/spree/order_promotion_subscriber.rb (100%) rename {core => legacy_promotions}/spec/subscribers/spree/order_promotion_subscriber_spec.rb (100%) diff --git a/core/app/subscribers/spree/order_promotion_subscriber.rb b/legacy_promotions/app/subscribers/spree/order_promotion_subscriber.rb similarity index 100% rename from core/app/subscribers/spree/order_promotion_subscriber.rb rename to legacy_promotions/app/subscribers/spree/order_promotion_subscriber.rb diff --git a/core/spec/subscribers/spree/order_promotion_subscriber_spec.rb b/legacy_promotions/spec/subscribers/spree/order_promotion_subscriber_spec.rb similarity index 100% rename from core/spec/subscribers/spree/order_promotion_subscriber_spec.rb rename to legacy_promotions/spec/subscribers/spree/order_promotion_subscriber_spec.rb