Skip to content

Commit

Permalink
Refactor TeacherMailer to take the application form
Browse files Browse the repository at this point in the history
Currently the TeacherMailer takes in the teacher as a parameter and then
extracts the most recent application form from it. This works in most
cases, but there are a few specific cases where this causes problems,
notably if the teacher has multiple application forms. In this case we
can end up choosing the incorrect application form and emails can end up
associated with the wrong application form.
  • Loading branch information
thomasleese committed Jan 18, 2024
1 parent 1d17bd9 commit e6af8e8
Show file tree
Hide file tree
Showing 23 changed files with 70 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ def notify_teacher
return
end

TeacherMailer
.with(teacher: application_form.teacher)
.initial_checks_passed
.deliver_later
TeacherMailer.with(application_form:).initial_checks_passed.deliver_later
end

def unassign_assessor
Expand Down
21 changes: 5 additions & 16 deletions app/mailers/teacher_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ class TeacherMailer < ApplicationMailer
include RegionHelper

before_action :set_application_form
before_action :set_further_information_request,
only: :further_information_reminder
before_action :set_further_information_requested, only: :application_declined

helper :application_form, :region

Expand Down Expand Up @@ -76,6 +73,8 @@ def further_information_requested
end

def further_information_reminder
@further_information_request = params[:further_information_request]

