From 7f2c79c0169ad11937120100898ab664b1e472c3 Mon Sep 17 00:00:00 2001 From: Alkesh Vaghmaria Date: Wed, 23 Oct 2024 15:53:47 +0100 Subject: [PATCH] fix and rake task for duplicate SLC data entries --- app/models/student_loans_data.rb | 3 +- .../fix_student_loan_repayment_amounts.rake | 24 ++++++++++++ ...fix_student_loan_repayment_amounts_spec.rb | 38 +++++++++++++++++++ spec/models/student_loans_data_spec.rb | 7 ++-- 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 lib/tasks/fix_student_loan_repayment_amounts.rake create mode 100644 spec/lib/tasks/fix_student_loan_repayment_amounts_spec.rb diff --git a/app/models/student_loans_data.rb b/app/models/student_loans_data.rb index abf02e16c6..9082ec679d 100644 --- a/app/models/student_loans_data.rb +++ b/app/models/student_loans_data.rb @@ -4,6 +4,7 @@ def self.repaying_plan_types end def self.total_repayment_amount - sum(:amount) + distinct_entries = select(:nino, :date_of_birth, :plan_type_of_deduction, :amount).distinct + distinct_entries.sum(:amount) end end diff --git a/lib/tasks/fix_student_loan_repayment_amounts.rake b/lib/tasks/fix_student_loan_repayment_amounts.rake new file mode 100644 index 0000000000..b8c7b89a9d --- /dev/null +++ b/lib/tasks/fix_student_loan_repayment_amounts.rake @@ -0,0 +1,24 @@ +desc "Fix TSLR claims with incorrect student loan amounts" +task fix_tslr_student_loan_amounts: :environment do |task, args| + # SLC data duplicates: + results = ActiveRecord::Base.connection.execute("select nino, date_of_birth, plan_type_of_deduction, amount, count(id), STRING_AGG(claim_reference, ', ') from student_loans_data group by nino, date_of_birth, plan_type_of_deduction, amount having count(id) > 1") + ninos = results.map { |result| result["nino"] } + + # claims affected + claims = Claim.left_joins(:payments).by_policy(Policies::StudentLoans).by_academic_year("2024/2025").where(national_insurance_number: ninos).where.not(payments: nil) + + puts "#{claims.count} claims to update" + claims.each do |claim| + old_amount = claim.eligibility.student_loan_repayment_amount + new_amount = StudentLoansData.where(nino: claim.national_insurance_number, date_of_birth: claim.date_of_birth).total_repayment_amount + + if new_amount != old_amount + if ARGV[1] == "run" + claim.eligibility.update!(student_loan_repayment_amount: new_amount) + puts "updated #{claim.reference} from #{old_amount} to #{new_amount}" + else + puts "should update #{claim.reference} from #{old_amount} to #{new_amount}" + end + end + end +end diff --git a/spec/lib/tasks/fix_student_loan_repayment_amounts_spec.rb b/spec/lib/tasks/fix_student_loan_repayment_amounts_spec.rb new file mode 100644 index 0000000000..3dc818961e --- /dev/null +++ b/spec/lib/tasks/fix_student_loan_repayment_amounts_spec.rb @@ -0,0 +1,38 @@ +require "rails_helper" + +Rails.application.load_tasks + +describe "fix_tslr_student_loan_amounts" do + before do + create(:student_loans_data, nino: "QQ123456A", date_of_birth: Date.new(1980, 1, 1), amount: 100, plan_type_of_deduction: 2) + create(:student_loans_data, nino: "QQ123456A", date_of_birth: Date.new(1980, 1, 1), amount: 50, plan_type_of_deduction: 1) + create(:student_loans_data, nino: "QQ123456B", date_of_birth: Date.new(1990, 1, 1), amount: 60, plan_type_of_deduction: 1) + create(:student_loans_data, nino: "QQ123456B", date_of_birth: Date.new(1990, 1, 1), amount: 60, plan_type_of_deduction: 1) # duplicate + claim_1 + claim_2 + create(:payment, :confirmed, claims: [claim_1]) + create(:payment, :confirmed, claims: [claim_2], payroll_run: Claim.first.payments.last.payroll_run) + end + + let(:claim_1) do + create(:claim, :submitted, policy: Policies::StudentLoans, academic_year: AcademicYear.current, national_insurance_number: "QQ123456A", date_of_birth: Date.new(1980, 1, 1), + eligibility_attributes: {student_loan_repayment_amount: 150}) + end + + let(:claim_2) do + create(:claim, :submitted, policy: Policies::StudentLoans, academic_year: AcademicYear.current, national_insurance_number: "QQ123456B", date_of_birth: Date.new(1990, 1, 1), + eligibility_attributes: {student_loan_repayment_amount: 120}) + end + + context "with the run argument" do + subject { Rake::Task["fix_tslr_student_loan_amounts"].invoke } + + before { allow(ARGV).to receive(:[]) { "run" } } + + it "updates TSLR claims that have incorrect amounts" do + subject + expect(claim_1.reload.eligibility.student_loan_repayment_amount).to eq 150 + expect(claim_2.reload.eligibility.student_loan_repayment_amount).to eq 60 + end + end +end diff --git a/spec/models/student_loans_data_spec.rb b/spec/models/student_loans_data_spec.rb index 64dc1b2075..a197dd2374 100644 --- a/spec/models/student_loans_data_spec.rb +++ b/spec/models/student_loans_data_spec.rb @@ -1,9 +1,10 @@ require "rails_helper" RSpec.describe StudentLoansData do - let!(:applicant_a_slc_record_one) { create(:student_loans_data, nino: "QQ123456A", amount: 100, plan_type_of_deduction: 2) } - let!(:applicant_a_slc_record_two) { create(:student_loans_data, nino: "QQ123456A", amount: 50, plan_type_of_deduction: 1) } - let!(:applicant_b_slc_record_one) { create(:student_loans_data, nino: "QQ123456B", amount: 60, plan_type_of_deduction: 1) } + let!(:applicant_a_slc_record_one) { create(:student_loans_data, nino: "QQ123456A", date_of_birth: Date.new(1980, 1, 1), amount: 100, plan_type_of_deduction: 2) } + let!(:applicant_a_slc_record_two) { create(:student_loans_data, nino: "QQ123456A", date_of_birth: Date.new(1980, 1, 1), amount: 50, plan_type_of_deduction: 1) } + let!(:applicant_b_slc_record_one) { create(:student_loans_data, nino: "QQ123456B", date_of_birth: Date.new(1990, 1, 1), amount: 60, plan_type_of_deduction: 1) } + let!(:applicant_b_duplicate) { create(:student_loans_data, nino: "QQ123456B", date_of_birth: Date.new(1990, 1, 1), amount: 60, plan_type_of_deduction: 1) } def query_results_by(**) described_class.where(**)