Skip to content

Commit

Permalink
Send reminder reference emails
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasleese committed Nov 15, 2023
1 parent 05fb8b5 commit 74b1f01
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 42 deletions.
60 changes: 50 additions & 10 deletions app/models/application_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,12 @@ class ApplicationForm < ApplicationRecord
.or(withdrawn.where("withdrawn_at < ?", 5.years.ago))
end

scope :remindable, -> { draft.where("created_at < ?", 5.months.ago) }
scope :remindable,
-> do
verification_stage.or(
draft_stage.where("created_at < ?", 5.months.ago),
)
end

def to_param
reference
Expand Down Expand Up @@ -262,18 +267,46 @@ def created_under_new_regulations?
created_at >= Date.parse(ENV.fetch("NEW_REGS_DATE", "2023-02-01"))
end

def should_send_reminder_email?(_name, number_of_reminders_sent)
return false if days_until_expired.nil? || teacher.application_form != self
def reminder_email_names
%w[expiration references]
end

(days_until_expired <= 14 && number_of_reminders_sent.zero?) ||
(days_until_expired <= 7 && number_of_reminders_sent == 1)
def should_send_reminder_email?(name, number_of_reminders_sent)
return false if teacher.application_form != self

case name
when "expiration"
return false if days_until_expired.nil?

(days_until_expired <= 14 && number_of_reminders_sent.zero?) ||
(days_until_expired <= 7 && number_of_reminders_sent == 1)
when "references"
unreviewed_reference_requests.any? do |reference_request|
reference_request.should_send_reminder_email?(
"expiration",
number_of_reminders_sent,
)
end
end
end

def send_reminder_email(_name, number_of_reminders_sent)
TeacherMailer
.with(teacher:, number_of_reminders_sent:)
.application_not_submitted
.deliver_later
def send_reminder_email(name, number_of_reminders_sent)
case name
when "expiration"
TeacherMailer
.with(teacher:, number_of_reminders_sent:)
.application_not_submitted
.deliver_later
when "references"
TeacherMailer
.with(
teacher:,
number_of_reminders_sent:,
reference_requests: unreviewed_reference_requests.to_a,
)
.references_reminder
.deliver_later
end
end

def expires_after
Expand All @@ -293,4 +326,11 @@ def build_documents
documents.build(document_type: :english_language_proficiency)
documents.build(document_type: :written_statement)
end

def unreviewed_reference_requests
ReferenceRequest
.joins(:work_history)
.where(work_histories: { application_form_id: id })
.where(received_at: nil, expired_at: nil)
end
end
127 changes: 97 additions & 30 deletions spec/services/send_reminder_email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
end
end

shared_examples "sends an email" do
shared_examples "sends an email" do |name|
it "logs an email" do
expect { call }.to change {
ReminderEmail.where(remindable:, name: "expiration").count
ReminderEmail.where(remindable:, name:).count
}.by(1)
end
end

shared_examples "sends a further information reminder email" do
include_examples "sends an email"
include_examples "sends an email", "expiration"