view_mail(
GOVUK_NOTIFY_TEMPLATE_ID,
to: teacher.email,
Expand Down Expand Up @@ -129,23 +128,13 @@ def references_requested

private

def teacher
params[:teacher]
def application_form
params[:application_form]
end

delegate :application_form, to: :teacher
delegate :assessment, :region, to: :application_form
delegate :assessment, :region, :teacher, to: :application_form

def set_application_form
@application_form = application_form
end

def set_further_information_request
@further_information_request = params[:further_information_request]
end

def set_further_information_requested
@further_information_requested =
assessment.further_information_requests.any?
end
end
4 changes: 2 additions & 2 deletions app/models/application_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,13 @@ def send_reminder_email(name, number_of_reminders_sent)
case name
when "expiration"
TeacherMailer
.with(teacher:, number_of_reminders_sent:)
.with(application_form: self, number_of_reminders_sent:)
.application_not_submitted
.deliver_later
when "references"
TeacherMailer
.with(
teacher:,
application_form: self,
number_of_reminders_sent:,
reference_requests:
reference_requests_not_yet_received_or_rejected.to_a,
Expand Down
7 changes: 5 additions & 2 deletions app/models/further_information_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def should_send_reminder_email?(_name, number_of_reminders_sent)

def send_reminder_email(_name, _number_of_reminders_sent)
TeacherMailer
.with(teacher:, further_information_request: self)
.with(application_form:, further_information_request: self)
.further_information_reminder
.deliver_later
end
Expand All @@ -63,7 +63,10 @@ def expires_after
end

def after_received(*)
TeacherMailer.with(teacher:).further_information_received.deliver_later
TeacherMailer
.with(application_form:)
.further_information_received
.deliver_later
end

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

def after_received(*)
if should_send_received_email?
TeacherMailer.with(teacher:).professional_standing_received.deliver_later
TeacherMailer
.with(application_form:)
.professional_standing_received
.deliver_later
end
end

Expand Down
9 changes: 5 additions & 4 deletions app/services/award_qts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ def call
raise MissingTRN if trn.blank?

ActiveRecord::Base.transaction do
teacher.update!(trn:, access_your_teaching_qualifications_url:)
application_form.teacher.update!(
trn:,
access_your_teaching_qualifications_url:,
)
application_form.update!(awarded_at: Time.zone.now)
ApplicationFormStatusUpdater.call(application_form:, user:)
end

TeacherMailer.with(teacher:).application_awarded.deliver_later
TeacherMailer.with(application_form:).application_awarded.deliver_later
end

class InvalidState < StandardError
Expand All @@ -44,6 +47,4 @@ class MissingTRN < StandardError
:user,
:trn,
:access_your_teaching_qualifications_url

delegate :teacher, to: :application_form
end
13 changes: 5 additions & 8 deletions app/services/create_further_information_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def call
requestable
end

TeacherMailer.with(teacher:).further_information_requested.deliver_later
TeacherMailer
.with(application_form:)
.further_information_requested
.deliver_later

further_information_request
end
Expand All @@ -38,11 +41,5 @@ def call

attr_reader :assessment, :user

def application_form
@application_form ||= assessment.application_form
end

def teacher
@teacher ||= application_form.teacher
end
delegate :application_form, to: :assessment
end
3 changes: 1 addition & 2 deletions app/services/decline_qts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ def call
ApplicationFormStatusUpdater.call(application_form:, user:)
end

TeacherMailer.with(teacher:).application_declined.deliver_later
TeacherMailer.with(application_form:).application_declined.deliver_later
end

private

attr_reader :application_form, :user
delegate :teacher, to: :application_form
end
10 changes: 2 additions & 8 deletions app/services/submit_application_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,11 @@ def call
ApplicationFormStatusUpdater.call(application_form:, user:)
end

TeacherMailer
.with(teacher: application_form.teacher)
.application_received
.deliver_later
TeacherMailer.with(application_form:).application_received.deliver_later

if !application_form.requires_preliminary_check &&
application_form.teaching_authority_provides_written_statement
TeacherMailer
.with(teacher: application_form.teacher)
.initial_checks_passed
.deliver_later
TeacherMailer.with(application_form:).initial_checks_passed.deliver_later
end

if region.teaching_authority_requires_submission_email
Expand Down
3 changes: 1 addition & 2 deletions app/services/update_work_history_contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def call
if email.present? && (reference_request = work_history.reference_request)
RefereeMailer.with(reference_request:).reference_requested.deliver_later
TeacherMailer
.with(teacher:, reference_requests: [reference_request])
.with(application_form:, reference_requests: [reference_request])
.references_requested
.deliver_later
end
Expand All @@ -39,7 +39,6 @@ def call
attr_reader :work_history, :user, :name, :job, :email

delegate :application_form, to: :work_history
delegate :teacher, to: :application_form

def change_value(column_name, new_value)
old_value = work_history.send(column_name)
Expand Down
6 changes: 2 additions & 4 deletions app/services/verify_assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ class AlreadyVerified < StandardError
:work_histories

delegate :application_form, to: :assessment
delegate :teacher,
:teaching_authority_provides_written_statement,
to: :application_form
delegate :teaching_authority_provides_written_statement, to: :application_form

def create_professional_standing_request
if professional_standing && !teaching_authority_provides_written_statement
Expand Down Expand Up @@ -84,7 +82,7 @@ def send_reference_request_emails(reference_requests)
end

TeacherMailer
.with(teacher:, reference_requests:)
.with(application_form:, reference_requests:)
.references_requested
.deliver_later
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<%= render(PreviewMailer::Component.new(
mailer_class: TeacherMailer,
name: :application_awarded,
teacher: @application_form.teacher,
application_form: @application_form,
)) %>

<div class="govuk-button-group">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<%= render(PreviewMailer::Component.new(
mailer_class: TeacherMailer,
name: :application_declined,
teacher: @application_form.teacher,
application_form: @application_form,
)) %>

<div class="govuk-button-group">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<%= render(PreviewMailer::Component.new(
mailer_class: TeacherMailer,
name: :references_requested,
teacher: @application_form.teacher,
application_form: @application_form,
reference_requests: [OpenStruct.new(
expires_at: Time.zone.now + 6.weeks,
)],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<%= render(PreviewMailer::Component.new(
mailer_class: TeacherMailer,
name: :further_information_requested,
teacher: @application_form.teacher,
application_form: @application_form,
)) %>

<%= form_with model: [:assessor_interface, @application_form, @assessment, @further_information_request] do |f| %>
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/application_forms.rake
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace :application_forms do
next unless teacher.application_form == application_form

TeacherMailer
.with(teacher:)
.with(application_form:)
.application_from_ineligible_country
.deliver_now
end
Expand Down
4 changes: 1 addition & 3 deletions spec/lib/application_mailer_observer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@

context "with a teacher mailer" do
let(:application_form) { create(:application_form) }
let(:message) do
TeacherMailer.with(teacher: application_form.teacher).application_received
end
let(:message) { TeacherMailer.with(application_form:).application_received }

include_examples "observes mail"
end
Expand Down
7 changes: 4 additions & 3 deletions spec/lib/mailer_preview_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

RSpec.describe MailerPreview do
let(:further_information_request) { create(:further_information_request) }
let(:teacher) do
further_information_request.assessment.application_form.teacher
let(:application_form) do
further_information_request.assessment.application_form
end
let(:teacher) { application_form.teacher }
let(:notify_key) { "notify-key" }
let(:notify_client) do
double(generate_template_preview: notify_template_preview)
Expand All @@ -27,7 +28,7 @@
subject do
described_class.with(
TeacherMailer,
teacher:,
application_form:,
further_information_request:,
).further_information_requested
end
Expand Down
Loading

0 comments on commit e6af8e8

Please sign in to comment.