Skip to content

Commit

Permalink
added back comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-solo committed Oct 3, 2023
1 parent 2e8400e commit 543cf4b
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 12 deletions.
6 changes: 3 additions & 3 deletions __tests__/pages/api/oasDeferral.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ describe('OasDEferral', () => {
})

// There is a bug #141941. Should be retested when the bug is fixed
/* CALC-145
/* CALC-145 */
it('should pass the 145 test - OAS-CALC-145', async () => {
const desiredName = 'CALC-145' // Replace with the desired name
const extractedPayload = getTransformedPayloadByName(filePath, desiredName)
Expand All @@ -457,15 +457,15 @@ describe('OasDEferral', () => {
expectAlwTooOld(res)
expectAlwsMarital(res)
//Future Benefit // To Check with Vero or Lorelei - test case it's 72 but in the code is 71
expectFutureOasGisBenefitEligible(res, 71, 237.52, 1547.4, 0)
expectFutureOasGisBenefitEligible(res, 72, 237.52, 1547.4, 0)

//partner results
expectOasNotEligible(res, true)
expectGisNotEligible(res, ResultReason.OAS, true)

//Future Benefit
expectFutureOasGisBenefitEligible(res, 66, 174.65, 1007.04, 0, true)
})*/
})
/* CALC-146 */
it('should pass the 146 test - OAS-CALC-146', async () => {
const desiredName = 'CALC-146' // Replace with the desired name
Expand Down
12 changes: 7 additions & 5 deletions utils/api/futureHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { RequestSchema as schema } from './definitions/schemas'
import {
buildQuery,
getAgeArray,
getAgeArray2,
OasEligibility,
AlwsEligibility,
} from './helpers/utils'
Expand Down Expand Up @@ -138,6 +139,7 @@ export class FutureHandler {
const partnerAge = Number(this.query.partnerAge)
const clientRes = Number(this.query.yearsInCanadaSince18)
const partnerRes = Number(this.query.partnerYearsInCanadaSince18)
const partnerOnlyCanada = this.query.partnerLivedOnlyInCanada

const clientDeferralMeta =
this.currentHandler.benefitResults?.client?.oas?.entitlement?.deferral
Expand Down Expand Up @@ -167,18 +169,18 @@ export class FutureHandler {
const ages = [age, partnerAge]
if (ages.some((age) => isNaN(age))) return this.futureResultsObj

// const futureAges = getAgeArray2(ages)

const futureAges = getAgeArray({
client: { age, res: this.query.livedOnlyInCanada ? 40 : clientRes },
partner: {
age: partnerAge,
res:
this.query.partnerLivedOnlyInCanada ||
Number(this.query.partnerAge) < 60
? 40
: partnerRes,
res: partnerOnlyCanada === 'true' || partnerAge < 60 ? 40 : partnerRes,
},
})

console.log('futureAges', futureAges)

let result = this.futureResultsObj
if (futureAges.length !== 0) {
const clientResults = []
Expand Down
201 changes: 197 additions & 4 deletions utils/api/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function getEligibleBenefits(benefits) {
}

export function getAgeArray(residencyData) {
console.log('residencyData', residencyData)
let [userAge, partnerAge] = [
residencyData.client.age,
residencyData.partner.age,
Expand All @@ -34,10 +35,7 @@ export function getAgeArray(residencyData) {
const result = []

function yearsUntilOAS(age, residency) {
if (
(age >= 65 && residency >= 10) ||
(age + (10 - residency) > 65 && age < 65)
) {
if (age >= 65 && residency >= 10) {
return null
}

Expand Down Expand Up @@ -67,17 +65,212 @@ export function getAgeArray(residencyData) {
let pALW = yearsUntilALW(partnerAge, partnerRes)
let pOAS = yearsUntilOAS(partnerAge, partnerRes)

console.log('cALW', cALW)
console.log('cOAS', cOAS)
console.log('pALW', pALW)
console.log('pOAS', pOAS)

let arr = [cALW, cOAS, pALW, pOAS]
if (arr.every((el) => el === null)) break

const years = Math.min(...arr.filter((num) => num !== null))
console.log('years', years)
userAge += years
partnerAge += years
userRes += years
partnerRes += years
result.push([userAge, partnerAge])
}

console.log('result', result)
return result
}

export function getAgeArray3(residencyData) {
let [userAge, partnerAge] = [
residencyData.client.age,
residencyData.partner.age,
]
let [userRes, partnerRes] = [
residencyData.client.res,
residencyData.partner.res,
]

const resLimit = 10
const result = []

// Early return if any element is missing
if ([userAge, partnerAge, userRes, partnerRes].some((el) => isNaN(el)))
return []

// Helper function to check ALW eligibility
function isEligibleForALW(age, residency, partnerOASEligibility) {
return (
age >= 60 && age < 65 && residency >= resLimit && partnerOASEligibility
)
}

// Helper function to check OAS eligibility
function isEligibleForOAS(age, residency) {
return age >= 65 && residency >= resLimit
}

// Tracking initial eligibility
let userPrevEligibility = [
isEligibleForALW(
userAge,
userRes,
isEligibleForOAS(partnerAge, partnerRes)
),
isEligibleForOAS(userAge, userRes),
]

let partnerPrevEligibility = [
isEligibleForALW(
partnerAge,
partnerRes,
isEligibleForOAS(userAge, userRes)
),
isEligibleForOAS(partnerAge, partnerRes),
]

// Iterate until both are eligible for OAS
while (
!(
isEligibleForOAS(userAge, userRes) &&
isEligibleForOAS(partnerAge, partnerRes)
)
) {
// Increase age and residency for both user and partner
userAge++
userRes++
partnerAge++
partnerRes++

// Check current eligibility
const userCurrEligibility = [
isEligibleForALW(
userAge,
userRes,
isEligibleForOAS(partnerAge, partnerRes)
),
isEligibleForOAS(userAge, userRes),
]

const partnerCurrEligibility = [
isEligibleForALW(
partnerAge,
partnerRes,
isEligibleForOAS(userAge, userRes)
),
isEligibleForOAS(partnerAge, partnerRes),
]

// If eligibility changed for either person, add to result
if (
userCurrEligibility.join() !== userPrevEligibility.join() ||
partnerCurrEligibility.join() !== partnerPrevEligibility.join()
) {
result.push([userAge, partnerAge])
}

// Update previous eligibility
userPrevEligibility = userCurrEligibility
partnerPrevEligibility = partnerCurrEligibility
}

console.log('result', result)
return result
}

export function getAgeArray2(ages: number[]) {
// {66.42: 40, 60.92: 10} == [70.5, 65]
// uALW: null
// uOAS: null
// pALW: null
// pOAS: 4.08

// {68.75: 12, 59.25: 40} == [69.5,60], [74.5,65]
// uALW: null // uALW: null
// uOAS: null // uOAS: null
// pALW: 0.75 // pALW: null
// pOAS: 5.75 // pOAS: 5

// {70: 15, 64: 8} -> (72:17, 66:10) == [72,66]
// {70: 15, 66: 8} -> (72:17, 68:10) == [72,68]
// {70.5: 15, 64.5: 8} -> [72.5, 66.5]
// 60: 9, 65: 10 -> 61: 10, 66: 11 -> 65: 14, 70: 15 == [61, 66], [65, 70]

// uALW: 1 // uALW: null (if already ALW eligible return null) // uALW: null
// uOAS: 5 // uOAS: 4 // uOAS: null
// pALW: null // pALW: null // pALW: null
// pOAS: null // pOAS: null // pOAS: null

// 45: 1, 62: 20 -> (48: 4, 65: 23), (60: 16, 77: 35), (65: 21, 82:42) == [48, 65], [60, 77], [65, 82]

// how to determine next milestone
// if under 65
// increase age and res by 1

// noone eligible -> client will eligible first -> then a year later partner will be eligible -> EVERYONE ELIGIBLE FOR OAS, DONE
// {68: 5, 69: 4} -> (73: 10, 74: 9) -> (74: 11, 75: 10) => [73, 74], [74, 75]

// 53, 59 -> (60,66), (65,71)
// 53:2, 59:1 -> (54:3, 60:2), (59: 8, 65: 7), (60: 9, 66:8) -> (61:10, 67:9) -> (62:11, 68:10) -> (65:14, 71:13) == [62, 68], [65,71]
const [userAge, partnerAge] = ages
const ageDiff = Math.abs(userAge - partnerAge)

const result = []

// while both res are below 10
// add 1 to everything

if (ageDiff > 5 && ages.some((age) => age < 60)) {
while (!ages.some((age) => age === 60)) {
// should be "until some age is not over 60"
let [userAge, partnerAge] = ages
if (userAge > partnerAge && userAge < 65) {
// make this 65 user oas eligibility age
let diff = 65 - userAge // client oas eligibility age - user age
userAge += diff
partnerAge += diff
} else if (userAge < partnerAge) {
let diff = 60 - userAge // user ALW eli age instead of 60
userAge += diff
partnerAge += diff
} else {
let diff = 60 - partnerAge // partner ALW age instead of 60
userAge += diff
partnerAge += diff
}
ages = [userAge, partnerAge]
result.push(ages)
}
}

while (!ages.every((age) => age >= 65)) {
// while both are not eligible
let [userAge, partnerAge] = ages
if (userAge >= 65 || partnerAge >= 65) {
if (userAge < partnerAge) {
let diff = 65 - userAge // partner OAS eligibility age instead of 65
userAge += diff
partnerAge += diff
} else {
let diff = 65 - partnerAge
userAge += diff
partnerAge += diff
}
} else {
const maxAge = Math.max(userAge, partnerAge)
const diff = 65 - maxAge
userAge += diff
partnerAge += diff
}
ages = [userAge, partnerAge]
result.push(ages)
}

return result
}

Expand Down

0 comments on commit 543cf4b

Please sign in to comment.