Skip to content

Commit

Permalink
Merge pull request #6302 from msupply-foundation/6263-suggest-next-da…
Browse files Browse the repository at this point in the history
…te-from-vax-card

6263 suggest next date from vax card
  • Loading branch information
lache-melvin authored Jan 29, 2025
2 parents ce072e0 + 18510e5 commit 0f4dc76
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
12 changes: 12 additions & 0 deletions client/packages/system/src/Encounter/DetailView/DetailView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import { getLogicalStatus } from '../utils';
import { PatientTabValue } from '../../Patient/PatientView/PatientView';
import { VaccinationCard } from '../../Vaccination/Components/VaccinationCard';
import { ScheduleNextEncounterModal } from './ScheduleNextEncounterModal';
import { usePatientVaccineCard } from '../../Vaccination/api/usePatientVaccineCard';
import { getNextVaccinationEncounterDate } from './helpers';

const getPatientBreadcrumbSuffix = (
encounter: EncounterFragment,
Expand Down Expand Up @@ -166,6 +168,15 @@ export const DetailView: FC = () => {
isError,
} = useEncounter.document.byId(id);

// If this is a vaccination encounter, we want to use the suggested
// next vaccination dates for the next encounter
const {
query: { data: vaccineCard },
} = usePatientVaccineCard(encounter?.programEnrolment?.id ?? '');
const suggestedNextEncounterDate = getNextVaccinationEncounterDate(
vaccineCard?.items ?? []
);

const handleSave = useEncounter.document.upsertDocument(
encounter?.patient.id ?? '',
encounter?.type ?? ''
Expand Down Expand Up @@ -363,6 +374,7 @@ export const DetailView: FC = () => {
encounterConfig={encounter.document.documentRegistry}
onClose={nextEncounterModal.toggleOff}
patientId={encounter.patient.id ?? ''}
suggestedDate={suggestedNextEncounterDate}
/>
)}
<Toolbar onChange={updateEncounter} encounter={encounter} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ export const ScheduleNextEncounterModal = ({
patientId,
encounterConfig,
onClose,
suggestedDate,
}: {
patientId: string;
encounterConfig: DocumentRegistryFragment;
onClose: () => void;
suggestedDate: Date | null;
}) => {
const { user, storeId } = useAuthContext();
const t = useTranslation();
const [draft, setDraft] = useState<EncounterSchema>({
createdDatetime: new Date().toISOString(),
startDatetime: suggestedDate?.toISOString(),
createdBy: { id: user?.id ?? '', username: user?.name ?? '' },
status: EncounterNodeStatus.Pending,
location: {
Expand Down
45 changes: 45 additions & 0 deletions client/packages/system/src/Encounter/DetailView/helpers.test.ts
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 client/packages/system/src/Encounter/DetailView/helpers.ts
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;
}

0 comments on commit 0f4dc76

Please sign in to comment.