Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpattinson committed Nov 9, 2023
1 parent dcafb2b commit 3ce587e
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 6 deletions.
7 changes: 7 additions & 0 deletions app/jobs/send_reference_request_reminder_to_teacher_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class SendReferenceRequestReminderToTeacherJob < ApplicationJob
def perform
SendReferenceRequestReminderToTeacher.call
end
end
12 changes: 12 additions & 0 deletions app/mailers/teacher_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ def references_requested
)
end

def references_reminder
@number_of_reminders_sent = teacher.reminder_emails.count
view_mail(
GOVUK_NOTIFY_TEMPLATE_ID,
to: teacher.email,
subject:
I18n.t(
"mailer.teacher.references_reminder.subject.#{@number_of_reminders_sent}",
),
)
end

private

def teacher
Expand Down
1 change: 1 addition & 0 deletions app/models/concerns/expirable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Expirable
extend ActiveSupport::Concern

def expires_at
return true if is_a?(Teacher)
return nil if requested_at.nil? || expires_after.nil?

requested_at + expires_after
Expand Down
25 changes: 25 additions & 0 deletions app/models/teacher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# index_teachers_on_uuid (uuid) UNIQUE
#
class Teacher < ApplicationRecord
include Remindable
include Emailable

devise :magic_link_authenticatable, :registerable, :timeoutable, :trackable
Expand All @@ -45,4 +46,28 @@ def send_magic_link(*)
def send_devise_notification(notification, *args)
devise_mailer.send(notification, self, *args).deliver_later
end

def should_send_reminder_email?
all_reference_requests =
ReferenceRequest
.joins(:work_history)
.where(work_histories: { application_form_id: application_form.id })
.where(received_at: nil, expired_at: nil)
@remindable_reference_requests =
all_reference_requests.select do |reference_request|
reference_request.should_send_reminder_email?(
(reference_request.expires_at.to_date - Time.zone.today).to_i,
reminder_emails.count,
)
end
Rails.logger.debug @remindable_reference_requests
@remindable_reference_requests.any?
end

def send_reminder_email(_number_of_reminders_sent)
TeacherMailer
.with(teacher: self, reference_requests: @remindable_reference_requests)
.references_reminder
.deliver_later
end
end
21 changes: 21 additions & 0 deletions app/services/send_reference_request_reminder_to_teacher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true
#
class SendReferenceRequestReminderToTeacher
include ServicePattern

def call
reference_requests =
ReferenceRequest.where(expired_at: nil, received_at: nil)
teachers_to_receive_reminder_email = Set.new

reference_requests.each do |reference_request|
if reference_request.should_display_teacher_reference_reminder_text?
teacher = reference_request.assessment.application_form.teacher
teachers_to_receive_reminder_email.add(teacher)
end
end
teachers_to_receive_reminder_email.each do |teacher|
TeacherMailer.with(teacher:).references_reminder.deliver_later
end
end
end
15 changes: 9 additions & 6 deletions app/services/send_reminder_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ def call
attr_reader :remindable

def send_reminder?
return false unless remindable.expires_at

remindable.should_send_reminder_email?(
days_until_expired,
number_of_reminders_sent,
)
if remindable.is_a?(Teacher)
remindable.should_send_reminder_email?
else
return false unless remindable.expires_at
remindable.should_send_reminder_email?(
days_until_expired,
number_of_reminders_sent,
)
end
end

def number_of_reminders_sent
Expand Down
18 changes: 18 additions & 0 deletions app/views/teacher_mailer/references_reminder.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Dear <%= application_form_full_name(@application_form) %>

We’re still waiting for a response from one or more of the references you provided to verify your work history.

<% if @teacher.reminder_emails.count == 0 %>
We contacted your references on <%= @application_form.work_histories.first.reference_request.requested_at.to_date.to_fs(:long_ordinal_uk) %> but have not received a response from:
<% elsif @teacher.reminder_emails.count == 1 %>
The following references have just 2 weeks to respond to the reference request.
<% end %>

<% @reference_requests.each do |reference_request| %>
<%= reference_request.contact_name + ". " + reference_request.work_history.school_name %>
<% end %>


They need to respond by <%= @reference_requests.first.expires_at.to_date.to_fs(:long_ordinal_uk) %>. If your references do not respond, and, as a result, we cannot verify your work history, we may not be able to award you QTS. You should contact your reference to remind them about the request.

