Skip to content

Commit

Permalink
Refactor query
Browse files Browse the repository at this point in the history
Refactors the CTE to build a generic eligibilities table we can join to.
Now when we add a new policy the payroll query should automatically pick
it up. Ideally we'd rename the "student_loan_repayment_amount" column in
the DB but that's quite involved, so for the time being we define an
'award_amount_column' column on the policy which returns the name of the
DB column we need to check to get the award amount.
  • Loading branch information
rjlynch committed Nov 8, 2024
1 parent bd422ae commit 5d465f9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 38 deletions.
4 changes: 4 additions & 0 deletions app/models/base_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@ def approvable?(claim)
def decision_deadline_date(claim)
(claim.submitted_at + Claim::DECISION_DEADLINE).to_date
end

def award_amount_column
"award_amount"
end
end
56 changes: 18 additions & 38 deletions app/models/payroll_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,50 +73,30 @@ def payments_count
end

def line_items(policy, filter: :all)
sql = <<~SQL
WITH claims_with_award_amount AS (
eligibilities_cte = "WITH eligibilities AS("
eligibilities_cte += Policies::POLICIES.map do |policy|
<<~SQL
SELECT
claims.*,
CASE
WHEN early_career_payments_eligibilities.id IS NOT NULL
THEN early_career_payments_eligibilities.award_amount
WHEN further_education_payments_eligibilities.id IS NOT NULL
THEN further_education_payments_eligibilities.award_amount
WHEN international_relocation_payments_eligibilities.id IS NOT NULL
THEN international_relocation_payments_eligibilities.award_amount
WHEN levelling_up_premium_payments_eligibilities.id IS NOT NULL
THEN levelling_up_premium_payments_eligibilities.award_amount
WHEN student_loans_eligibilities.id IS NOT NULL
THEN student_loans_eligibilities.student_loan_repayment_amount
END AS award_amount
FROM claims
LEFT JOIN early_career_payments_eligibilities
ON claims.eligibility_id = early_career_payments_eligibilities.id
AND claims.eligibility_type = 'Policies::EarlyCareerPayments::Eligibility'
LEFT JOIN further_education_payments_eligibilities
ON claims.eligibility_id = further_education_payments_eligibilities.id
AND claims.eligibility_type = 'Policies::FurtherEducationPayments::Eligibility'
LEFT JOIN international_relocation_payments_eligibilities
ON claims.eligibility_id = international_relocation_payments_eligibilities.id
AND claims.eligibility_type = 'Policies::InternationalRelocationPayments::Eligibility'
LEFT JOIN levelling_up_premium_payments_eligibilities
ON claims.eligibility_id = levelling_up_premium_payments_eligibilities.id
AND claims.eligibility_type = 'Policies::LevellingUpPremiumPayments::Eligibility'
LEFT JOIN student_loans_eligibilities
ON claims.eligibility_id = student_loans_eligibilities.id
AND claims.eligibility_type = 'Policies::StudentLoans::Eligibility'
)
id,
#{policy.award_amount_column} AS award_amount,
'#{policy::Eligibility}' AS eligibility_type
FROM #{policy::Eligibility.table_name}
SQL
end.join(" UNION ALL ")
eligibilities_cte += ")"

sql = <<~SQL
#{eligibilities_cte}
SELECT
/* A topup is always paid in different payment/payroll_run than the main claim was */
COALESCE(topups.award_amount, claims.award_amount) AS award_amount
COALESCE(topups.award_amount, eligibilities.award_amount) AS award_amount
FROM payments
JOIN claim_payments ON claim_payments.payment_id = payments.id
JOIN claims_with_award_amount AS claims
ON claims.id = claim_payments.claim_id
LEFT JOIN topups
ON topups.claim_id = claims.id
JOIN claims ON claims.id = claim_payments.claim_id
JOIN eligibilities
ON claims.eligibility_id = eligibilities.id
AND claims.eligibility_type = eligibilities.eligibility_type
LEFT JOIN topups ON topups.claim_id = claims.id
WHERE payments.payroll_run_id = '#{id}'
SQL

Expand Down
4 changes: 4 additions & 0 deletions app/models/policies/student_loans.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,9 @@ def current_financial_year(format = :default)
def payroll_file_name
"TSLR"
end

def award_amount_column
"student_loan_repayment_amount"
end
end
end

0 comments on commit 5d465f9

Please sign in to comment.