Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Spree
# Mailing after {Spree::Order} is confirmed.
class OrderConfirmationMailerSubscriber
include Omnes::Subscriber

handle :order_finalized,
with: :send_confirmation_email,
id: :spree_order_mailer_send_confirmation_email

# Sends confirmation email to the user.
#
# @param event [Omnes::UnstructuredEvent]
def send_confirmation_email(event)
order = event[:order]
unless order.confirmation_delivered?
Spree::Config.order_mailer_class.confirm_email(order).deliver_later
order.update_column(:confirmation_delivered, true)
end
end
end
end
19 changes: 4 additions & 15 deletions core/app/subscribers/spree/order_mailer_subscriber.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
# frozen_string_literal: true

module Spree
# Mailing after events on a {Spree::Order}
class OrderMailerSubscriber
include Omnes::Subscriber

handle :order_finalized,
with: :send_confirmation_email,
id: :spree_order_mailer_send_confirmation_email

# Sends confirmation email to the user
#
# @param event [Omnes::UnstructuredEvent]
def send_confirmation_email(event)
order = event[:order]
unless order.confirmation_delivered?
Spree::Config.order_mailer_class.confirm_email(order).deliver_later
order.update_column(:confirmation_delivered, true)
end
end
def send_confirmation_email(_event) = nil
deprecate send_confirmation_email:
"Use Spree::OrderConfirmationMailerSubscriber#send_confirmation_email instead",
deprecator: Spree.deprecator

def send_reimbursement_email(_event) = nil
deprecate send_reimbursement_email:
Expand Down
2 changes: 1 addition & 1 deletion core/lib/spree/core/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Engine < ::Rails::Engine
reimbursement_errored
].each { |event_name| Spree::Bus.register(event_name) }

Spree::OrderMailerSubscriber.new.subscribe_to(Spree::Bus)
Spree::OrderConfirmationMailerSubscriber.new.subscribe_to(Spree::Bus)
Spree::ReimbursementMailerSubscriber.new.subscribe_to(Spree::Bus)
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require 'rails_helper'
require 'action_mailer'

RSpec.describe Spree::OrderConfirmationMailerSubscriber do
let(:bus) { Omnes::Bus.new }

before do
bus.register(:order_finalized)

described_class.new.subscribe_to(bus)
end

describe 'on :on_order_finalized' do
it 'sends confirmation email' do
order = create(:order, confirmation_delivered: false)

expect(Spree::OrderMailer).to receive(:confirm_email).and_call_original

bus.publish(:order_finalized, order:)
end

it 'marks the order as having the confirmation email delivered' do
order = create(:order, confirmation_delivered: false)

bus.publish(:order_finalized, order:)

expect(order.confirmation_delivered).to be(true)
end

it "doesn't send confirmation email if already sent" do
order = build(:order, confirmation_delivered: true)

expect(Spree::OrderMailer).not_to receive(:confirm_email)

bus.publish(:order_finalized, order:)
end
end
end
30 changes: 8 additions & 22 deletions core/spec/subscribers/spree/order_mailer_subscriber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,15 @@
described_class.new.subscribe_to(bus)
end

describe 'on :on_order_finalized' do
it 'sends confirmation email' do
order = create(:order, confirmation_delivered: false)
describe "#send_confirmation_email" do
subject { described_class.new.send_confirmation_email({}) }

expect(Spree::OrderMailer).to receive(:confirm_email).and_call_original

bus.publish(:order_finalized, order:)
end

it 'marks the order as having the confirmation email delivered' do
order = create(:order, confirmation_delivered: false)

bus.publish(:order_finalized, order:)

expect(order.confirmation_delivered).to be(true)
end

it "doesn't send confirmation email if already sent" do
order = build(:order, confirmation_delivered: true)

expect(Spree::OrderMailer).not_to receive(:confirm_email)

bus.publish(:order_finalized, order:)
it "results in a deprecation warning" do
if ENV["SOLIDUS_RAISE_DEPRECATIONS"]
expect { subject }.to raise_error(ActiveSupport::DeprecationException)
else
expect(subject).to eq nil
end
end
end

Expand Down
Loading