-
Notifications
You must be signed in to change notification settings - Fork 40
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
OHRI-2295 Update OHRI endpoints to use SWR #1902
Open
CynthiaKamau
wants to merge
1
commit into
dev
Choose a base branch
from
swr-migration
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,21 @@ | ||
import { openmrsFetch } from '@openmrs/esm-framework'; | ||
import { fhirBaseUrl, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework'; | ||
import dayjs from 'dayjs'; | ||
import { | ||
finalHIVCodeConcept, | ||
finalPositiveHIVValueConcept, | ||
computedHIV_StatusConcept, | ||
encounterRepresentation, | ||
covidOutcomesCohortUUID, | ||
} from './constants'; | ||
import { encounterRepresentation } from './constants'; | ||
import useSWR from 'swr'; | ||
|
||
const BASE_WS_API_URL = '/ws/rest/v1/'; | ||
const BASE_FHIR_API_URL = '/ws/fhir2/R4/'; | ||
import { configSchema } from './config-schema'; | ||
import { type ReportDataArray } from './types'; | ||
|
||
export function fetchLastVisit(uuid: string) { | ||
return openmrsFetch(`/ws/fhir2/R4/Encounter?patient=${uuid}&_sort=-date&_count=1`); | ||
return openmrsFetch(`${fhirBaseUrl}/Encounter?patient=${uuid}&_sort=-date&_count=1`); | ||
} | ||
|
||
export function fetchPatientList(offSet: number = 0, pageSize: number = 10) { | ||
return openmrsFetch(`/ws/fhir2/R4/Patient?_getpagesoffset=${offSet}&_count=${pageSize}&_summary=data`); | ||
return openmrsFetch(`${fhirBaseUrl}/Patient?_getpagesoffset=${offSet}&_count=${pageSize}&_summary=data`); | ||
} | ||
|
||
export function fetchTodayClients() { | ||
let date = dayjs().format('YYYY-MM-DD'); | ||
return openmrsFetch(`/ws/fhir2/R4/Encounter?date=${date}`).then(({ data }) => { | ||
return openmrsFetch(`${fhirBaseUrl}/Encounter?date=${date}`).then(({ data }) => { | ||
if (data.entry?.length) { | ||
return cleanDuplicatePatientReferences(data); | ||
} | ||
|
@@ -35,7 +28,7 @@ export function fetchPatientsFromObservationCodeConcept(codeConcept: string, val | |
let startDate = dayjs().subtract(cutOffDays, 'day').format('YYYY-MM-DD'); | ||
|
||
return openmrsFetch( | ||
`/ws/fhir2/R4/Observation?code=${codeConcept}${valueConcept ? `&value-concept=${valueConcept}` : ''}${ | ||
`${fhirBaseUrl}/Observation?code=${codeConcept}${valueConcept ? `&value-concept=${valueConcept}` : ''}${ | ||
cutOffDays ? `&_lastUpdated=ge${startDate}&_lastUpdated=le${endDate}` : '' | ||
}`, | ||
).then(({ data }) => { | ||
|
@@ -54,13 +47,13 @@ function cleanDuplicatePatientReferences(data) { | |
patientRefs = Array.from(patientRefs); | ||
return Promise.all( | ||
patientRefs.map((ref) => { | ||
return openmrsFetch(BASE_FHIR_API_URL + ref); | ||
return openmrsFetch(fhirBaseUrl + `/` + ref); | ||
}), | ||
); | ||
} | ||
|
||
export function performPatientSearch(query, objectVersion) { | ||
return openmrsFetch(`${BASE_WS_API_URL}/patient?q=${query}${objectVersion ? `&v=${objectVersion}` : ''}`, { | ||
return openmrsFetch(`${restBaseUrl}/patient?q=${query}${objectVersion ? `&v=${objectVersion}` : ''}`, { | ||
method: 'GET', | ||
}); | ||
} | ||
|
@@ -77,27 +70,25 @@ export function getPatients(searchPhrase?: string, offset?: number, pageSize: nu | |
} | ||
|
||
export async function getCohort(cohortUuid: string, version?: string) { | ||
const { data } = await openmrsFetch( | ||
BASE_WS_API_URL + `cohortm/cohort/${cohortUuid}${version ? `?v=${version}` : ``}`, | ||
); | ||
const { data } = await openmrsFetch(restBaseUrl + `/cohortm/cohort/${cohortUuid}${version ? `?v=${version}` : ``}`); | ||
data.cohortMembers = data.cohortMembers.filter((member) => !member.voided); | ||
return data; | ||
} | ||
|
||
export async function getReportingCohort(cohortUuid: string, queryParams?: string[]) { | ||
const params = queryParams ? queryParams.join('&') : ''; | ||
const url = params ? `reportingrest/cohort/${cohortUuid}?${params}` : `reportingrest/cohort/${cohortUuid}`; | ||
const { data } = await openmrsFetch(BASE_WS_API_URL + url); | ||
const url = params ? `/reportingrest/cohort/${cohortUuid}?${params}` : `/reportingrest/cohort/${cohortUuid}`; | ||
const { data } = await openmrsFetch(restBaseUrl + url); | ||
return data; | ||
} | ||
|
||
export async function getReportingCohortMembers(cohortUuid: string, queryParams?: string[]) { | ||
const params = queryParams ? queryParams.join('&') : ''; | ||
const url = params ? `reportingrest/cohort/${cohortUuid}?${params}` : `reportingrest/cohort/${cohortUuid}`; | ||
const { data } = await openmrsFetch(BASE_WS_API_URL + url); | ||
const url = params ? `/reportingrest/cohort/${cohortUuid}?${params}` : `/reportingrest/cohort/${cohortUuid}`; | ||
const { data } = await openmrsFetch(restBaseUrl + url); | ||
return Promise.all( | ||
data.members.map((member) => { | ||
return openmrsFetch(BASE_WS_API_URL + `patient/${member.uuid}?v=full`); | ||
return openmrsFetch(restBaseUrl + `/patient/${member.uuid}?v=full`); | ||
}), | ||
); | ||
} | ||
|
@@ -106,8 +97,7 @@ export async function getCohorts(cohortTypeUuid?: string) { | |
const { | ||
data: { results, error }, | ||
} = await openmrsFetch( | ||
BASE_WS_API_URL + | ||
`cohortm/cohort?v=custom:(uuid,name,voided)${cohortTypeUuid ? `&cohortType=${cohortTypeUuid}` : ''}`, | ||
restBaseUrl + `/cohortm/cohort?v=custom:(uuid,name,voided)${cohortTypeUuid ? `&cohortType=${cohortTypeUuid}` : ''}`, | ||
); | ||
if (error) { | ||
throw error; | ||
|
@@ -116,7 +106,7 @@ export async function getCohorts(cohortTypeUuid?: string) { | |
} | ||
|
||
export function addPatientToCohort(patientUuid: string, cohortUuid: string) { | ||
return openmrsFetch(`${BASE_WS_API_URL}cohortm/cohortmember`, { | ||
return openmrsFetch(`${restBaseUrl}/cohortm/cohortmember`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
|
@@ -130,13 +120,13 @@ export function addPatientToCohort(patientUuid: string, cohortUuid: string) { | |
} | ||
|
||
export function evictCohortMembership(membershipUuid: string) { | ||
return openmrsFetch(`${BASE_WS_API_URL}cohortm/cohortmember/${membershipUuid}`, { method: 'DELETE' }); | ||
return openmrsFetch(`${restBaseUrl}/cohortm/cohortmember/${membershipUuid}`, { method: 'DELETE' }); | ||
} | ||
|
||
export async function getPatientListsForPatient(patientUuid: string) { | ||
const { | ||
data: { results, error }, | ||
} = await openmrsFetch(`${BASE_WS_API_URL}cohortm/cohortmember?patient=${patientUuid}&v=full`); | ||
} = await openmrsFetch(`${restBaseUrl}/cohortm/cohortmember?patient=${patientUuid}&v=full`); | ||
if (error) { | ||
throw error; | ||
} | ||
|
@@ -145,7 +135,7 @@ export async function getPatientListsForPatient(patientUuid: string) { | |
|
||
export function fetchPatientsFinalHIVStatus(patientUUID: string) { | ||
return openmrsFetch( | ||
`/ws/fhir2/R4/Observation?code=${finalHIVCodeConcept}&value-concept=${finalPositiveHIVValueConcept}&patient=${patientUUID}&_sort=-date&_count=1`, | ||
`${fhirBaseUrl}/Observation?code=${configSchema.obsConcepts._default.finalHIVCodeConcept}&value-concept=${configSchema.obsConcepts._default.finalPositiveHIVValueConcept}&patient=${patientUUID}&_sort=-date&_count=1`, | ||
).then(({ data }) => { | ||
if (data.entry?.length) { | ||
return data.entry[0].resource.valueCodeableConcept.coding[0].display; | ||
|
@@ -160,13 +150,13 @@ export function fetchPatientObservationFromEncounter( | |
observationCode: string, | ||
) { | ||
return openmrsFetch( | ||
`/ws/fhir2/R4/Observation?patient=${patientUUID}&encounter=${encounterUUID}&code=${observationCode}&_sort=-date&_count=1`, | ||
`${fhirBaseUrl}/Observation?patient=${patientUUID}&encounter=${encounterUUID}&code=${observationCode}&_sort=-date&_count=1`, | ||
); | ||
} | ||
|
||
export function fetchPatientComputedConcept_HIV_Status(patientUUID: string) { | ||
return openmrsFetch( | ||
`/ws/fhir2/R4/Observation?code=${computedHIV_StatusConcept}&value-concept=${computedHIV_StatusConcept}&patient=${patientUUID}&_sort=-date&_count=1`, | ||
`${fhirBaseUrl}/Observation?code=${configSchema.obsConcepts._default.computedHIV_StatusConcept}&value-concept=${configSchema.obsConcepts._default.computedHIV_StatusConcept}&patient=${patientUUID}&_sort=-date&_count=1`, | ||
).then(({ data }) => { | ||
if (data.entry?.length) { | ||
return data.entry[0].resource.valueCodeableConcept.coding[0].display; | ||
|
@@ -177,7 +167,7 @@ export function fetchPatientComputedConcept_HIV_Status(patientUUID: string) { | |
|
||
export function fetchPatientLastEncounter(patientUuid: string, encounterType) { | ||
const query = `encounterType=${encounterType}&patient=${patientUuid}`; | ||
return openmrsFetch(`/ws/rest/v1/encounter?${query}&v=${encounterRepresentation}`).then(({ data }) => { | ||
return openmrsFetch(`${restBaseUrl}/encounter?${query}&v=${encounterRepresentation}`).then(({ data }) => { | ||
if (data.results.length) { | ||
const sortedEncounters = data.results.sort( | ||
(firstEncounter, secondEncounter) => | ||
|
@@ -190,26 +180,8 @@ export function fetchPatientLastEncounter(patientUuid: string, encounterType) { | |
}); | ||
} | ||
|
||
export function fetchPatientCovidOutcome() { | ||
return openmrsFetch(`/ws/rest/v1/reportingrest/cohort/${covidOutcomesCohortUUID}`).then(({ data }) => { | ||
if (data.members?.length) { | ||
let patientRefs = data.members.map((member) => { | ||
return member.uuid; | ||
}); | ||
patientRefs = new Set([...patientRefs]); | ||
patientRefs = Array.from(patientRefs); | ||
return Promise.all( | ||
patientRefs.map((ref) => { | ||
return openmrsFetch(BASE_FHIR_API_URL + '/Person/' + ref); | ||
}), | ||
); | ||
} | ||
return []; | ||
}); | ||
} | ||
|
||
export function fetchConceptNameByUuid(conceptUuid: string) { | ||
return openmrsFetch(`/ws/rest/v1/concept/${conceptUuid}/name?limit=1`).then(({ data }) => { | ||
return openmrsFetch(`${restBaseUrl}/concept/${conceptUuid}/name?limit=1`).then(({ data }) => { | ||
if (data.results.length) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All these should use SWR and can be hooks since they are GET requests |
||
const concept = data.results[data.results.length - 1]; | ||
return concept.display; | ||
|
@@ -219,7 +191,7 @@ export function fetchConceptNameByUuid(conceptUuid: string) { | |
} | ||
|
||
export function fetchPatientRelationships(patientUuid: string) { | ||
return openmrsFetch(`${BASE_WS_API_URL}relationship?person=${patientUuid}&v=full`).then(({ data }) => { | ||
return openmrsFetch(`${restBaseUrl}/relationship?person=${patientUuid}&v=full`).then(({ data }) => { | ||
if (data.results.length) { | ||
return data.results; | ||
} | ||
|
@@ -228,35 +200,15 @@ export function fetchPatientRelationships(patientUuid: string) { | |
} | ||
|
||
export function fetchOpenMRSForms(formNames: string[]) { | ||
const fetch = (name) => openmrsFetch(`/ws/rest/v1/form?q=${name}&v=full`); | ||
const fetch = (name) => openmrsFetch(`${restBaseUrl}/form?q=${name}&v=full`); | ||
return Promise.all(formNames.map((name) => fetch(name))); | ||
} | ||
|
||
export function fetchFormsClobData(valueReferences: string[]) { | ||
const fetch = (ref: string) => openmrsFetch(`/ws/rest/v1/clobdata/${ref}`); | ||
const fetch = (ref: string) => openmrsFetch(`${restBaseUrl}/clobdata/${ref}`); | ||
return Promise.all(valueReferences?.map((ref) => fetch(ref))); | ||
} | ||
|
||
export async function fetchMambaReportData(reportId: string) { | ||
try { | ||
const response = await openmrsFetch(`ws/rest/v1/mamba/report?report_id=${reportId}`); | ||
const data = await response.json(); | ||
|
||
if (data && data.results && data.results.length > 0) { | ||
const record = data.results[0].record; | ||
|
||
for (const item of record) { | ||
return item.value ? parseInt(item.value, 10) : 0; | ||
} | ||
} | ||
|
||
return 0; | ||
} catch (error) { | ||
console.error(`Error fetching data for report_id=${reportId}: `, error); | ||
throw new Error(`Error fetching data for report_id=${reportId}: ${error}`); | ||
} | ||
} | ||
|
||
export function fetchEtlData( | ||
reportType: 'fetchMambaAncData' | 'MotherHivStatus', | ||
reportId?: string, | ||
|
@@ -285,10 +237,10 @@ export function fetchEtlData( | |
let endpoint = ''; | ||
switch (reportType) { | ||
case 'fetchMambaAncData': | ||
endpoint = `/ws/rest/v1/mamba/report?report_id=${reportId}&ptracker_id=${pTrackerId}&person_uuid=${patientUuid}`; | ||
endpoint = `${restBaseUrl}/mamba/report?report_id=${reportId}&ptracker_id=${pTrackerId}&person_uuid=${patientUuid}`; | ||
break; | ||
case 'MotherHivStatus': | ||
endpoint = `/ws/rest/v1/mamba/report?report_id=${reportId}&ptracker_id=${pTrackerId}&person_uuid=${patientUuid}`; | ||
endpoint = `${restBaseUrl}/mamba/report?report_id=${reportId}&ptracker_id=${pTrackerId}&person_uuid=${patientUuid}`; | ||
break; | ||
default: | ||
throw new Error('Invalid report type'); | ||
|
@@ -323,19 +275,19 @@ export async function getCohortList( | |
) { | ||
const params = queryParams ? queryParams.join('&') : ''; | ||
const cohortMembersUrl = params | ||
? `reportingrest/cohort/${cohortUuid}?${params}` | ||
: `reportingrest/cohort/${cohortUuid}`; | ||
const cohortUrl = `cohortm/cohort/${cohortUuid}?v=full`; | ||
? `/reportingrest/cohort/${cohortUuid}?${params}` | ||
: `/reportingrest/cohort/${cohortUuid}`; | ||
const cohortUrl = `/cohortm/cohort/${cohortUuid}?v=full`; | ||
|
||
const url = isReportingCohort ? cohortMembersUrl : cohortUrl; | ||
|
||
const { data } = await openmrsFetch(BASE_WS_API_URL + url); | ||
const { data } = await openmrsFetch(restBaseUrl + url); | ||
|
||
if (data?.members) { | ||
return Promise.all( | ||
data.members.map((member) => { | ||
return openmrsFetch( | ||
`/ws/rest/v1/encounter?encounterType=${encounterType}&patient=${member.uuid}&v=${encounterRepresentation}`, | ||
`${restBaseUrl}/encounter?encounterType=${encounterType}&patient=${member.uuid}&v=${encounterRepresentation}`, | ||
).then(({ data }) => { | ||
if (data.results.length) { | ||
const sortedEncounters = data.results.sort( | ||
|
@@ -355,15 +307,14 @@ export async function getCohortList( | |
return Promise.all( | ||
data.cohortMembers.map((member) => { | ||
return openmrsFetch( | ||
`/ws/rest/v1/encounter?encounterType=${encounterType}&patient=${member.patient.uuid}&v=${encounterRepresentation}`, | ||
`${restBaseUrl}/encounter?encounterType=${encounterType}&patient=${member.patient.uuid}&v=${encounterRepresentation}`, | ||
).then(({ data }) => { | ||
if (data.results.length) { | ||
const sortedEncounters = data.results | ||
.sort( | ||
(firstEncounter, secondEncounter) => | ||
new Date(secondEncounter.encounterDatetime).getTime() - | ||
new Date(firstEncounter.encounterDatetime).getTime(), | ||
); | ||
const sortedEncounters = data.results.sort( | ||
(firstEncounter, secondEncounter) => | ||
new Date(secondEncounter.encounterDatetime).getTime() - | ||
new Date(firstEncounter.encounterDatetime).getTime(), | ||
); | ||
return sortedEncounters[0]; | ||
} | ||
return null; | ||
|
@@ -372,3 +323,16 @@ export async function getCohortList( | |
); | ||
} | ||
} | ||
|
||
export function useMambaReportData(reportId: string) { | ||
const { data, error, isLoading } = useSWR<{ data: { results: ReportDataArray } }, Error>( | ||
`${restBaseUrl}/mamba/report?report_id=${reportId}`, | ||
openmrsFetch, | ||
); | ||
|
||
return { | ||
data: data?.data?.results[0]?.record[0]?.value ?? 0, | ||
error, | ||
isLoading, | ||
}; | ||
} |
36 changes: 0 additions & 36 deletions
36
packages/esm-commons-lib/src/components/banner-tags/.patient-status-tag.test.tsx
This file was deleted.
Oops, something went wrong.
25 changes: 16 additions & 9 deletions
25
packages/esm-commons-lib/src/components/banner-tags/patient-status-tag.component.tsx
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 |
---|---|---|
@@ -1,18 +1,25 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import React from 'react'; | ||
import { Tag } from '@carbon/react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { isPatientHivPositive } from './patientHivStatus'; | ||
import { usePatientsFinalHIVStatus } from './usePatientHivStatus'; | ||
import { useConfig } from '@openmrs/esm-framework'; | ||
|
||
export function PatientStatusBannerTag({ patientUuid }) { | ||
const { t } = useTranslation(); | ||
const [hivPositive, setHivPositive] = useState(false); | ||
const { obsConcepts } = useConfig(); | ||
const { isLoading, hivStatus, error } = usePatientsFinalHIVStatus( | ||
patientUuid, | ||
obsConcepts.finalHIVCodeConcept, | ||
obsConcepts.finalPositiveHIVValueConcept, | ||
); | ||
|
||
useEffect(() => { | ||
isPatientHivPositive(patientUuid).then((result) => setHivPositive(result)); | ||
}, [hivPositive, patientUuid]); | ||
if (isLoading) { | ||
return <p>{t('loading', 'Loading...')}</p>; | ||
} | ||
|
||
//TODO: Improve refresh time | ||
// forceRerender(); | ||
if (error) { | ||
return <p>{t('error', 'Error...')}</p>; | ||
} | ||
|
||
return <>{hivPositive && <Tag type="red">{t('hivPositive', 'HIV Positive')}</Tag>}</>; | ||
return <>{hivStatus && <Tag type="red">{t('hivPositive', 'HIV Positive')}</Tag>}</>; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CynthiaKamau I still see fetch functions, I think most of them can be transformed to use
SWR
and make them hooks. The ones I think my be hard might be the ones that are using promise.all but we can get a work around on them.