-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6302 from msupply-foundation/6263-suggest-next-da…
…te-from-vax-card 6263 suggest next date from vax card
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
client/packages/system/src/Encounter/DetailView/helpers.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { DateUtils } from '@common/intl'; | ||
import { VaccinationCardItemFragment } from '../../Vaccination/api/operations.generated'; | ||
import { getNextVaccinationEncounterDate } from './helpers'; | ||
|
||
describe('getNextVaccinationEncounterDate', () => { | ||
it('should return null if there are no items', () => { | ||
const items: VaccinationCardItemFragment[] = []; | ||
expect(getNextVaccinationEncounterDate(items)).toBeNull(); | ||
}); | ||
|
||
it('should return null if there are no suggested dates', () => { | ||
const items = [ | ||
{ id: '1', suggestedDate: null }, | ||
{ id: '2', suggestedDate: null }, | ||
] as VaccinationCardItemFragment[]; | ||
expect(getNextVaccinationEncounterDate(items)).toBeNull(); | ||
}); | ||
|
||
it('should return null if all suggested dates are in the past', () => { | ||
const items = [ | ||
{ id: '1', suggestedDate: '2021-01-01' }, | ||
{ id: '2', suggestedDate: '2021-01-02' }, | ||
] as VaccinationCardItemFragment[]; | ||
expect(getNextVaccinationEncounterDate(items)).toBeNull(); | ||
}); | ||
|
||
it('should return the nearest suggested date', () => { | ||
const oneMonthFromNow = DateUtils.addMonths(new Date(), 1); | ||
const items = [ | ||
{ | ||
id: '1', | ||
suggestedDate: DateUtils.addMonths(new Date(), 4).toISOString(), | ||
}, | ||
{ | ||
id: '2', | ||
suggestedDate: oneMonthFromNow.toISOString(), | ||
}, | ||
{ | ||
id: '3', | ||
suggestedDate: DateUtils.addMonths(new Date(), 2).toISOString(), | ||
}, | ||
] as VaccinationCardItemFragment[]; | ||
expect(getNextVaccinationEncounterDate(items)).toEqual(oneMonthFromNow); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
client/packages/system/src/Encounter/DetailView/helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { VaccinationCardItemFragment } from '../../Vaccination/api/operations.generated'; | ||
|
||
export function getNextVaccinationEncounterDate( | ||
items: VaccinationCardItemFragment[] | ||
): Date | null { | ||
const nextSuggestedDate = items.reduce<Date | null>( | ||
(nextSuggestedDate, vaccination) => { | ||
if (!vaccination.suggestedDate) return nextSuggestedDate; | ||
|
||
const vaccinationDate = new Date(vaccination.suggestedDate); | ||
|
||
if (vaccinationDate < new Date()) return nextSuggestedDate; | ||
|
||
if (!nextSuggestedDate || vaccinationDate < nextSuggestedDate) { | ||
return vaccinationDate; | ||
} | ||
return nextSuggestedDate; | ||
}, | ||
null | ||
); | ||
|
||
return nextSuggestedDate; | ||
} |