Skip to content

Commit

Permalink
Convert existing mailer calls to use new classes
Browse files Browse the repository at this point in the history
Removes service param from mailer calls and replaces it with the appropriate mailer class. Adds a method to return the correct mailer class in service objects and refactors tests to accommodate this change.
  • Loading branch information
Kizr committed Oct 11, 2024
1 parent 98bc704 commit 08cd617
Show file tree
Hide file tree
Showing 20 changed files with 77 additions and 72 deletions.
20 changes: 20 additions & 0 deletions app/services/application_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,24 @@ def call
def self.call(...)
new(...).call
end

private

def user_mailer_class(service)
case service
when :placements
Placements::UserMailer
when :claims
Claims::UserMailer
end
end

def support_user_mailer_class(service)
case service
when :placements
Placements::SupportUserMailer
when :claims
Claims::SupportUserMailer
end
end
end
2 changes: 1 addition & 1 deletion app/services/claims/claim/create_draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def call

def send_claim_created_support_notification_email
claim.school_users.each do |user|
UserMailer.with(service: :claims)
Claims::UserMailer
.claim_created_support_notification(claim, user).deliver_later
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/claims/claim/submit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def call
attr_reader :claim, :user

def send_claim_submitted_notification_email
UserMailer.with(service: :claims)
Claims::UserMailer
.claim_submitted_notification(user, claim).deliver_later
end

Expand Down
2 changes: 1 addition & 1 deletion app/services/placements/partnerships/notify/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Create < Notify

def notify_users
partner_organisation_users.each do |user|
UserMailer.with(service: :placements)
::Placements::UserMailer
.partnership_created_notification(
user, @source_organisation, @partner_organisation
).deliver_later
Expand Down
2 changes: 1 addition & 1 deletion app/services/placements/partnerships/notify/remove.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Remove < Notify

def notify_users
partner_organisation_users.each do |user|
UserMailer.with(service: :placements)
::Placements::UserMailer
.partnership_destroyed_notification(
user, @source_organisation, @partner_organisation
).deliver_later
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Assign < NotifyProvider

def notify_users
@provider.users.each do |user|
UserMailer.with(service: :placements)
::Placements::UserMailer
.placement_provider_assigned_notification(
user, @provider, @placement
).deliver_later
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Remove < NotifyProvider

def notify_users
@provider.users.each do |user|
UserMailer.with(service: :placements)
::Placements::UserMailer
.placement_provider_removed_notification(
user, @provider, @placement
).deliver_later
Expand Down
2 changes: 1 addition & 1 deletion app/services/support_user/invite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ def initialize(support_user:)
end

def call
SupportUserMailer.with(service: support_user.service).support_user_invitation(support_user).deliver_later
support_user_mailer_class(support_user.service).support_user_invitation(support_user).deliver_later
end
end
2 changes: 1 addition & 1 deletion app/services/support_user/remove.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def call
private

def send_email_notification
SupportUserMailer.with(service: support_user.service).support_user_removal_notification(support_user).deliver_later
support_user_mailer_class(support_user.service).support_user_removal_notification(support_user).deliver_later
end
end
2 changes: 1 addition & 1 deletion app/services/user/invite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ def initialize(user:, organisation:)
attr_reader :user, :organisation, :service

def call
UserMailer.with(service: user.service).user_membership_created_notification(user, organisation).deliver_later
user_mailer_class(user.service).user_membership_created_notification(user, organisation).deliver_later
end
end
2 changes: 1 addition & 1 deletion app/services/user/remove.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(user:, organisation:)

def call
user_membership.destroy!
UserMailer.with(service: user.service).user_membership_destroyed_notification(user, organisation).deliver_later
user_mailer_class(user.service).user_membership_destroyed_notification(user, organisation).deliver_later
end

private
Expand Down
5 changes: 2 additions & 3 deletions spec/services/claims/claim/create_draft_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
create(:claims_user, email: "[email protected]", user_memberships: [create(:user_membership, organisation: school)])

mailer_stub = class_double("mailer")
expect(UserMailer).to receive(:with).with(any_args).twice.and_return(mailer_stub)
expect(mailer_stub).to receive(:claim_created_support_notification).twice.and_return(mailer_stub)
expect(Claims::UserMailer).to receive(:claim_created_support_notification).twice.and_return(mailer_stub)
expect(mailer_stub).to receive(:deliver_later).twice

expect { draft_service }.to change(claim, :reference).from(nil).to("123")
Expand All @@ -40,7 +39,7 @@
service = described_class.call(claim: submitted_claim)

expect(service).to be_nil
expect(UserMailer).not_to receive(:with).with(any_args)
expect(Claims::UserMailer).not_to receive(:with).with(any_args)
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/services/claims/claim/submit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
allow(SecureRandom).to receive(:random_number).with(99_999_999).and_return(123)

expect { submit_service }.to change(claim, :reference).from(nil).to("123")
.and(have_enqueued_mail(UserMailer, :claim_submitted_notification).once)
.and(have_enqueued_mail(Claims::UserMailer, :claim_submitted_notification).once)
.and(have_enqueued_job(SlackNotifier::Message::DeliveryJob).once)

expect(claim.status).to eq("submitted")
Expand All @@ -32,7 +32,7 @@
service = described_class.call(claim: claim_with_reference, user:)

expect { service }.to not_change(claim, :reference)
.and(not_have_enqueued_mail(UserMailer, :claim_submitted_notification))
.and(not_have_enqueued_mail(Claims::UserMailer, :claim_submitted_notification))
.and(not_have_enqueued_job(SlackNotifier::Message::DeliveryJob))
end
end
Expand All @@ -43,7 +43,7 @@
service = described_class.call(claim: submitted_claim, user:)

