Skip to content

Commit

Permalink
Add DeliverEmail
Browse files Browse the repository at this point in the history
This adds a service for encapsulating the logic related to sending
emails and recording the emails in the timeline. This allows us to send
emails in the fake data and record the correct time rather than adding
the timeline event in the Sidekiq process.
  • Loading branch information
thomasleese committed Apr 4, 2024
1 parent bbc1192 commit 4e5059a
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ def notify_teacher
return
end

TeacherMailer.with(application_form:).initial_checks_passed.deliver_later
DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :initial_checks_passed,
)
end

def unassign_assessor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ def update_request
)
end

TeacherMailer.with(application_form:).consent_requested.deliver_later
DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :consent_requested,
)
end

redirect_to [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ def submit
end
end

TeacherMailer.with(application_form:).consent_submitted.deliver_later
DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :consent_submitted,
)

redirect_to %i[teacher_interface application_form]
end
Expand Down
28 changes: 0 additions & 28 deletions app/lib/application_mailer_observer.rb

This file was deleted.

18 changes: 2 additions & 16 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ApplicationMailer < Mail::Notify::Mailer
default from: I18n.t("service.email.enquiries")

Expand All @@ -6,20 +8,4 @@ class ApplicationMailer < Mail::Notify::Mailer
"GOVUK_NOTIFY_TEMPLATE_ID_APPLICATION",
"95adafaf-0920-4623-bddc-340853c047af",
)

after_action :store_observer_metadata

def store_observer_metadata
mailer_class_name = self.class.name.demodulize
mailer_action_name = action_name
application_form_id = application_form.id

message.instance_variable_set(:@mailer_class_name, mailer_class_name)
message.instance_variable_set(:@mailer_action_name, mailer_action_name)
message.instance_variable_set(:@application_form_id, application_form_id)

message.class.send(:attr_reader, :mailer_class_name)
message.class.send(:attr_reader, :mailer_action_name)
message.class.send(:attr_reader, :application_form_id)
end
end
33 changes: 19 additions & 14 deletions app/models/application_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,22 +272,27 @@ def should_send_reminder_email?(name, number_of_reminders_sent)
def send_reminder_email(name, number_of_reminders_sent)
case name
when "consent"
TeacherMailer.with(application_form: self).consent_reminder.deliver_later
DeliverEmail.call(
application_form: self,
mailer: TeacherMailer,
action: :consent_reminder,
)
when "expiration"
TeacherMailer
.with(application_form: self, number_of_reminders_sent:)
.application_not_submitted
.deliver_later
DeliverEmail.call(
application_form: self,
mailer: TeacherMailer,
action: :application_not_submitted,
number_of_reminders_sent:,
)
when "references"
TeacherMailer
.with(
application_form: self,
number_of_reminders_sent:,
reference_requests:
reference_requests_not_yet_received_or_rejected.to_a,
)
.references_reminder
.deliver_later
DeliverEmail.call(
application_form: self,
mailer: TeacherMailer,
action: :references_reminder,
number_of_reminders_sent:,
reference_requests:
reference_requests_not_yet_received_or_rejected.to_a,
)
end
end

Expand Down
19 changes: 11 additions & 8 deletions app/models/further_information_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ def should_send_reminder_email?(_name, number_of_reminders_sent)
end

def send_reminder_email(_name, _number_of_reminders_sent)
TeacherMailer
.with(application_form:, further_information_request: self)
.further_information_reminder
.deliver_later
DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :further_information_reminder,
further_information_request: self,
)
end

def expires_after
Expand All @@ -63,10 +65,11 @@ def expires_after
end

def after_received(*)
TeacherMailer
.with(application_form:)
.further_information_received
.deliver_later
DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :further_information_received,
)
end

def after_expired(user:)
Expand Down
9 changes: 5 additions & 4 deletions app/models/professional_standing_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ def expires_after

def after_received(*)
if should_send_received_email?
TeacherMailer
.with(application_form:)
.professional_standing_received
.deliver_later
DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :professional_standing_received,
)
end
end

Expand Down
11 changes: 7 additions & 4 deletions app/models/reference_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,13 @@ def should_send_reminder_email?(_name, number_of_reminders_sent)
end

def send_reminder_email(_name, number_of_reminders_sent)
RefereeMailer
.with(reference_request: self, number_of_reminders_sent:)
.reference_reminder
.deliver_later
DeliverEmail.call(
application_form:,
mailer: RefereeMailer,
action: :reference_reminder,
reference_request: self,
number_of_reminders_sent:,
)
end

def expires_after
Expand Down
6 changes: 5 additions & 1 deletion app/services/award_qts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def call
ApplicationFormStatusUpdater.call(application_form:, user:)
end

TeacherMailer.with(application_form:).application_awarded.deliver_later
DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :application_awarded,
)
end

class InvalidState < StandardError
Expand Down
6 changes: 5 additions & 1 deletion app/services/decline_qts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ def call
CreateTimelineEvent.call("application_declined", application_form:, user:)
end

TeacherMailer.with(application_form:).application_declined.deliver_later
DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :application_declined,
)
end

private
Expand Down
36 changes: 36 additions & 0 deletions app/services/deliver_email.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

class DeliverEmail
include ServicePattern

def initialize(application_form:, mailer:, action:, **params)
@application_form = application_form
@mailer = mailer
@action = action
@params = params
end

def call
create_job
create_timeline_event
end

private

attr_reader :application_form, :mailer, :action, :params

def create_job
mailer.with(application_form:, **params).send(action).deliver_later
end

def create_timeline_event
CreateTimelineEvent.call(
"email_sent",
application_form:,
user: "Mailer",
mailer_class_name: mailer.name.demodulize,
mailer_action_name: action,
message_subject:,
)
end
end
9 changes: 5 additions & 4 deletions app/services/request_further_information.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ def create_and_request
end

def send_email
TeacherMailer
.with(application_form:)
.further_information_requested
.deliver_later
DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :further_information_requested,
)
end
end
21 changes: 15 additions & 6 deletions app/services/submit_application_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,27 @@ def call
ApplicationFormStatusUpdater.call(application_form:, user:)
end

TeacherMailer.with(application_form:).application_received.deliver_later
DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :application_received,
)

if !application_form.requires_preliminary_check &&
application_form.teaching_authority_provides_written_statement
TeacherMailer.with(application_form:).initial_checks_passed.deliver_later
DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :initial_checks_passed,
)
end

if region.teaching_authority_requires_submission_email
TeachingAuthorityMailer
.with(application_form:)
.application_submitted
.deliver_later
DeliverEmail.call(
application_form:,
mailer: TeachingAuthorityMailer,
action: :application_submitted,
)
end

FindApplicantInDQTJob.perform_later(
Expand Down
18 changes: 13 additions & 5 deletions app/services/update_work_history_contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,19 @@ def call
end

if email.present? && (reference_request = work_history.reference_request)
RefereeMailer.with(reference_request:).reference_requested.deliver_later
TeacherMailer
.with(application_form:, reference_requests: [reference_request])
.references_requested
.deliver_later
DeliverEmail.call(
application_form:,
mailer: RefereeMailer,
action: :reference_requested,
reference_request:,
)

DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :references_requested,
reference_requests: [reference_request],
)
end
end

Expand Down
17 changes: 12 additions & 5 deletions app/services/verify_assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,19 @@ def create_reference_requests

def send_reference_request_emails(reference_requests)
reference_requests.each do |reference_request|
RefereeMailer.with(reference_request:).reference_requested.deliver_later
DeliverEmail.call(
application_form:,
mailer: RefereeMailer,
action: :reference_requested,
reference_request:,
)
end

TeacherMailer
.with(application_form:, reference_requests:)
.references_requested
.deliver_later
DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :references_requested,
reference_requests:,
)
end
end
9 changes: 5 additions & 4 deletions lib/tasks/application_forms.rake
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ namespace :application_forms do
teacher = application_form.teacher
next unless teacher.application_form == application_form

TeacherMailer
.with(application_form:)
.application_from_ineligible_country
.deliver_now
DeliverEmail.call(
application_form:,
mailer: TeacherMailer,
action: :application_from_ineligible_country,
)
end
end

Expand Down
Loading

0 comments on commit 4e5059a

Please sign in to comment.