it "sends an email" do
expect { call }.to have_enqueued_mail(
Expand All @@ -42,7 +42,7 @@
end

shared_examples "sends a reference reminder email" do
include_examples "sends an email"
include_examples "sends an email", "expiration"

it "sends an email" do
expect { subject }.to have_enqueued_mail(
Expand All @@ -53,7 +53,7 @@
end

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

it "sends an email" do
expect { subject }.to have_enqueued_mail(
Expand All @@ -69,56 +69,68 @@
end
end

shared_examples "first reminder email" do |shared_example|
shared_examples "sends an application references reminder email" do
include_examples "sends an email", "references"

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

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

context "when a previous reminder has been sent" do
before { remindable.reminder_emails.create!(name: "expiration") }
before { remindable.reminder_emails.create!(name:) }
include_examples "doesn't send an email"
end

context "when two previous reminders have been sent" do
before do
2.times { remindable.reminder_emails.create!(name: "expiration") }
end
before { 2.times { remindable.reminder_emails.create!(name:) } }
include_examples "doesn't send an email"
end
end

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

context "when one previous reminder has been sent" do
before { remindable.reminder_emails.create!(name: "expiration") }
before { remindable.reminder_emails.create!(name:) }
include_examples shared_example
end

context "when two previous reminders have been sent" do
before do
2.times { remindable.reminder_emails.create!(name: "expiration") }
end
before { 2.times { remindable.reminder_emails.create!(name:) } }
include_examples "doesn't send an email"
end
end

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

context "when one previous reminder has been sent" do
before { remindable.reminder_emails.create!(name: "expiration") }
before { remindable.reminder_emails.create!(name:) }
include_examples shared_example
end

context "when two previous reminders have been sent" do
before do
2.times { remindable.reminder_emails.create!(name: "expiration") }
end
before { 2.times { remindable.reminder_emails.create!(name:) } }
include_examples shared_example
end
end
Expand All @@ -131,13 +143,15 @@
context "with less than two weeks remaining" do
let(:application_created_at) { (6.months - 13.days).ago }
include_examples "first reminder email",
"sends an application not submitted email"
"sends an application not submitted email",
"expiration"
end

context "with less than one week remaining" do
let(:application_created_at) { (6.months - 1.day).ago }
include_examples "second reminder email",
"sends an application not submitted email"
"sends an application not submitted email",
"expiration"
end
end

Expand All @@ -150,39 +164,45 @@
context "with less than two weeks remaining" do
let(:further_information_requested_at) { (6.weeks - 13.days).ago }
include_examples "first reminder email",
"sends a further information reminder email"
"sends a further information reminder email",
"expiration"
end

context "with less than one week remaining" do
let(:further_information_requested_at) { (6.weeks - 5.days).ago }
include_examples "second reminder email",
"sends a further information reminder email"
"sends a further information reminder email",
"expiration"
end

context "with one day remaining" do
let(:further_information_requested_at) { (6.weeks - 47.hours).ago }
include_examples "third reminder email",
"sends a further information reminder email"
"sends a further information reminder email",
"expiration"
end
end

shared_examples_for "an FI request that is allowed 4 weeks to complete" do
context "with less than two weeks remaining" do
let(:further_information_requested_at) { (4.weeks - 13.days).ago }
include_examples "first reminder email",
"sends a further information reminder email"
"sends a further information reminder email",
"expiration"
end

context "with less than one week remaining" do
let(:further_information_requested_at) { (4.weeks - 5.days).ago }
include_examples "second reminder email",
"sends a further information reminder email"
"sends a further information reminder email",
"expiration"
end

context "with one day remaining" do
let(:further_information_requested_at) { (4.weeks - 47.hours).ago }
include_examples "third reminder email",
"sends a further information reminder email"
"sends a further information reminder email",
"expiration"
end
end

Expand Down Expand Up @@ -276,13 +296,15 @@
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 reminder email"
"sends a reference reminder email",
"expiration"
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 reminder email"
"sends a reference reminder email",
"expiration"
end
end

Expand All @@ -299,5 +321,50 @@
end
include_examples "doesn't send an email"
end

context "with a teacher with an remindable reference request" do
let(:remindable) { create(:application_form, :submitted) }
let(:assessment) { create(:assessment, application_form: remindable) }
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 an application references reminder email",
"references"
end

context "with less than two weeks remaining" do
let(:reference_requested_at) { (6.weeks - 13.days).ago }
include_examples "second reminder email",
"sends an application references reminder email",
"references"
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
4 changes: 2 additions & 2 deletions spec/support/shared_examples/remindable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

describe "#should_send_reminder_email?" do
let(:should_send_reminder_email?) do
subject.should_send_reminder_email?("type", 0)
subject.should_send_reminder_email?("expiration", 0)
end

it "returns a boolean" do
Expand All @@ -23,7 +23,7 @@
end

describe "#send_reminder_email" do
let(:send_reminder_email) { subject.send_reminder_email("type", 0) }
let(:send_reminder_email) { subject.send_reminder_email("expiration", 0) }

it "enqueues an email" do
expect { send_reminder_email }.to have_enqueued_mail
Expand Down

0 comments on commit 74b1f01

Please sign in to comment.