Skip to content

Commit

Permalink
Use EmailDeliveryJob
Browse files Browse the repository at this point in the history
This updates the email delivery to use the new `EmailDeliveryJob` added
in the previous commit rather than using `ActionMailer`.

By switching to this job we get automatic retry behaviour on GOV.UK
Notify server errors, and tracking of message IDs.
  • Loading branch information
thomasleese committed Jan 21, 2025
1 parent a9cc298 commit 05bd1a7
Show file tree
Hide file tree
Showing 16 changed files with 215 additions and 298 deletions.
24 changes: 17 additions & 7 deletions app/controllers/concerns/consent_form_mailer_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ module ConsentFormMailerConcern
extend ActiveSupport::Concern

def send_consent_form_confirmation(consent_form)
mailer = ConsentMailer.with(consent_form:)

if consent_form.contact_injection?
mailer.confirmation_injection.deliver_later
EmailDeliveryJob.perform_later(
:consent_confirmation_injection,
consent_form:
)
elsif consent_form.consent_refused?
mailer.confirmation_refused.deliver_later
EmailDeliveryJob.perform_later(
:consent_confirmation_refused,
consent_form:
)

if consent_form.parent_phone_receive_updates
SMSDeliveryJob.perform_later(
Expand All @@ -18,13 +22,19 @@ def send_consent_form_confirmation(consent_form)
)
end
elsif consent_form.needs_triage?
mailer.confirmation_triage.deliver_later
EmailDeliveryJob.perform_later(
:consent_confirmation_triage,
consent_form:
)
elsif consent_form.actual_upcoming_session ==
consent_form.organisation.generic_clinic_session ||
consent_form.actual_upcoming_session&.completed?
mailer.confirmation_clinic.deliver_later
EmailDeliveryJob.perform_later(
:consent_confirmation_clinic,
consent_form:
)
else
mailer.confirmation_given.deliver_later
EmailDeliveryJob.perform_later(:consent_confirmation_given, consent_form:)

if consent_form.parent_phone_receive_updates
SMSDeliveryJob.perform_later(:consent_confirmation_given, consent_form:)
Expand Down
12 changes: 6 additions & 6 deletions app/controllers/concerns/triage_mailer_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ def send_triage_confirmation(patient_session, consent)
params = { consent:, session:, sent_by: current_user }

if vaccination_will_happen?(patient_session, consent)
TriageMailer.with(params).vaccination_will_happen.deliver_later
EmailDeliveryJob.perform_later(:triage_vaccination_will_happen, **params)
elsif vaccination_wont_happen?(patient_session, consent)
TriageMailer.with(params).vaccination_wont_happen.deliver_later
EmailDeliveryJob.perform_later(:triage_vaccination_wont_happen, **params)
elsif vaccination_at_clinic?(patient_session, consent)
TriageMailer.with(params).vaccination_at_clinic.deliver_later
EmailDeliveryJob.perform_later(:triage_vaccination_at_clinic, **params)
elsif consent.triage_needed?
ConsentMailer.with(params).confirmation_triage.deliver_later
EmailDeliveryJob.perform_later(:consent_confirmation_triage, **params)
elsif consent.response_refused?
ConsentMailer.with(params).confirmation_refused.deliver_later
EmailDeliveryJob.perform_later(:consent_confirmation_refused, **params)

if consent.parent.phone_receive_updates
SMSDeliveryJob.perform_later(:consent_confirmation_refused, **params)
end
elsif consent.response_given?
ConsentMailer.with(params).confirmation_given.deliver_later
EmailDeliveryJob.perform_later(:consent_confirmation_given, **params)

if consent.parent.phone_receive_updates
SMSDeliveryJob.perform_later(:consent_confirmation_given, **params)
Expand Down
29 changes: 14 additions & 15 deletions app/controllers/concerns/vaccination_mailer_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@ def send_vaccination_confirmation(vaccination_record)
parents = parents_for_vaccination_mailer(vaccination_record)
return if parents.empty?

mailer_action =
template_name =
if vaccination_record.administered?
:confirmation_administered
:vaccination_confirmation_administered
else
:confirmation_not_administered
:vaccination_confirmation_not_administered
end

text_template_name = :"vaccination_#{mailer_action}"

parents.each do |parent|
params = { parent:, vaccination_record:, sent_by: try(:current_user) }

if parent.email.present?
VaccinationMailer.with(params).public_send(mailer_action).deliver_later
end
EmailDeliveryJob.perform_later(template_name, **params)

if parent.phone.present? && parent.phone_receive_updates
SMSDeliveryJob.perform_later(text_template_name, **params)
if parent.phone_receive_updates
SMSDeliveryJob.perform_later(template_name, **params)
end
end
end
Expand All @@ -33,12 +29,15 @@ def send_vaccination_deletion(vaccination_record)
parents = parents_for_vaccination_mailer(vaccination_record)
return if parents.empty?

