Skip to content

Commit

Permalink
LUPEYALPHA-1035 - Only send chase email once, rejected/held claims ar…
Browse files Browse the repository at this point in the history
…e ignored
  • Loading branch information
kenfodder committed Sep 23, 2024
1 parent 606e6a4 commit 92c09c3
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/models/policies/further_education_payments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions app/models/policies/further_education_payments/eligibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/analytics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
policy: Policies::FurtherEducationPayments,
eligibility: build(
:further_education_payments_eligibility,
:eligible,
provider_verification_email_last_sent_at: nil
:eligible
))
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -107,13 +143,43 @@
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)
.with(claim_with_provider_email_sent_over_3_weeks_ago_verified)
.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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 92c09c3

Please sign in to comment.