Skip to content

Commit 673fcc2

Browse files
committed
OHRI-2295 Update OHRI endpoints to use SWR
1 parent 41f1f66 commit 673fcc2

File tree

14 files changed

+325
-450
lines changed

14 files changed

+325
-450
lines changed

packages/esm-commons-lib/src/api.resource.ts

+25-39
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import { openmrsFetch } from '@openmrs/esm-framework';
22
import dayjs from 'dayjs';
3-
import {
4-
finalHIVCodeConcept,
5-
finalPositiveHIVValueConcept,
6-
computedHIV_StatusConcept,
7-
encounterRepresentation,
8-
covidOutcomesCohortUUID,
9-
} from './constants';
3+
import { BASE_FHIR_API_URL, BASE_WS_API_URL, encounterRepresentation } from './constants';
104
import useSWR from 'swr';
11-
12-
const BASE_WS_API_URL = '/ws/rest/v1/';
13-
const BASE_FHIR_API_URL = '/ws/fhir2/R4/';
5+
import { configSchema } from './config-schema';
146

157
export function fetchLastVisit(uuid: string) {
168
return openmrsFetch(`/ws/fhir2/R4/Encounter?patient=${uuid}&_sort=-date&_count=1`);
@@ -145,7 +137,7 @@ export async function getPatientListsForPatient(patientUuid: string) {
145137

146138
export function fetchPatientsFinalHIVStatus(patientUUID: string) {
147139
return openmrsFetch(
148-
`/ws/fhir2/R4/Observation?code=${finalHIVCodeConcept}&value-concept=${finalPositiveHIVValueConcept}&patient=${patientUUID}&_sort=-date&_count=1`,
140+
`/ws/fhir2/R4/Observation?code=${configSchema.obsConcepts._default.finalHIVCodeConcept}&value-concept=${configSchema.obsConcepts._default.finalPositiveHIVValueConcept}&patient=${patientUUID}&_sort=-date&_count=1`,
149141
).then(({ data }) => {
150142
if (data.entry?.length) {
151143
return data.entry[0].resource.valueCodeableConcept.coding[0].display;
@@ -166,7 +158,7 @@ export function fetchPatientObservationFromEncounter(
166158

167159
export function fetchPatientComputedConcept_HIV_Status(patientUUID: string) {
168160
return openmrsFetch(
169-
`/ws/fhir2/R4/Observation?code=${computedHIV_StatusConcept}&value-concept=${computedHIV_StatusConcept}&patient=${patientUUID}&_sort=-date&_count=1`,
161+
`/ws/fhir2/R4/Observation?code=${configSchema.obsConcepts._default.computedHIV_StatusConcept}&value-concept=${configSchema.obsConcepts._default.computedHIV_StatusConcept}&patient=${patientUUID}&_sort=-date&_count=1`,
170162
).then(({ data }) => {
171163
if (data.entry?.length) {
172164
return data.entry[0].resource.valueCodeableConcept.coding[0].display;
@@ -191,7 +183,9 @@ export function fetchPatientLastEncounter(patientUuid: string, encounterType) {
191183
}
192184

193185
export function fetchPatientCovidOutcome() {
194-
return openmrsFetch(`/ws/rest/v1/reportingrest/cohort/${covidOutcomesCohortUUID}`).then(({ data }) => {
186+
return openmrsFetch(
187+
`/ws/rest/v1/reportingrest/cohort/${configSchema.obsConcepts._default.covidOutcomesCohortUUID}`,
188+
).then(({ data }) => {
195189
if (data.members?.length) {
196190
let patientRefs = data.members.map((member) => {
197191
return member.uuid;
@@ -237,26 +231,6 @@ export function fetchFormsClobData(valueReferences: string[]) {
237231
return Promise.all(valueReferences?.map((ref) => fetch(ref)));
238232
}
239233

240-
export async function fetchMambaReportData(reportId: string) {
241-
try {
242-
const response = await openmrsFetch(`ws/rest/v1/mamba/report?report_id=${reportId}`);
243-
const data = await response.json();
244-
245-
if (data && data.results && data.results.length > 0) {
246-
const record = data.results[0].record;
247-
248-
for (const item of record) {
249-
return item.value ? parseInt(item.value, 10) : 0;
250-
}
251-
}
252-
253-
return 0;
254-
} catch (error) {
255-
console.error(`Error fetching data for report_id=${reportId}: `, error);
256-
throw new Error(`Error fetching data for report_id=${reportId}: ${error}`);
257-
}
258-
}
259-
260234
export function fetchEtlData(
261235
reportType: 'fetchMambaAncData' | 'MotherHivStatus',
262236
reportId?: string,
@@ -358,12 +332,11 @@ export async function getCohortList(
358332
`/ws/rest/v1/encounter?encounterType=${encounterType}&patient=${member.patient.uuid}&v=${encounterRepresentation}`,
359333
).then(({ data }) => {
360334
if (data.results.length) {
361-
const sortedEncounters = data.results
362-
.sort(
363-
(firstEncounter, secondEncounter) =>
364-
new Date(secondEncounter.encounterDatetime).getTime() -
365-
new Date(firstEncounter.encounterDatetime).getTime(),
366-
);
335+
const sortedEncounters = data.results.sort(
336+
(firstEncounter, secondEncounter) =>
337+
new Date(secondEncounter.encounterDatetime).getTime() -
338+
new Date(firstEncounter.encounterDatetime).getTime(),
339+
);
367340
return sortedEncounters[0];
368341
}
369342
return null;
@@ -372,3 +345,16 @@ export async function getCohortList(
372345
);
373346
}
374347
}
348+
349+
export function useMambaReportData(reportId: string) {
350+
const { data, error, isLoading } = useSWR<{ data: { results: any } }, Error>(
351+
`${BASE_WS_API_URL}/mamba/report?report_id=${reportId}`,
352+
openmrsFetch,
353+
);
354+
355+
return {
356+
data: data?.data ? data?.data?.results : '0',
357+
error,
358+
isLoading,
359+
};
360+
}

packages/esm-commons-lib/src/components/banner-tags/.patient-status-tag.test.tsx

-36
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
import React, { useEffect, useState } from 'react';
1+
import React from 'react';
22
import { Tag } from '@carbon/react';
33
import { useTranslation } from 'react-i18next';
4-
import { isPatientHivPositive } from './patientHivStatus';
4+
import { usePatientsFinalHIVStatus } from './usePatientHivStatus';
5+
import { useConfig } from '@openmrs/esm-framework';
56

67
export function PatientStatusBannerTag({ patientUuid }) {
78
const { t } = useTranslation();
8-
const [hivPositive, setHivPositive] = useState(false);
9+
const { obsConcepts } = useConfig();
10+
const { isLoading, hivStatus, error } = usePatientsFinalHIVStatus(
11+
patientUuid,
12+
obsConcepts.finalHIVCodeConcept,
13+
obsConcepts.finalPositiveHIVValueConcept,
14+
);
915

10-
useEffect(() => {
11-
isPatientHivPositive(patientUuid).then((result) => setHivPositive(result));
12-
}, [hivPositive, patientUuid]);
16+
if (isLoading) {
17+
return <p>{t('loading', 'Loading...')}</p>;
18+
}
1319

14-
//TODO: Improve refresh time
15-
// forceRerender();
20+
if (error) {
21+
return <p>{t('error', 'Error...')}</p>;
22+
}
1623

17-
return <>{hivPositive && <Tag type="red">{t('hivPositive', 'HIV Positive')}</Tag>}</>;
24+
return <>{hivStatus && <Tag type="red">{t('hivPositive', 'HIV Positive')}</Tag>}</>;
1825
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
4+
import { PatientStatusBannerTag } from './patient-status-tag.component';
5+
import { usePatientsFinalHIVStatus } from './usePatientHivStatus';
6+
import { useConfig } from '@openmrs/esm-framework';
7+
8+
const mockedUsePatientsFinalHIVStatus = jest.mocked(usePatientsFinalHIVStatus);
9+
const mockUseConfig = jest.mocked(useConfig);
10+
11+
jest.mock('./usePatientHivStatus', () => {
12+
const originalModule = jest.requireActual('./usePatientHivStatus');
13+
14+
return {
15+
...originalModule,
16+
usePatientsFinalHIVStatus: jest.fn().mockImplementation(() => ({
17+
hivStatus: true,
18+
isLoading: false,
19+
})),
20+
};
21+
});
22+
23+
describe('PatientStatusBannerTag', () => {
24+
beforeEach(() => {
25+
jest.clearAllMocks();
26+
mockUseConfig.mockReturnValue({
27+
obsConcepts: {
28+
finalHIVCodeConcept: 'e16b0068-b6a2-46b7-aba9-e3be00a7b4ab',
29+
finalPositiveHIVValueConcept: '703AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
30+
},
31+
});
32+
});
33+
34+
const samplePatientUuid = '703AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
35+
36+
it('renders red tag when patient is HIV positive', async () => {
37+
render(<PatientStatusBannerTag patientUuid={samplePatientUuid} />);
38+
expect(screen.getByText(/HIV Positive/i)).toBeInTheDocument();
39+
});
40+
41+
it('does not render red tag when patient is not HIV positive', async () => {
42+
mockedUsePatientsFinalHIVStatus.mockReturnValue({ hivStatus: false, isLoading: false, error: null });
43+
render(<PatientStatusBannerTag patientUuid={samplePatientUuid} />);
44+
expect(screen.queryByText('HIV Positive')).not.toBeInTheDocument();
45+
});
46+
});

packages/esm-commons-lib/src/components/banner-tags/patientHivStatus.ts

-44
This file was deleted.

packages/esm-commons-lib/src/components/banner-tags/patientStatus.test.ts

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { fhirBaseUrl, openmrsFetch } from '@openmrs/esm-framework';
2+
import useSWR from 'swr';
3+
4+
export function usePatientsFinalHIVStatus(
5+
patientUuid: string,
6+
finalHIVCodeConcept: string,
7+
finalPositiveHIVValueConcept: string,
8+
) {
9+
const url = `${fhirBaseUrl}/Observation?code=${finalHIVCodeConcept}&value-concept=${finalPositiveHIVValueConcept}&patient=${patientUuid}&_sort=-date&_count=1`;
10+
const { data, error, isLoading, mutate } = useSWR<{ data: any }, Error>(url, openmrsFetch);
11+
12+
const hivStatusResult = data?.data?.entry[0].resource.valueCodeableConcept.coding[0].display;
13+
const hivStatus = hivStatusResult.toLowerCase().includes('positive') ? true : false;
14+
15+
return { hivStatus: hivStatus, isLoading: isLoading, error: error };
16+
}

0 commit comments

Comments
 (0)