parents.each do |parent|
params = { parent:, vaccination_record:, sent_by: try(:current_user) }
sent_by = try(:current_user)

if parent.email.present?
VaccinationMailer.with(params).deleted.deliver_later
end
parents.each do |parent|
EmailDeliveryJob.perform_later(
:vaccination_deleted,
parent:,
vaccination_record:,
sent_by:
)
end
end

Expand Down
21 changes: 11 additions & 10 deletions app/models/consent_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,25 @@ def self.create_and_send!(

is_school = session.location.school?

mailer_action = (is_school ? :"school_#{type}" : :"clinic_#{type}")
mail_template =
:"consent_#{(is_school ? :"school_#{type}" : :"clinic_#{type}")}"

text_template =
if type == :request
:"consent_#{mailer_action}"
mail_template
elsif is_school
:consent_school_reminder
end

parents.each do |parent|
unless parent.email.nil?
ConsentMailer
.with(parent:, patient:, programme:, session:, sent_by: current_user)
.send(mailer_action)
.deliver_later
end

next if parent.phone.nil?
EmailDeliveryJob.perform_later(
mail_template,
parent:,
patient:,
programme:,
session:,
sent_by: current_user
)

SMSDeliveryJob.perform_later(
text_template,
Expand Down
38 changes: 8 additions & 30 deletions app/models/session_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,42 +79,20 @@ def self.create_and_send!(

if type == :school_reminder
contacts.each do |consent|
if consent.parent.email.present?
SessionMailer
.with(consent:, patient_session:, sent_by: current_user)
.school_reminder
.deliver_later
end
params = { consent:, patient_session:, sent_by: current_user }

unless consent.parent.phone.present? &&
consent.parent.phone_receive_updates
next
end
EmailDeliveryJob.perform_later(:session_school_reminder, **params)

next unless consent.parent.phone_receive_updates

SMSDeliveryJob.perform_later(
:session_school_reminder,
consent:,
patient_session:,
sent_by: current_user
)
SMSDeliveryJob.perform_later(:session_school_reminder, **params)
end
else
contacts.each do |parent|
if parent.email.present?
SessionMailer
.with(parent:, patient_session:, sent_by: current_user)
.send(type)
.deliver_later
end

next if parent.phone.blank?
params = { parent:, patient_session:, sent_by: current_user }

SMSDeliveryJob.perform_later(
:"session_#{type}",
parent:,
patient_session:,
sent_by: current_user
)
EmailDeliveryJob.perform_later(:"session_#{type}", **params)
SMSDeliveryJob.perform_later(:"session_#{type}", **params)
end
end
end
Expand Down
35 changes: 15 additions & 20 deletions spec/controllers/concerns/consent_form_mailer_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
end

it "sends a confirmation email" do
expect { send_consent_form_confirmation }.to have_enqueued_mail(
ConsentMailer,
:confirmation_given
).with(params: { consent_form: }, args: [])
expect { send_consent_form_confirmation }.to have_delivered_email(
:consent_confirmation_given
).with(consent_form:)
end

it "sends a consent given text" do
Expand All @@ -27,10 +26,9 @@
before { consent_form.contact_injection = true }

it "sends an injection confirmation email" do
expect { send_consent_form_confirmation }.to have_enqueued_mail(
ConsentMailer,
:confirmation_injection
).with(params: { consent_form: }, args: [])
expect { send_consent_form_confirmation }.to have_delivered_email(
:consent_confirmation_injection
).with(consent_form:)
end

it "doesn't send a text" do
Expand All @@ -42,10 +40,9 @@
before { consent_form.response = :refused }

it "sends an confirmation refused email" do
expect { send_consent_form_confirmation }.to have_enqueued_mail(
ConsentMailer,
:confirmation_refused
).with(params: { consent_form: }, args: [])
expect { send_consent_form_confirmation }.to have_delivered_email(
:consent_confirmation_refused
).with(consent_form:)
end

it "sends a consent refused text" do
Expand All @@ -59,10 +56,9 @@
before { consent_form.health_answers.last.response = "yes" }

it "sends an confirmation needs triage email" do
expect { send_consent_form_confirmation }.to have_enqueued_mail(
ConsentMailer,
:confirmation_triage
).with(params: { consent_form: }, args: [])
expect { send_consent_form_confirmation }.to have_delivered_email(
:consent_confirmation_triage
).with(consent_form:)
end

it "doesn't send a text" do
Expand All @@ -85,10 +81,9 @@
end

it "sends an confirmation needs triage email" do
expect { send_consent_form_confirmation }.to have_enqueued_mail(
ConsentMailer,
:confirmation_clinic
).with(params: { consent_form: }, args: [])
expect { send_consent_form_confirmation }.to have_delivered_email(
:consent_confirmation_clinic
).with(consent_form:)
end

it "doesn't send a text" do
Expand Down
Loading

0 comments on commit 05bd1a7

Please sign in to comment.