-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Omnes the default for pub/sub events
We add an Omnes subscriber class for order mailing work, equivalent to the legacy subscriber module. From now on, new applications will use Omnes by default. We also run our test suite with it, but we still allow running with the legacy adapter through a `USE_LEGACY_EVENTS` environment variable (and use it in a new CI job).
- Loading branch information
1 parent
690a198
commit 0751249
Showing
12 changed files
with
167 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# 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 | ||
|
||
handle :reimbursement_reimbursed, | ||
with: :send_reimbursement_email, | ||
id: :spree_order_mailer_send_reimbursement_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 | ||
|
||
# Sends reimbursement email to the user | ||
# | ||
# @param event [Omnes::UnstructuredEvent] | ||
def send_reimbursement_email(event) | ||
reimbursement = event[:reimbursement] | ||
Spree::Config.reimbursement_mailer_class.reimbursement_email(reimbursement.id).deliver_later | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
core/spec/subscribers/spree/order_mailer_subscriber_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require 'action_mailer' | ||
|
||
RSpec.describe Spree::OrderMailerSubscriber do | ||
let(:bus) { Omnes::Bus.new } | ||
|
||
before do | ||
bus.register(:order_finalized) | ||
bus.register(:reimbursement_reimbursed) | ||
|
||
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: order) | ||
end | ||
|
||
it 'marks the order as having the confirmation email delivered' do | ||
order = create(:order, confirmation_delivered: false) | ||
|
||
bus.publish(:order_finalized, order: 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: order) | ||
end | ||
end | ||
|
||
describe 'on :reimbursement_reimbursed' do | ||
it 'sends reimbursement email' do | ||
reimbursement = build(:reimbursement) | ||
|
||
expect(Spree::ReimbursementMailer).to receive(:reimbursement_email).and_call_original | ||
|
||
bus.publish(:reimbursement_reimbursed, reimbursement: reimbursement) | ||
end | ||
end | ||
end |