-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3340 from DFE-Digital/CAPT-1877-fix-slc-amounts-o…
…n-tslr-claims [CAPT-1877] fix and rake task for duplicate SLC data entries
- Loading branch information
Showing
4 changed files
with
68 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters