Skip to content

Commit

Permalink
(refactor) Tweak calcMonthsOnART helper logic (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
denniskigen authored Dec 4, 2024
1 parent ece0e7f commit ce5f457
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
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

0 comments on commit ce5f457

Please sign in to comment.