<%= render "shared/teacher_mailer/footer" %>
4 changes: 4 additions & 0 deletions config/locales/mailer.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ en:
subject: Your qualified teacher status application – we’ve received your %{certificate}
references_requested:
subject: Your qualified teacher status application – we’ve contacted your references
references_reminder:
subject:
0: Waiting on references – QTS application
1: Your references only have two weeks left to respond
teaching_authority:
application_submitted:
subject: "%{name} has made an application for qualified teacher status (QTS) in England"
106 changes: 106 additions & 0 deletions spec/services/send_reference_request_reminder_to_teach_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe SendReferenceRequestReminderToTeacher do
describe "#call" do
subject(:call) { described_class.call }

shared_examples "doesn't send an email" do
it "doesn't send any email" do
expect { call }.to_not have_enqueued_mail
end
end

shared_examples "sends a reference request reminder to teacher email" do
it "sends an email" do
expect { call }.to have_enqueued_mail(
TeacherMailer,
:references_reminder,
).with(params: { teacher: }, args: [])
end
end

shared_examples "first reminder email" do |shared_example|
context "when no previous reminder has been sent" do
include_examples shared_example
end

context "when a previous reminder has been sent" do
before { reference_request.reminder_emails.create }
include_examples "doesn't send an email"
end

context "when two previous reminders have been sent" do
before { 2.times { reference_request.reminder_emails.create } }
include_examples "doesn't send an email"
end
end

shared_examples "second reminder email" do |shared_example|
context "when no previous reminder has been sent" do
include_examples shared_example
end

context "when one previous reminder has been sent" do
before { reference_request.reminder_emails.create }
include_examples shared_example
end

context "when two previous reminders have been sent" do
before { 2.times { reference_request.reminder_emails.create } }
include_examples "doesn't send an email"
end
end

context "with a requested reference request" do
let(:application_form) do
create(
:application_form,
:submitted,
:old_regs,
region:,
teacher_id: teacher.id,
)
end
let(:assessment) { create(:assessment, application_form:) }
let(:region) { create(:region, :in_country, country_code: "FR") }
let(:work_history) { reference_request.work_history }
let(:teacher) { create(:teacher) }

let(:reference_request) do
create(
:reference_request,
requested_at: reference_requested_at,
assessment:,
)
end

context "with less than four weeks remaining" do
let(:reference_requested_at) { (6.weeks - 27.days).ago }
include_examples "first reminder email",
"sends a reference request reminder to teacher email"
end

context "with less than two weeks remaining" do
let(:reference_requested_at) { (6.weeks - 13.days).ago }
include_examples "second reminder email",
"sends a reference request reminder to teacher email"
end
end

context "with a received reference request" do
let(:reference_request) do
create(:reference_request, :received, requested_at: Time.zone.now)
end
include_examples "doesn't send an email"
end

context "with an expired reference request" do
let(:reference_request) do
create(:reference_request, :expired, requested_at: Time.zone.now)
end
include_examples "doesn't send an email"
end
end
end
61 changes: 61 additions & 0 deletions spec/services/send_reminder_email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@
end
end

shared_examples "sends a teacher references reminder email" do
include_examples "sends an email"

it "sends an email" do
expect { subject }.to have_enqueued_mail(
TeacherMailer,
:references_reminder,
).with(params: { teacher: remindable, reference_requests: [reference_request] }, args: [])
end
end

shared_examples "sends an application not submitted email" do
include_examples "sends an email"

Expand Down Expand Up @@ -293,5 +304,55 @@
end
include_examples "doesn't send an email"
end

context "with a teacher with an remindable reference request" do
let!(:remindable) do
create(
:teacher
)
end
let!(:application_form) do
create(:application_form, :submitted, :old_regs, region:, teacher: remindable)
end
let!(:assessment) { create(:assessment, application_form:) }
let!(:region) { create(:region, :in_country, country_code: "FR") }
let!(:reference_request) do
create(
:reference_request,
requested_at: reference_requested_at,
assessment:,
)
end
context "with less than four weeks remaining" do
let(:reference_requested_at) { (6.weeks - 27.days).ago }
include_examples "first reminder email",
"sends a teacher references reminder email"
end

context "with less than two weeks remaining" do
let(:reference_requested_at) { (6.weeks - 13.days).ago }
include_examples "second reminder email",
"sends a teacher references reminder email"
end

context "with more than four weeks remaining" do
let(:reference_requested_at) { (2.days).ago }
include_examples "doesn't send an email"
end

context "with a received reference request" do
let(:reference_request) do
create(:reference_request, :received, requested_at: Time.zone.now)
end
include_examples "doesn't send an email"
end

context "with an expired reference request" do
let(:reference_request) do
create(:reference_request, :expired, requested_at: Time.zone.now)
end
include_examples "doesn't send an email"
end
end
end
end

0 comments on commit 3ce587e

Please sign in to comment.