Skip to content

Commit

Permalink
Merge pull request #3340 from DFE-Digital/CAPT-1877-fix-slc-amounts-o…
Browse files Browse the repository at this point in the history
…n-tslr-claims

[CAPT-1877] fix and rake task for duplicate SLC data entries
  • Loading branch information
alkesh authored Oct 23, 2024
2 parents ad38396 + 7f2c79c commit 7eb9c54
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/models/student_loans_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions lib/tasks/fix_student_loan_repayment_amounts.rake
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions spec/lib/tasks/fix_student_loan_repayment_amounts_spec.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions spec/models/student_loans_data_spec.rb
Original file line number Diff line number Diff line change
@@ -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(**)
Expand Down

0 comments on commit 7eb9c54

Please sign in to comment.