Skip to content

Commit

Permalink
OHRI-2256 Fix breaking cohort list due to infinite loop (#1887)
Browse files Browse the repository at this point in the history
  • Loading branch information
CynthiaKamau authored Jul 1, 2024
1 parent 908d905 commit b09063b
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 364 deletions.
58 changes: 58 additions & 0 deletions packages/esm-commons-lib/src/api.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,61 @@ function isInvalidValue(value) {
}
return false;
}

export async function getCohortList(
cohortUuid: string,
queryParams?: string[],
isReportingCohort?: boolean,
encounterType?: string,
) {
const params = queryParams ? queryParams.join('&') : '';
const cohortMembersUrl = params
? `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);

if (data?.members) {
return Promise.all(
data.members.map((member) => {
return openmrsFetch(
`/ws/rest/v1/encounter?encounterType=${encounterType}&patient=${member.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(),
);

return sortedEncounters[0];
}

return null;
});
}),
);
} else if (data?.cohortMembers) {
return Promise.all(
data.cohortMembers.map((member) => {
return openmrsFetch(
`/ws/rest/v1/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(),
);
return sortedEncounters[0];
}
return null;
});
}),
);
}
}
Loading

0 comments on commit b09063b

Please sign in to comment.