diff --git a/app/jobs/further_education_payments/provider_verification_chase_email_job.rb b/app/jobs/further_education_payments/provider_verification_chase_email_job.rb index 02154116a7..f96bb977d6 100644 --- a/app/jobs/further_education_payments/provider_verification_chase_email_job.rb +++ b/app/jobs/further_education_payments/provider_verification_chase_email_job.rb @@ -25,8 +25,10 @@ def unverified_claims_with_provider_email_sent_over_3_weeks_ago Policies::FurtherEducationPayments::Eligibility .includes(:claim) .unverified - .where("provider_verification_email_last_sent_at < ?", 3.weeks.ago) + .provider_verification_email_last_sent_over(3.weeks.ago) + .provider_verification_chase_email_not_sent .map(&:claim) + .reject { |claim| claim.held? || claim.latest_decision&.rejected? } end end end diff --git a/app/models/policies/further_education_payments.rb b/app/models/policies/further_education_payments.rb index e3796c4fc7..60c3695ef2 100644 --- a/app/models/policies/further_education_payments.rb +++ b/app/models/policies/further_education_payments.rb @@ -69,7 +69,7 @@ def verification_due_date_for_claim(claim) end def verification_chase_due_date_for_claim(claim) - (claim.eligibility.provider_verification_email_last_sent_at + 3.weeks).to_date + (claim.eligibility.provider_verification_chase_email_last_sent_at + 3.weeks).to_date end def duplicate_claim?(claim) diff --git a/app/models/policies/further_education_payments/eligibility.rb b/app/models/policies/further_education_payments/eligibility.rb index f8025e46d8..90be4cc547 100644 --- a/app/models/policies/further_education_payments/eligibility.rb +++ b/app/models/policies/further_education_payments/eligibility.rb @@ -26,6 +26,8 @@ def description belongs_to :school, optional: true scope :unverified, -> { where(verification: {}) } + scope :provider_verification_email_last_sent_over, ->(older_than) { where("provider_verification_email_last_sent_at < ?", older_than) } + scope :provider_verification_chase_email_not_sent, -> { where(provider_verification_chase_email_last_sent_at: nil) } # Claim#school expects this alias_method :current_school, :school diff --git a/app/models/policies/further_education_payments/provider_verification_emails.rb b/app/models/policies/further_education_payments/provider_verification_emails.rb index 85f49160cb..957adbf9db 100644 --- a/app/models/policies/further_education_payments/provider_verification_emails.rb +++ b/app/models/policies/further_education_payments/provider_verification_emails.rb @@ -11,9 +11,9 @@ def send_further_education_payment_provider_verification_email ClaimMailer.further_education_payment_provider_verification_email(@claim).deliver_later end - # Subsequent chase email + # Second automated provider chase email def send_further_education_payment_provider_verification_chase_email - @claim.eligibility.update!(provider_verification_email_last_sent_at: Time.now) + @claim.eligibility.update!(provider_verification_chase_email_last_sent_at: Time.now) ClaimMailer.further_education_payment_provider_verification_chase_email(@claim).deliver_later end end diff --git a/config/analytics.yml b/config/analytics.yml index 708762f75e..2b35342025 100644 --- a/config/analytics.yml +++ b/config/analytics.yml @@ -300,6 +300,7 @@ shared: - half_teaching_hours - flagged_as_duplicate - provider_verification_email_last_sent_at + - provider_verification_chase_email_last_sent_at :eligible_fe_providers: - id - ukprn diff --git a/db/migrate/20240923130010_add_provider_verification_chase_email_last_sent_at_to_further_education_payments_eligibilities.rb b/db/migrate/20240923130010_add_provider_verification_chase_email_last_sent_at_to_further_education_payments_eligibilities.rb new file mode 100644 index 0000000000..442085905f --- /dev/null +++ b/db/migrate/20240923130010_add_provider_verification_chase_email_last_sent_at_to_further_education_payments_eligibilities.rb @@ -0,0 +1,5 @@ +class AddProviderVerificationChaseEmailLastSentAtToFurtherEducationPaymentsEligibilities < ActiveRecord::Migration[7.0] + def change + add_column :further_education_payments_eligibilities, :provider_verification_chase_email_last_sent_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 2237f77b7d..d7d495ccc3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_09_16_173031) do +ActiveRecord::Schema[7.0].define(version: 2024_09_23_130010) do # These are extensions that must be enabled in order to support this database enable_extension "citext" enable_extension "pg_trgm" @@ -262,6 +262,7 @@ t.jsonb "verification", default: {} t.boolean "flagged_as_duplicate", default: false t.datetime "provider_verification_email_last_sent_at" + t.datetime "provider_verification_chase_email_last_sent_at" t.index ["possible_school_id"], name: "index_fe_payments_eligibilities_on_possible_school_id" t.index ["school_id"], name: "index_fe_payments_eligibilities_on_school_id" end diff --git a/spec/jobs/further_education_payments/provider_verification_chase_email_spec.rb b/spec/jobs/further_education_payments/provider_verification_chase_email_spec.rb index 0d683ebc31..1e6d580952 100644 --- a/spec/jobs/further_education_payments/provider_verification_chase_email_spec.rb +++ b/spec/jobs/further_education_payments/provider_verification_chase_email_spec.rb @@ -14,8 +14,7 @@ policy: Policies::FurtherEducationPayments, eligibility: build( :further_education_payments_eligibility, - :eligible, - provider_verification_email_last_sent_at: nil + :eligible )) } @@ -53,6 +52,42 @@ )) } + let!(:claim_with_provider_chase_email_already_sent) { + create(:claim, + :submitted, + policy: Policies::FurtherEducationPayments, + eligibility: build( + :further_education_payments_eligibility, + :eligible, + :verified, + provider_verification_email_last_sent_at: DateTime.new(2024, 9, 1, 8, 0, 0), + provider_verification_chase_email_last_sent_at: DateTime.new(2024, 9, 22, 8, 0, 0) + )) + } + + let!(:claim_rejected_after_provider_verification_was_sent) { + create(:claim, + :rejected, + policy: Policies::FurtherEducationPayments, + eligibility: build( + :further_education_payments_eligibility, + :eligible, + provider_verification_email_last_sent_at: DateTime.new(2024, 10, 1, 7, 0, 0) + )) + } + + let!(:claim_held_after_provider_verification_was_sent) { + create(:claim, + :submitted, + :held, + policy: Policies::FurtherEducationPayments, + eligibility: build( + :further_education_payments_eligibility, + :eligible, + provider_verification_email_last_sent_at: DateTime.new(2024, 10, 1, 7, 0, 0) + )) + } + before do allow(ClaimMailer).to( receive(:further_education_payment_provider_verification_chase_email) @@ -62,7 +97,7 @@ end it "sends an email only to unverified claims with a provider email last sent over 3 weeks ago " do - expect(claim_with_provider_email_sent_over_3_weeks_ago.eligibility.reload.provider_verification_email_last_sent_at).to eq Time.now + expect(claim_with_provider_email_sent_over_3_weeks_ago.eligibility.reload.provider_verification_chase_email_last_sent_at).to eq Time.now expect(ClaimMailer).to( have_received(:further_education_payment_provider_verification_chase_email) @@ -88,6 +123,7 @@ it "does not send a chaser if a provider email was not previously sent" do expect(claim_with_no_provider_email_sent.eligibility.reload.provider_verification_email_last_sent_at).to be_nil + expect(claim_with_no_provider_email_sent.eligibility.reload.provider_verification_chase_email_last_sent_at).to be_nil expect(ClaimMailer).to( have_received(:further_education_payment_provider_verification_chase_email) @@ -97,7 +133,7 @@ end it "does not send a chaser if it has not been 3 weeks since a provider email was sent" do - expect(claim_with_provider_email_sent_less_than_3_weeks_ago.eligibility.reload.provider_verification_email_last_sent_at).to eq DateTime.new(2024, 10, 15, 7, 0, 0) + expect(claim_with_provider_email_sent_less_than_3_weeks_ago.eligibility.reload.provider_verification_chase_email_last_sent_at).to be_nil expect(ClaimMailer).to( have_received(:further_education_payment_provider_verification_chase_email) @@ -107,7 +143,7 @@ end it "does not sent a chaser for any claims that are verified" do - expect(claim_with_provider_email_sent_over_3_weeks_ago_verified.eligibility.reload.provider_verification_email_last_sent_at).to eq DateTime.new(2024, 10, 1, 7, 0, 0) + expect(claim_with_provider_email_sent_over_3_weeks_ago_verified.eligibility.reload.provider_verification_chase_email_last_sent_at).to be_nil expect(ClaimMailer).to( have_received(:further_education_payment_provider_verification_chase_email) @@ -115,5 +151,35 @@ .exactly(0).times ) end + + it "does not send a chaser email if one has been sent before" do + expect(claim_with_provider_chase_email_already_sent.eligibility.reload.provider_verification_chase_email_last_sent_at).to eq DateTime.new(2024, 9, 22, 8, 0, 0) + + expect(ClaimMailer).to( + have_received(:further_education_payment_provider_verification_chase_email) + .with(claim_with_provider_chase_email_already_sent) + .exactly(0).times + ) + end + + it "does not send a chaser email if the claim has been rejected" do + expect(claim_rejected_after_provider_verification_was_sent.eligibility.reload.provider_verification_chase_email_last_sent_at).to be_nil + + expect(ClaimMailer).to( + have_received(:further_education_payment_provider_verification_chase_email) + .with(claim_rejected_after_provider_verification_was_sent) + .exactly(0).times + ) + end + + it "does not send a chaser email if the claimis on hold" do + expect(claim_held_after_provider_verification_was_sent.eligibility.reload.provider_verification_chase_email_last_sent_at).to be_nil + + expect(ClaimMailer).to( + have_received(:further_education_payment_provider_verification_chase_email) + .with(claim_held_after_provider_verification_was_sent) + .exactly(0).times + ) + end end end diff --git a/spec/models/policies/further_education_payments/provider_verfication_emails_spec.rb b/spec/models/policies/further_education_payments/provider_verfication_emails_spec.rb index 4faeba68a0..134717fa6e 100644 --- a/spec/models/policies/further_education_payments/provider_verfication_emails_spec.rb +++ b/spec/models/policies/further_education_payments/provider_verfication_emails_spec.rb @@ -43,7 +43,7 @@ ) ) - expect(claim.eligibility.reload.provider_verification_email_last_sent_at).to eq Time.now + expect(claim.eligibility.reload.provider_verification_chase_email_last_sent_at).to eq Time.now end end end