From 476b4ffda3d9e191b2db7bd1aa86f944ee43f65a Mon Sep 17 00:00:00 2001 From: Richard Lynch Date: Tue, 29 Oct 2024 14:33:05 +0000 Subject: [PATCH] Add note on csv upload When the fraud risk csv is uploaded add notes to any claims with matching details. --- app/forms/admin/fraud_risk_csv_upload_form.rb | 20 +++++++++ app/models/policies.rb | 4 ++ app/models/risk_indicator.rb | 2 + .../admin/admin_fraud_prevention_spec.rb | 28 +++++++++++++ .../admin/fraud_risk_csv_upload_form_spec.rb | 42 +++++++++++++++++++ 5 files changed, 96 insertions(+) diff --git a/app/forms/admin/fraud_risk_csv_upload_form.rb b/app/forms/admin/fraud_risk_csv_upload_form.rb index ddc461b099..98249a7648 100644 --- a/app/forms/admin/fraud_risk_csv_upload_form.rb +++ b/app/forms/admin/fraud_risk_csv_upload_form.rb @@ -21,6 +21,10 @@ def save RiskIndicator.where.not(id: records.map(&:id)).destroy_all records.each(&:save!) + + claims_to_note.each do |claim| + AutomatedChecks::ClaimVerifiers::FraudRisk.new(claim: claim).perform + end end true @@ -53,5 +57,21 @@ def csv_has_required_headers def csv_has_required_headers? csv.headers.include?("field") && csv.headers.include?("value") end + + def claims_to_note + flagged_eligibility_claim_ids = Policies.with_attribute(:teacher_reference_number).flat_map do |policy| + policy::Eligibility + .where(teacher_reference_number: RiskIndicator.teacher_reference_number.select(:value)) + .joins(:claim) + .select("claims.id") + end + + Claim + .where( + "LOWER(national_insurance_number) IN (?)", + RiskIndicator.national_insurance_number.select("LOWER(value)") + ) + .or(Claim.where(id: flagged_eligibility_claim_ids)) + end end end diff --git a/app/models/policies.rb b/app/models/policies.rb index f08416c093..70d9efc982 100644 --- a/app/models/policies.rb +++ b/app/models/policies.rb @@ -36,4 +36,8 @@ def self.[](policy_type) def self.constantize(policy) "Policies::#{policy}".constantize end + + def self.with_attribute(attr) + POLICIES.select { |policy| policy::Eligibility.has_attribute?(attr) } + end end diff --git a/app/models/risk_indicator.rb b/app/models/risk_indicator.rb index 34614cb18a..f47e85faff 100644 --- a/app/models/risk_indicator.rb +++ b/app/models/risk_indicator.rb @@ -4,6 +4,8 @@ class RiskIndicator < ApplicationRecord national_insurance_number ].freeze + enum field: SUPPORTED_FIELDS.index_by(&:itself) + validates :field, presence: { message: "'field' can't be blank" } diff --git a/spec/features/admin/admin_fraud_prevention_spec.rb b/spec/features/admin/admin_fraud_prevention_spec.rb index 84846232ba..8b8a9f5644 100644 --- a/spec/features/admin/admin_fraud_prevention_spec.rb +++ b/spec/features/admin/admin_fraud_prevention_spec.rb @@ -93,6 +93,34 @@ "This claim cannot be approved because the national insurance number " \ "and teacher reference number are included on the fraud prevention list." ) + + visit admin_claim_notes_path(flagged_claim_trn) + + within(".hmcts-timeline:first-of-type") do + expect(page).to have_content( + "This claim has been flagged as the " \ + "teacher reference number is included on the fraud prevention list." + ) + end + + visit admin_claim_notes_path(flagged_claim_nino) + + within(".hmcts-timeline:first-of-type") do + expect(page).to have_content( + "This claim has been flagged as the " \ + "national insurance number is included on the fraud prevention list." + ) + end + + visit admin_claim_notes_path(flagged_claim_trn_and_nino) + + within(".hmcts-timeline:first-of-type") do + expect(page).to have_content( + "This claim has been flagged as the " \ + "national insurance number and teacher reference number are included " \ + "on the fraud prevention list." + ) + end end end diff --git a/spec/forms/admin/fraud_risk_csv_upload_form_spec.rb b/spec/forms/admin/fraud_risk_csv_upload_form_spec.rb index 87e6528894..a027455cee 100644 --- a/spec/forms/admin/fraud_risk_csv_upload_form_spec.rb +++ b/spec/forms/admin/fraud_risk_csv_upload_form_spec.rb @@ -136,5 +136,47 @@ ) ).to exist end + + it "adds a note to claims that are flagged" do + claim_1 = create(:claim, national_insurance_number: "qq123456c") + + claim_2 = create( + :claim, + eligibility_attributes: {teacher_reference_number: "1234567"} + ) + + claim_3 = create( + :claim, + national_insurance_number: "qq123456c", + eligibility_attributes: {teacher_reference_number: "1234567"} + ) + + form.save + + expect(claim_1.notes.by_label("fraud_risk").last.body).to eq( + "This claim has been flagged as the national insurance number is " \ + "included on the fraud prevention list." + ) + + expect(claim_2.notes.by_label("fraud_risk").last.body).to eq( + "This claim has been flagged as the teacher reference number is " \ + "included on the fraud prevention list." + ) + + expect(claim_3.notes.by_label("fraud_risk").last.body).to eq( + "This claim has been flagged as the national insurance number and " \ + "teacher reference number are included on the fraud prevention list." + ) + end + + it "doesn't add a note to claims that aren't flagged" do + claim = create( + :claim, + national_insurance_number: "qq123456d", + eligibility_attributes: {teacher_reference_number: "1234568"} + ) + + expect { form.save }.not_to change { claim.notes.count } + end end end