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
Implement subscribers into emails gem
Damian Legawiec19877f664b5e
diff --git a/emails/app/models/spree/order/emails.rb b/emails/app/models/spree/order/emails.rbdeleted file mode 100644index 8d81c5fc9c..0000000000--- a/emails/app/models/spree/order/emails.rb+++ /dev/null@@ -1,30 +0,0 @@-module Spree- class Order < Spree.base_class- module Emails- def deliver_order_confirmation_email- if completed?- OrderMailer.confirm_email(id).deliver_later if store.prefers_send_consumer_transactional_emails?- update_column(:confirmation_delivered, true)- else- errors.add(:base, Spree.t(:order_email_resent_error))- end- end-- # Returns true if:- # 1. an email address is set for new order notifications AND- # 2. no notification for this order has been sent yet.- def deliver_store_owner_order_notification_email?- store.new_order_notifications_email.present? && !store_owner_notification_delivered?- end-- def deliver_store_owner_order_notification_email- OrderMailer.store_owner_notification_email(id).deliver_later- update_column(:store_owner_notification_delivered, true)- end-- def send_cancel_email- OrderMailer.cancel_email(id).deliver_later- end- end- end-enddiff --git a/emails/app/models/spree/reimbursement/emails.rb b/emails/app/models/spree/reimbursement/emails.rbdeleted file mode 100644index 404b599f85..0000000000--- a/emails/app/models/spree/reimbursement/emails.rb+++ /dev/null@@ -1,9 +0,0 @@-module Spree- class Reimbursement < Spree.base_class- module Emails- def send_reimbursement_email- Spree::ReimbursementMailer.reimbursement_email(id).deliver_later if store.prefers_send_consumer_transactional_emails?- end- end- end-enddiff --git a/emails/app/models/spree/shipment/emails.rb b/emails/app/models/spree/shipment/emails.rbdeleted file mode 100644index 7506b069e3..0000000000--- a/emails/app/models/spree/shipment/emails.rb+++ /dev/null@@ -1,9 +0,0 @@-module Spree- class Shipment < Spree.base_class- module Emails- def send_shipped_email- ShipmentMailer.shipped_email(@shipment.id).deliver_later if @shipment.store.prefers_send_consumer_transactional_emails?- end- end- end-enddiff --git a/emails/app/subscribers/spree/order_email_subscriber.rb b/emails/app/subscribers/spree/order_email_subscriber.rbnew file mode 100644index 0000000000..d862b3f801--- /dev/null+++ b/emails/app/subscribers/spree/order_email_subscriber.rb@@ -0,0 +1,51 @@+# frozen_string_literal: true++module Spree+ class OrderEmailSubscriber < Spree::Subscriber+ subscribes_to 'order.complete', 'order.cancel'++ on 'order.complete', :send_confirmation_email+ on 'order.cancel', :send_cancel_email++ private++ def send_confirmation_email(event)+ order = find_order(event)+ return unless order+ return if order.confirmation_delivered?++ store = order.store+ return unless store.prefers_send_consumer_transactional_emails?++ OrderMailer.confirm_email(order.id).deliver_later+ order.update_column(:confirmation_delivered, true)++ send_store_owner_notification(order) if should_notify_store_owner?(order)+ end++ def send_cancel_email(event)+ order = find_order(event)+ return unless order++ OrderMailer.cancel_email(order.id).deliver_later+ end++ def send_store_owner_notification(order)+ return if order.store_owner_notification_delivered?+ return if order.store.new_order_notifications_email.blank?++ OrderMailer.store_owner_notification_email(order.id).deliver_later+ order.update_column(:store_owner_notification_delivered, true)+ end++ def should_notify_store_owner?(order)+ order.store.new_order_notifications_email.present? &&+ !order.store_owner_notification_delivered?+ end++ def find_order(event)+ order_id = event.payload['id']+ Spree::Order.find_by(id: order_id)+ end+ end+enddiff --git a/emails/app/subscribers/spree/reimbursement_email_subscriber.rb b/emails/app/subscribers/spree/reimbursement_email_subscriber.rbnew file mode 100644index 0000000000..c1ddcd2965--- /dev/null+++ b/emails/app/subscribers/spree/reimbursement_email_subscriber.rb@@ -0,0 +1,24 @@+# frozen_string_literal: true++module Spree+ class ReimbursementEmailSubscriber < Spree::Subscriber+ subscribes_to 'reimbursement.reimbursed'++ def handle(event)+ reimbursement = find_reimbursement(event)+ return unless reimbursement++ store = reimbursement.store+ return unless store.prefers_send_consumer_transactional_emails?++ ReimbursementMailer.reimbursement_email(reimbursement.id).deliver_later+ end++ private++ def find_reimbursement(event)+ reimbursement_id = event.payload['id']+ Spree::Reimbursement.find_by(id: reimbursement_id)+ end+ end+enddiff --git a/emails/app/subscribers/spree/shipment_email_subscriber.rb b/emails/app/subscribers/spree/shipment_email_subscriber.rbnew file mode 100644index 0000000000..0fc5bff9bd--- /dev/null+++ b/emails/app/subscribers/spree/shipment_email_subscriber.rb@@ -0,0 +1,24 @@+# frozen_string_literal: true++module Spree+ class ShipmentEmailSubscriber < Spree::Subscriber+ subscribes_to 'shipment.ship'++ def handle(event)+ shipment = find_shipment(event)+ return unless shipment++ store = shipment.store+ return unless store.prefers_send_consumer_transactional_emails?++ ShipmentMailer.shipped_email(shipment.id).deliver_later+ end++ private++ def find_shipment(event)+ shipment_id = event.payload['id']+ Spree::Shipment.find_by(id: shipment_id)+ end+ end+enddiff --git a/emails/lib/spree/emails/engine.rb b/emails/lib/spree/emails/engine.rbindex 3662e9e765..af75b5b937 100644--- a/emails/lib/spree/emails/engine.rb+++ b/emails/lib/spree/emails/engine.rb@@ -5,6 +5,16 @@ module Spreeclass Engine < Rails::Engineisolate_namespace Spreeengine_name 'spree_emails'++ # Add app/subscribers to autoload paths+ config.paths.add 'app/subscribers', eager_load: true++ # Register email subscribers after initialization+ config.after_initialize do+ Spree::OrderEmailSubscriber.register!+ Spree::ShipmentEmailSubscriber.register!+ Spree::ReimbursementEmailSubscriber.register!+ endendendenddiff --git a/emails/spec/models/spree/order_spec.rb b/emails/spec/models/spree/order_spec.rbindex 3ecaa5448b..83f982db33 100644--- a/emails/spec/models/spree/order_spec.rb+++ b/emails/spec/models/spree/order_spec.rb@@ -1,10 +1,22 @@require 'spec_helper'describe Spree::Order, type: :model do+ include ActiveJob::TestHelper+let(:store) { @default_store }let(:user) { create(:user) }let(:order) { create(:order, user: user, store: store) }+ before do+ # Ensure subscriber is registered+ Spree::OrderEmailSubscriber.unregister!+ Spree::OrderEmailSubscriber.register!+ end++ after do+ Spree::OrderEmailSubscriber.unregister!+ end+context '#finalize!' dolet(:order) { create(:order, email: '[email protected]', store: store) }@@ -12,85 +24,77 @@ describe Spree::Order, type: :model doorder.update_column :state, 'complete'end- it 'sends an order confirmation email to customer' do+ it 'sends an order confirmation email to customer via subscriber' domail_message = double 'Mail::Message'expect(Spree::OrderMailer).to receive(:confirm_email).with(order.id).and_return mail_messageexpect(mail_message).to receive :deliver_later- order.finalize!++ perform_enqueued_jobs(only: Spree::Events::SubscriberJob) do+ order.finalize!+ endendcontext 'when send_consumer_transactional_emails store setting is set to false' dobefore do- allow(order.store).to receive(:prefers_send_consumer_transactional_emails?).and_return(false)+ # Update store preferences in DB since subscriber reloads order+ order.store.update!(preferences: order.store.preferences.merge(send_consumer_transactional_emails: false))endit 'does not send order confirmation email to customer' doexpect(Spree::OrderMailer).not_to receive(:confirm_email)- order.finalize!++ perform_enqueued_jobs(only: Spree::Events::SubscriberJob) do+ order.finalize!+ endendend- it 'sets confirmation delivered when finalizing' do+ it 'sets confirmation delivered when finalizing via subscriber' do+ allow(Spree::OrderMailer).to receive(:confirm_email).and_return(double(deliver_later: true))+ allow(Spree::OrderMailer).to receive(:store_owner_notification_email).and_return(double(deliver_later: true))+expect(order.confirmation_delivered?).to be false- order.finalize!- expect(order.confirmation_delivered?).to be true++ perform_enqueued_jobs(only: Spree::Events::SubscriberJob) do+ order.finalize!+ end++ expect(order.reload.confirmation_delivered?).to be trueendit 'does not send duplicate confirmation emails' do- allow(order).to receive_messages(confirmation_delivered?: true)+ order.update_column(:confirmation_delivered, true)expect(Spree::OrderMailer).not_to receive(:confirm_email)- order.finalize!++ perform_enqueued_jobs(only: Spree::Events::SubscriberJob) do+ order.finalize!+ endendcontext 'new order notifications' doit 'sends a new order notification email to store owner when notification email address is set' domail_message = double 'Mail::Message'- allow(store).to receive(:new_order_notifications_email).and_return('[email protected]')+ allow(Spree::OrderMailer).to receive(:confirm_email).and_return(double(deliver_later: true))+ # Update store in DB since subscriber reloads order+ store.update!(new_order_notifications_email: '[email protected]')expect(Spree::OrderMailer).to receive(:store_owner_notification_email).with(order.id).and_return mail_messageexpect(mail_message).to receive :deliver_later- order.finalize!++ perform_enqueued_jobs(only: Spree::Events::SubscriberJob) do+ order.finalize!+ endendit 'does not send a new order notification email to store owner when notification email address is blank' do- allow(store).to receive(:new_order_notifications_email).and_return(nil)+ allow(Spree::OrderMailer).to receive(:confirm_email).and_return(double(deliver_later: true))+ # Update store in DB since subscriber reloads order+ store.update!(new_order_notifications_email: nil)- mail_message = double 'Mail::Message'expect(Spree::OrderMailer).to_not receive(:store_owner_notification_email)- order.finalize!- end- end- end-- describe '#deliver_order_confirmation_email' do- let(:order) { create(:completed_order_with_totals, email: '[email protected]', store: store) }- context 'when send_consumer_transactional_emails store setting is enabled' do- before do- allow(order.store).to receive(:prefers_send_consumer_transactional_emails?).and_return(true)- end-- it 'sends order confirmation email' do- mail_message = double 'Mail::Message'- expect(Spree::OrderMailer).to receive(:confirm_email).with(order.id).and_return(mail_message)- expect(mail_message).to receive(:deliver_later)- order.deliver_order_confirmation_email- end- end-- context 'when send_consumer_transactional_emails store setting is disabled' do- before do- allow(order.store).to receive(:prefers_send_consumer_transactional_emails?).and_return(false)- end-- it 'does not send order confirmation email' do- expect(Spree::OrderMailer).not_to receive(:confirm_email)- order.deliver_order_confirmation_email- end-- it 'still sets confirmation_delivered to true' do- expect(order.confirmation_delivered?).to be false- order.deliver_order_confirmation_email- expect(order.confirmation_delivered?).to be true+ perform_enqueued_jobs(only: Spree::Events::SubscriberJob) do+ order.finalize!+ endendendend@@ -122,7 +126,7 @@ describe Spree::Order, type: :model doallow_any_instance_of(Spree::OrderUpdater).to receive(:update_adjustment_total).and_return(10)end- it 'sends a cancel email' do+ it 'sends a cancel email via subscriber' do# Stub methods that cause side-effects in this testallow(shipment).to receive(:cancel!)allow(order).to receive :restock_items!@@ -133,7 +137,11 @@ describe Spree::Order, type: :model domail_message}expect(mail_message).to receive :deliver_later- order.cancel!++ perform_enqueued_jobs(only: Spree::Events::SubscriberJob) do+ order.cancel!+ end+expect(order_id).to eq(order.id)endenddiff --git a/emails/spec/models/spree/reimbursement_spec.rb b/emails/spec/models/spree/reimbursement_spec.rbindex f14ce041c1..bb1c33abc3 100644--- a/emails/spec/models/spree/reimbursement_spec.rb+++ b/emails/spec/models/spree/reimbursement_spec.rb@@ -1,8 +1,14 @@require 'spec_helper'describe Spree::Reimbursement, type: :model do+ include ActiveJob::TestHelper+describe '#perform!' do- subject { reimbursement.perform! }+ subject do+ perform_enqueued_jobs(only: Spree::Events::SubscriberJob) do+ reimbursement.perform!+ end+ endlet!(:adjustments) { [] } # placeholder to ensure it gets run prior the "before" at this level@@ -28,9 +34,17 @@ describe Spree::Reimbursement, type: :model dobefore docustomer_return.save!return_item.accept!++ # Ensure subscriber is registered+ Spree::ReimbursementEmailSubscriber.unregister!+ Spree::ReimbursementEmailSubscriber.register!+ end++ after do+ Spree::ReimbursementEmailSubscriber.unregister!end- it 'triggers the reimbursement mailer to be sent' do+ it 'triggers the reimbursement mailer to be sent via subscriber' doexpect(Spree::ReimbursementMailer).to receive(:reimbursement_email).with(reimbursement.id) { double(deliver_later: true) }subjectenddiff --git a/emails/spec/models/spree/shipment_spec.rb b/emails/spec/models/spree/shipment_spec.rbindex d7c8b2bec2..6e23ade148 100644--- a/emails/spec/models/spree/shipment_spec.rb+++ b/emails/spec/models/spree/shipment_spec.rb@@ -1,6 +1,8 @@require 'spec_helper'describe Spree::Shipment, type: :model do+ include ActiveJob::TestHelper+let(:order) { create(:order) }let(:shipping_method) { create(:shipping_method, name: 'UPS') }let(:shipment) { create(:shipment, cost: 1, state: 'pending', stock_location: create(:stock_location), order: order) }@@ -13,6 +15,14 @@ describe Spree::Shipment, type: :model dotouch_later: falseallow(shipment).to receive_messages shipping_method: shipping_method++ # Ensure subscriber is registered+ Spree::ShipmentEmailSubscriber.unregister!+ Spree::ShipmentEmailSubscriber.register!+ end++ after do+ Spree::ShipmentEmailSubscriber.unregister!end['ready', 'canceled'].each do |state|@@ -22,7 +32,7 @@ describe Spree::Shipment, type: :model doallow(shipment).to receive_messages(require_inventory: false, update_order: true, state: state)end- it 'sends a shipment email' do+ it 'sends a shipment email via subscriber' domail_message = double 'Mail::Message'shipment_id = nilexpect(Spree::ShipmentMailer).to receive(:shipped_email) { |*args|@@ -32,18 +42,26 @@ describe Spree::Shipment, type: :model doexpect(mail_message).to receive :deliver_laterallow_any_instance_of(Spree::ShipmentHandler).to receive(:update_order_shipment_state)- shipment.ship!+ perform_enqueued_jobs(only: Spree::Events::SubscriberJob) do+ shipment.ship!+ end+expect(shipment_id).to eq(shipment.id)endcontext 'when send_consumer_transactional_emails store setting is set to false' dobefore do- allow(shipment.store).to receive(:prefers_send_consumer_transactional_emails?).and_return(false)+ # Update store preferences in DB since subscriber reloads shipment+ shipment.store.update!(preferences: shipment.store.preferences.merge(send_consumer_transactional_emails: false))endit 'does not trigger the shipment mailer to be sent' do+ allow_any_instance_of(Spree::ShipmentHandler).to receive(:update_order_shipment_state)expect(Spree::ShipmentMailer).not_to receive(:shipped_email)- shipment.ship!++ perform_enqueued_jobs(only: Spree::Events::SubscriberJob) do+ shipment.ship!+ endendendenddiff --git a/emails/spec/subscribers/spree/order_email_subscriber_spec.rb b/emails/spec/subscribers/spree/order_email_subscriber_spec.rbnew file mode 100644index 0000000000..681842e58c--- /dev/null+++ b/emails/spec/subscribers/spree/order_email_subscriber_spec.rb@@ -0,0 +1,118 @@+# frozen_string_literal: true++require 'spec_helper'++RSpec.describe Spree::OrderEmailSubscriber do+ include ActiveJob::TestHelper++ let(:store) { create(:store, new_order_notifications_email: '[email protected]') }+ let(:order) { create(:completed_order_with_totals, store: store) }++ def publish_event(event_name)+ perform_enqueued_jobs(only: Spree::Events::SubscriberJob) do+ Spree::Events.publish(+ event_name,+ { 'id' => order.id }+ )+ end+ end++ before do+ store.update!(preferences: store.preferences.merge(send_consumer_transactional_emails: true))+ # Unregister first to avoid duplicate subscriptions from engine initialization+ described_class.unregister!+ described_class.register!+ end++ after do+ described_class.unregister!+ end++ describe 'order.complete event' do+ it 'sends confirmation email' do+ expect(Spree::OrderMailer).to receive(:confirm_email).with(order.id).and_return(double(deliver_later: true))+ allow(Spree::OrderMailer).to receive(:store_owner_notification_email).and_return(double(deliver_later: true))++ publish_event('order.complete')+ end++ it 'marks confirmation as delivered' do+ allow(Spree::OrderMailer).to receive(:confirm_email).and_return(double(deliver_later: true))+ allow(Spree::OrderMailer).to receive(:store_owner_notification_email).and_return(double(deliver_later: true))++ expect { publish_event('order.complete') }.to change { order.reload.confirmation_delivered }.from(false).to(true)+ end++ it 'sends store owner notification email' do+ allow(Spree::OrderMailer).to receive(:confirm_email).and_return(double(deliver_later: true))+ expect(Spree::OrderMailer).to receive(:store_owner_notification_email).with(order.id).and_return(double(deliver_later: true))++ publish_event('order.complete')+ end++ it 'marks store owner notification as delivered' do+ allow(Spree::OrderMailer).to receive(:confirm_email).and_return(double(deliver_later: true))+ allow(Spree::OrderMailer).to receive(:store_owner_notification_email).and_return(double(deliver_later: true))++ order.update_column(:store_owner_notification_delivered, false)+ expect { publish_event('order.complete') }.to change { order.reload.store_owner_notification_delivered }.from(false).to(true)+ end++ context 'when confirmation already delivered' do+ before { order.update_column(:confirmation_delivered, true) }++ it 'does not send confirmation email again' do+ expect(Spree::OrderMailer).not_to receive(:confirm_email)++ publish_event('order.complete')+ end+ end++ context 'when store does not prefer transactional emails' do+ before do+ store.update!(preferences: store.preferences.merge(send_consumer_transactional_emails: false))+ end++ it 'does not send confirmation email' do+ expect(Spree::OrderMailer).not_to receive(:confirm_email)++ publish_event('order.complete')+ end+ end++ context 'when store owner notification email is blank' do+ before { store.update!(new_order_notifications_email: nil) }++ it 'does not send store owner notification email' do+ allow(Spree::OrderMailer).to receive(:confirm_email).and_return(double(deliver_later: true))+ expect(Spree::OrderMailer).not_to receive(:store_owner_notification_email)++ publish_event('order.complete')+ end+ end++ context 'when order not found' do+ it 'does not raise an error' do+ order.destroy++ expect { publish_event('order.complete') }.not_to raise_error+ end+ end+ end++ describe 'order.cancel event' do+ it 'sends cancel email' do+ expect(Spree::OrderMailer).to receive(:cancel_email).with(order.id).and_return(double(deliver_later: true))++ publish_event('order.cancel')+ end++ context 'when order not found' do+ it 'does not raise an error' do+ order.destroy++ expect { publish_event('order.cancel') }.not_to raise_error+ end+ end+ end+enddiff --git a/emails/spec/subscribers/spree/reimbursement_email_subscriber_spec.rb b/emails/spec/subscribers/spree/reimbursement_email_subscriber_spec.rbnew file mode 100644index 0000000000..8481e0bfc5--- /dev/null+++ b/emails/spec/subscribers/spree/reimbursement_email_subscriber_spec.rb@@ -0,0 +1,56 @@+# frozen_string_literal: true++require 'spec_helper'++RSpec.describe Spree::ReimbursementEmailSubscriber do+ include ActiveJob::TestHelper++ let(:reimbursement) { create(:reimbursement) }+ let(:store) { reimbursement.store }++ def publish_event(event_name, reimbursement_id = reimbursement.id)+ perform_enqueued_jobs(only: Spree::Events::SubscriberJob) do+ Spree::Events.publish(+ event_name,+ { 'id' => reimbursement_id }+ )+ end+ end++ before do+ store.update!(preferences: store.preferences.merge(send_consumer_transactional_emails: true))+ # Unregister first to avoid duplicate subscriptions from engine initialization+ described_class.unregister!+ described_class.register!+ end++ after do+ described_class.unregister!+ end++ describe 'reimbursement.reimbursed event' do+ it 'sends reimbursement email' do+ expect(Spree::ReimbursementMailer).to receive(:reimbursement_email).with(reimbursement.id).and_return(double(deliver_later: true))++ publish_event('reimbursement.reimbursed')+ end++ context 'when store does not prefer transactional emails' do+ before do+ store.update!(preferences: store.preferences.merge(send_consumer_transactional_emails: false))+ end++ it 'does not send reimbursement email' do+ expect(Spree::ReimbursementMailer).not_to receive(:reimbursement_email)++ publish_event('reimbursement.reimbursed')+ end+ end++ context 'when reimbursement not found' do+ it 'does not raise an error' do+ expect { publish_event('reimbursement.reimbursed', -1) }.not_to raise_error+ end+ end+ end+enddiff --git a/emails/spec/subscribers/spree/shipment_email_subscriber_spec.rb b/emails/spec/subscribers/spree/shipment_email_subscriber_spec.rbnew file mode 100644index 0000000000..7941c75f1f--- /dev/null+++ b/emails/spec/subscribers/spree/shipment_email_subscriber_spec.rb@@ -0,0 +1,59 @@+# frozen_string_literal: true++require 'spec_helper'++RSpec.describe Spree::ShipmentEmailSubscriber do+ include ActiveJob::TestHelper++ let(:store) { create(:store) }+ let(:order) { create(:completed_order_with_totals, store: store) }+ let(:shipment) { order.shipments.first }++ def publish_event(event_name)+ perform_enqueued_jobs(only: Spree::Events::SubscriberJob) do+ Spree::Events.publish(+ event_name,+ { 'id' => shipment.id }+ )+ end+ end++ before do+ store.update!(preferences: store.preferences.merge(send_consumer_transactional_emails: true))+ # Unregister first to avoid duplicate subscriptions from engine initialization+ described_class.unregister!+ described_class.register!+ end++ after do+ described_class.unregister!+ end++ describe 'shipment.ship event' do+ it 'sends shipped email' do+ expect(Spree::ShipmentMailer).to receive(:shipped_email).with(shipment.id).and_return(double(deliver_later: true))++ publish_event('shipment.ship')+ end++ context 'when store does not prefer transactional emails' do+ before do+ store.update!(preferences: store.preferences.merge(send_consumer_transactional_emails: false))+ end++ it 'does not send shipped email' do+ expect(Spree::ShipmentMailer).not_to receive(:shipped_email)++ publish_event('shipment.ship')+ end+ end++ context 'when shipment not found' do+ it 'does not raise an error' do+ shipment.destroy++ expect { publish_event('shipment.ship') }.not_to raise_error+ end+ end+ end+end