expect { service }.to not_change(claim, :reference)
.and(not_have_enqueued_mail(UserMailer, :claim_submitted_notification))
.and(not_have_enqueued_mail(Claims::UserMailer, :claim_submitted_notification))
.and(not_have_enqueued_job(SlackNotifier::Message::DeliveryJob))

expect(service).to be_nil
Expand All @@ -57,7 +57,7 @@
allow(SecureRandom).to receive(:random_number).with(99_999_999).and_return(123, 456)

expect { submit_service }.to change(claim, :reference).from(nil).to("456")
.and(have_enqueued_mail(UserMailer, :claim_submitted_notification).once)
.and(have_enqueued_mail(Claims::UserMailer, :claim_submitted_notification).once)
.and(have_enqueued_job(SlackNotifier::Message::DeliveryJob).once)

expect(claim.status).to eq("submitted")
Expand Down
29 changes: 12 additions & 17 deletions spec/services/placements/partnerships/notify/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@

it "sends a notification email to every user belonging to the provider" do
expect { partnership_notify_creation }.to have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:partnership_created_notification,
).with(
params: { service: :placements },
args: [user_1, school, provider],
user_1, school, provider
).and have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:partnership_created_notification,
).with(
params: { service: :placements },
args: [user_2, school, provider],
user_2, school, provider
)
end
end
Expand All @@ -57,27 +55,24 @@

it "sends a notification email to every placements user belonging to the school" do
expect { partnership_notify_creation }.to have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:partnership_created_notification,
).with(
params: { service: :placements },
args: [user_1, provider, school],
user_1, provider, school
).and have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:partnership_created_notification,
).with(
params: { service: :placements },
args: [user_2, provider, school],
user_2, provider, school
)
end

it "does not send a notification email to users not belonging to the placements service" do
expect { partnership_notify_creation }.not_to have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:partnership_created_notification,
).with(
params: { service: :placements },
args: [user_3, provider, school],
user_3, provider, school
)
end
end
Expand Down Expand Up @@ -105,7 +100,7 @@

it "does not send a notification email" do
expect { partnership_notify_creation }.not_to have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:partnership_created_notification,
)
end
Expand All @@ -129,7 +124,7 @@

it "does not send a notification email" do
expect { partnership_notify_creation }.not_to have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:partnership_created_notification,
)
end
Expand Down
29 changes: 12 additions & 17 deletions spec/services/placements/partnerships/notify/remove_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@

it "sends a notification email to every user belonging to the provider" do
expect { partnership_notify_removal }.to have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:partnership_destroyed_notification,
).with(
params: { service: :placements },
args: [user_1, school, provider],
user_1, school, provider
).and have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:partnership_destroyed_notification,
).with(
params: { service: :placements },
args: [user_2, school, provider],
user_2, school, provider
)
end
end
Expand All @@ -60,27 +58,24 @@

it "sends a notification email to every user belonging to the school" do
expect { partnership_notify_removal }.to have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:partnership_destroyed_notification,
).with(
params: { service: :placements },
args: [user_1, provider, school],
user_1, provider, school
).and have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:partnership_destroyed_notification,
).with(
params: { service: :placements },
args: [user_2, provider, school],
user_2, provider, school
)
end

it "does not send a notification email to users not belonging to the school" do
expect { partnership_notify_removal }.not_to have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:partnership_destroyed_notification,
).with(
params: { service: :placements },
args: [user_3, provider, school],
user_3, provider, school
)
end
end
Expand Down Expand Up @@ -108,7 +103,7 @@

it "does not send a notification email" do
expect { partnership_notify_removal }.not_to have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:partnership_destroyed_notification,
)
end
Expand All @@ -132,7 +127,7 @@

it "does not send a notification email" do
expect { partnership_notify_removal }.not_to have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:partnership_destroyed_notification,
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@

it "sends a notification email to every user belonging to the provider" do
expect { notify_provider_service }.to have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:placement_provider_assigned_notification,
).with(
params: { service: :placements },
args: [user_1, provider, placement],
user_1, provider, placement
).and have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:placement_provider_assigned_notification,
).with(
params: { service: :placements },
args: [user_2, provider, placement],
user_2, provider, placement
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@

it "sends a notification email to every user belonging to the provider" do
expect { notify_provider_service }.to have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:placement_provider_removed_notification,
).with(
params: { service: :placements },
args: [user_1, provider, placement],
user_1, provider, placement
).and have_enqueued_mail(
UserMailer,
Placements::UserMailer,
:placement_provider_removed_notification,
).with(
params: { service: :placements },
args: [user_2, provider, placement],
user_2, provider, placement
)
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/services/support_user/invite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
let(:support_user) { create(:claims_support_user) }

it "enqueues an invitation email" do
expect { invite_support_user }.to have_enqueued_mail(SupportUserMailer, :support_user_invitation)
.with(params: { service: :claims }, args: [support_user])
expect { invite_support_user }.to have_enqueued_mail(Claims::SupportUserMailer, :support_user_invitation)
.with(support_user)
end
end

context "when given a valid Placements Support User" do
let(:support_user) { create(:placements_support_user) }

it "enqueues an invitation email" do
expect { invite_support_user }.to have_enqueued_mail(SupportUserMailer, :support_user_invitation)
.with(params: { service: :placements }, args: [support_user])
expect { invite_support_user }.to have_enqueued_mail(Placements::SupportUserMailer, :support_user_invitation)
.with(support_user)
end
end
end
Expand Down
Loading

0 comments on commit 08cd617

Please sign in to comment.