Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix MergeYears to accept retireDate passed #264 #279

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions src/library/user-state-manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,36 @@ const useYear62State = createPersistedState("Year62", global.sessionStorage);
* and the birth year to specified retirement date of the user,
* whichever is more complete.
* If no retire date is specified, set it to their 70th birthday
*
* TODO : allow specified retire date to update length of array
* e.g. let cleanRetireDate = retireDate === null? dayjs(birthDate).add(70,'year').toDate(): retireDate;
*/
function mergeYears(
earnings: EarningsRecord,
birthDate: Date | null
//,retireDate: Date | null
birthDate: Date | null,
retireDate: Date | null,
expectedLastEarningYear: number | null
): EarningsRecord {
/* if we don't have other info, just return what was passed in */
console.log(`expectedLastEarningYear:${expectedLastEarningYear}, birthdate: ${birthDate}, retireDate: ${retireDate}`)
if (birthDate === null) return earnings;
//let cleanRetireDate = retireDate === null? dayjs(birthDate).add(70,'year').toDate(): retireDate;
let cleanRetireDate = dayjs(birthDate).add(71,'year').toDate();
const birthYear = dayjs(birthDate).get('year');
const maxRetireDate = dayjs(birthDate).add(71,'year').toDate()
const retireYearFromReport = dayjs(retireDate!).get('year') || null;

let cleanRetireDate: Date;
if ((expectedLastEarningYear && retireDate) === null) {
cleanRetireDate = dayjs(birthDate).add(71,'year').toDate();
} else if ((expectedLastEarningYear === null) && (retireYearFromReport! - birthYear) < 71){
cleanRetireDate = retireDate!
} else if ((retireDate === null) && ((expectedLastEarningYear! - birthYear) < 71)){
cleanRetireDate = dayjs().year(expectedLastEarningYear!).toDate()
} else {
cleanRetireDate = maxRetireDate
}

const earningsRecord = earnings || {};

/*TODO(TDK) why do we need to create a new date object here, it is a string coming in? */
const birthYear = new Date(birthDate).getFullYear();
const startEmploymentYear =
(earnings && Object.keys(earnings) && Number(Object.keys(earnings)[0])) ||
birthYear + 18;
const retireYear = cleanRetireDate.getFullYear();
const retireYear = dayjs(cleanRetireDate).get('year');
const endYear = retireYear;

var tempRecord = {} as EarningsRecord;
Expand All @@ -123,7 +131,6 @@ function mergeYears(
}
return tempRecord;
}

/**
* Helper function to get a Date object equivalent to the start of the date given
*/
Expand Down Expand Up @@ -261,8 +268,8 @@ export default function UserStateManager(
/* setEarnings & mergeYears is unlikely to fire retroactively on changes
of birthdate or retire date ideally we'd have a single function for all
three alterations (or wrappers around each of them) if we go this way in the end*/
setEarnings: (earnings) => setEarnings(mergeYears(earnings, birthDate, retireDate)),
/* note the entries in the dependency array for birthDate and retireDate */
setEarnings: (earnings) => setEarnings(mergeYears(earnings, birthDate, retireDate, expectedLastEarningYear)),
/* note the entries in the dependency array for birthDate, retireDate, and expectedLastEarningYear */
setEarningsFormat,
setHaveSSAAccount,
setIsEmploymentCovered,
Expand Down Expand Up @@ -295,7 +302,8 @@ export default function UserStateManager(
setAwiTrendSelection,
setExpectedPercentageWageIncrease,
birthDate,
retireDate
retireDate,
expectedLastEarningYear
]
);

Expand Down