From a659f6a60d97405fb51ad0462d38a725adff1cb3 Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Tue, 13 Feb 2024 11:21:49 +0000 Subject: [PATCH] Allow sending reminders for consent requests This ensures that we send reminder emails for consent if we haven't received a response from the user in 3 weeks. It uses the same reminder mechanism for the reference requests, although as we don't send individual reminders we don't mark the qualification request class as remindable. --- app/models/application_form.rb | 16 ++++++++- app/models/qualification_request.rb | 4 +-- spec/models/application_form_spec.rb | 53 ++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/app/models/application_form.rb b/app/models/application_form.rb index 99dbdff203..c64a0cb994 100644 --- a/app/models/application_form.rb +++ b/app/models/application_form.rb @@ -247,13 +247,18 @@ def created_under_new_regulations? end def reminder_email_names - %w[expiration references] + %w[consent expiration references] end def should_send_reminder_email?(name, number_of_reminders_sent) return false if teacher.application_form != self case name + when "consent" + number_of_reminders_sent.zero? && + consent_requests_not_yet_received.any? do |qualification_request| + qualification_request.days_until_expired <= 21 + end when "expiration" return false if days_until_expired.nil? @@ -271,6 +276,8 @@ 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 when "expiration" TeacherMailer .with(application_form: self, number_of_reminders_sent:) @@ -306,4 +313,11 @@ def reference_requests_not_yet_received_or_rejected .where.not(requested_at: nil) .where(received_at: nil, verify_passed: nil, review_passed: nil) end + + def consent_requests_not_yet_received + QualificationRequest + .joins(:qualification) + .where(qualifications: { application_form_id: id }) + .consent_respondable + end end diff --git a/app/models/qualification_request.rb b/app/models/qualification_request.rb index f1c1b06a28..8b48a993f0 100644 --- a/app/models/qualification_request.rb +++ b/app/models/qualification_request.rb @@ -47,9 +47,9 @@ class QualificationRequest < ApplicationRecord scope :consent_received, -> { where.not(consent_received_at: nil) } scope :consent_respondable, -> do - consent_required - .consent_requested + consent_requested .where(consent_received_at: nil) + .joins(assessment: :application_form) .merge(ApplicationForm.assessable) end diff --git a/spec/models/application_form_spec.rb b/spec/models/application_form_spec.rb index bf10ed76b9..39a8269d07 100644 --- a/spec/models/application_form_spec.rb +++ b/spec/models/application_form_spec.rb @@ -410,6 +410,59 @@ end end + describe "#should_send_reminder_email?" do + subject(:should_send_reminder_email?) do + application_form.should_send_reminder_email?( + name, + number_of_reminders_sent, + ) + end + + let(:application_form) { create(:application_form, :verification_stage) } + let(:assessment) { create(:assessment, application_form:) } + let(:qualification) { create(:qualification, application_form:) } + + context "with consent reminders" do + let(:name) { "consent" } + + context "with no reminders sent" do + let(:number_of_reminders_sent) { 0 } + it { is_expected.to be false } + + context "with a qualification request" do + before do + create( + :qualification_request, + assessment:, + qualification:, + consent_requested_at: 1.week.ago, + ) + end + + it { is_expected.to be false } + end + + context "with a qualification request older than 3 weeks ago" do + before do + create( + :qualification_request, + assessment:, + qualification:, + consent_requested_at: 4.weeks.ago, + ) + end + + it { is_expected.to be true } + end + end + + context "with no reminders sent" do + let(:number_of_reminders_sent) { 1 } + it { is_expected.to be false } + end + end + end + describe "#from_ineligible_country" do subject(:from_ineligible_country) do described_class.from_ineligible_country