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

(refactor) Tweak calcMonthsOnART helper logic #441

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/utils/common-expression-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe('CommonExpressionHelpers', () => {
it('should return the correct number of months on ART', () => {
const artStartDate = new Date('2020-01-01');
const today = new Date();
const monthsOnART = Math.floor((today.getTime() - artStartDate.getTime()) / (1000 * 60 * 60 * 24 * 30));
const monthsOnART = dayjs(today).diff(dayjs(artStartDate), 'months');
expect(helpers.calcMonthsOnART(artStartDate)).toBe(monthsOnART);
});

Expand Down
17 changes: 10 additions & 7 deletions src/utils/common-expression-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,22 @@ export class CommonExpressionHelpers {
};

calcMonthsOnART = (artStartDate: Date) => {
if (artStartDate == null) return null;
if (artStartDate == null) {
return null;
}

if (!(artStartDate instanceof Date)) {
throw new Error('DateFormatException: value passed is not a valid date');
}

let today = new Date();
let resultMonthsOnART: number;
let artInDays = Math.round((today.getTime() - artStartDate.getTime?.()) / 86400000);
if (artStartDate && artInDays >= 30) {
resultMonthsOnART = Math.floor(artInDays / 30);
const today = new Date();
const artInDays = Math.round((today.getTime() - artStartDate.getTime()) / 86400000);

if (artInDays < 30) {
return 0;
}
return artStartDate ? resultMonthsOnART : null;

return dayjs(today).diff(artStartDate, 'month');
};

calcViralLoadStatus = (viralLoadCount: number) => {
Expand Down
Loading