Skip to content
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

(fix) Use order reasons should correctly chunk concept references and load all of them #1813

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
'^@carbon/charts-react$': path.resolve(__dirname, '__mocks__', '@carbon__charts-react.ts'),
'^dexie$': require.resolve('dexie'),
'^lodash-es/(.*)$': 'lodash/$1',
'^lodash-es$': 'lodash',
'^react-i18next$': path.resolve(__dirname, '__mocks__', 'react-i18next.js'),
'^uuid$': path.resolve(__dirname, 'node_modules', 'uuid', 'dist', 'index.js'),
},
Expand Down
49 changes: 24 additions & 25 deletions packages/esm-patient-labs-app/src/lab-orders/api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import useSWR, { mutate } from 'swr';
import { type FetchResponse, openmrsFetch, useConfig, restBaseUrl, showSnackbar } from '@openmrs/esm-framework';
import { type ConfigObject } from '../config-schema';
import { useCallback, useMemo } from 'react';
import type { OrderPost, PatientOrderFetchResponse, LabOrderBasketItem } from '@openmrs/esm-patient-common-lib';
import useSWRImmutable from 'swr/immutable';
import { chunk } from 'lodash-es';
import { type FetchResponse, openmrsFetch, useConfig, restBaseUrl, showSnackbar } from '@openmrs/esm-framework';
import type { OrderPost, PatientOrderFetchResponse, LabOrderBasketItem } from '@openmrs/esm-patient-common-lib';
import { type ConfigObject } from '../config-schema';

export const careSettingUuid = '6f0c9a92-6f24-11e3-af88-005056821db0';
/**
Expand Down Expand Up @@ -44,16 +45,14 @@ export function usePatientLabOrders(patientUuid: string, status: 'ACTIVE' | 'any
}

export function useOrderReasons(conceptUuids: Array<string>) {
const shouldFetch = conceptUuids && conceptUuids.length > 0;
const url = shouldFetch ? getConceptReferenceUrls(conceptUuids) : null;
const { data, error, isLoading } = useSWRImmutable<FetchResponse<ConceptResponse>, Error>(
shouldFetch ? `${restBaseUrl}/${url[0]}` : null,
openmrsFetch,
const { data, error, isLoading } = useSWRImmutable<Array<FetchResponse<ConceptReferenceResponse>>, Error>(
conceptUuids && conceptUuids.length > 0 ? getConceptReferenceUrls(conceptUuids) : null,
(key: Array<string>) => Promise.all(key.map((url) => openmrsFetch<ConceptReferenceResponse>(url))),
);

const ob = data?.data;
const ob: ConceptReferenceResponse = data?.reduce((acc, response) => ({ ...acc, ...response.data }), {});
const orderReasons = ob
? Object.entries(ob).map(([key, value]) => ({
? Object.values(ob).map((value) => ({
uuid: value.uuid,
display: value.display,
}))
Expand All @@ -70,6 +69,12 @@ export function useOrderReasons(conceptUuids: Array<string>) {
return { orderReasons: orderReasons, isLoading };
}

function getConceptReferenceUrls(conceptUuids: Array<string>) {
return chunk(conceptUuids, 10).map(
(partition) => `${restBaseUrl}/conceptreferences?references=${partition.join(',')}&v=custom:(uuid,display)`,
);
}

export function prepLabOrderPostData(order: LabOrderBasketItem, patientUuid: string, encounterUuid: string): OrderPost {
if (order.action === 'NEW' || order.action === 'RENEW') {
return {
Expand Down Expand Up @@ -112,15 +117,6 @@ export function prepLabOrderPostData(order: LabOrderBasketItem, patientUuid: str
throw new Error(`Unknown order action: ${order.action}.`);
}
}
const chunkSize = 10;
export function getConceptReferenceUrls(conceptUuids: Array<string>) {
const accumulator = [];
for (let i = 0; i < conceptUuids.length; i += chunkSize) {
accumulator.push(conceptUuids.slice(i, i + chunkSize));
}

return accumulator.map((partition) => `conceptreferences?references=${partition.join(',')}&v=custom:(uuid,display)`);
}

export type PostDataPrepLabOrderFunction = (
order: LabOrderBasketItem,
Expand All @@ -132,13 +128,16 @@ export interface ConceptAnswers {
display: string;
uuid: string;
}
export interface ConceptResponse {
uuid: string;
display: string;
datatype: {

export interface ConceptReferenceResponse {
[key: string]: {
uuid: string;
display: string;
datatype: {
uuid: string;
display: string;
};
answers: Array<ConceptAnswers>;
setMembers: Array<ConceptAnswers>;
};
answers: Array<ConceptAnswers>;
setMembers: Array<ConceptAnswers>;
}
Loading