@openmrs/esm-patient-common-lib 8.2.1-pre.6228 → 8.2.1-pre.6234

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-patient-common-lib",
3
- "version": "8.2.1-pre.6228",
3
+ "version": "8.2.1-pre.6234",
4
4
  "license": "MPL-2.0",
5
5
  "description": "Library for common patient chart components",
6
6
  "browser": "dist/openmrs-esm-patient-common-lib.js",
@@ -2,3 +2,6 @@ export * from './useOrderBasket';
2
2
  export * from './postOrders';
3
3
  export * from './useOrders';
4
4
  export * from './types';
5
+ export * from './useOrderableConceptSets';
6
+ export * from './useOrderType';
7
+ export * from './useOrderTypes';
@@ -1,7 +1,14 @@
1
1
  import { openmrsFetch, type OpenmrsResource, parseDate, restBaseUrl, type Visit } from '@openmrs/esm-framework';
2
2
  import { getPatientUuidFromStore } from '../store/patient-chart-store';
3
3
  import { type OrderBasketStore, orderBasketStore } from './store';
4
- import { type ExtractedOrderErrorObject, type OrderBasketItem, type OrderErrorObject, type OrderPost } from './types';
4
+ import type {
5
+ DrugOrderPost,
6
+ TestOrderPost,
7
+ ExtractedOrderErrorObject,
8
+ OrderBasketItem,
9
+ OrderErrorObject,
10
+ OrderPost,
11
+ } from './types';
5
12
 
6
13
  export async function postOrdersOnNewEncounter(
7
14
  patientUuid: string,
@@ -26,7 +33,7 @@ export async function postOrdersOnNewEncounter(
26
33
  const { items, postDataPrepFunctions }: OrderBasketStore = orderBasketStore.getState();
27
34
  const patientItems = items[patientUuid];
28
35
 
29
- const orders: Array<OrderPost> = [];
36
+ const orders: Array<DrugOrderPost | TestOrderPost> = [];
30
37
 
31
38
  Object.entries(patientItems).forEach(([grouping, groupOrders]) => {
32
39
  groupOrders.forEach((order) => {
@@ -0,0 +1,2 @@
1
+ export * from './order';
2
+ export * from './order-type';
@@ -0,0 +1,14 @@
1
+ export type OrderTypeJavaClassName = 'org.openmrs.Order' | 'org.openmrs.TestOrder' | 'org.openmrs.DrugOrder';
2
+
3
+ export interface OrderTypeResponse {
4
+ uuid: string;
5
+ display: string;
6
+ name: string;
7
+ javaClassName: OrderTypeJavaClassName;
8
+ retired: false;
9
+ description: string;
10
+ conceptClasses: Array<{
11
+ uuid: string;
12
+ display: string;
13
+ }>;
14
+ }
@@ -1,4 +1,25 @@
1
- import { type OpenmrsResource } from '@openmrs/esm-framework';
1
+ import type { OpenmrsResource } from '@openmrs/esm-framework';
2
+
3
+ export interface Concept extends OpenmrsResource {
4
+ name?: {
5
+ display: string;
6
+ };
7
+ names?: Array<{
8
+ display: string;
9
+ }>;
10
+ conceptClass?: {
11
+ uuid: string;
12
+ };
13
+ answers?: Array<Concept>;
14
+ setMembers?: Array<Concept>;
15
+ }
16
+ export interface Drug {
17
+ uuid: string;
18
+ strength: string;
19
+ concept: Concept;
20
+ dosageForm: OpenmrsResource;
21
+ display: string;
22
+ }
2
23
 
3
24
  export type OrderAction = 'NEW' | 'REVISE' | 'DISCONTINUE' | 'RENEW';
4
25
 
@@ -23,7 +44,7 @@ export interface OrderErrorObject {
23
44
  }
24
45
 
25
46
  export interface OrderBasketItem {
26
- action?: OrderAction;
47
+ action: OrderAction;
27
48
  display: string;
28
49
  uuid?: string;
29
50
  orderer?: string;
@@ -43,10 +64,21 @@ export interface OrderBasketItem {
43
64
  * An optional identifier from the fulfiller (e.g., lab system) for the specimen or record associated with the order.
44
65
  */
45
66
  accessionNumber?: string;
67
+ concept?: Concept;
68
+ instructions?: string;
69
+ urgency?: OrderUrgency;
70
+ previousOrder?: string;
71
+ orderType?: string;
72
+ orderNumber?: string;
46
73
  }
47
74
 
48
75
  export type OrderUrgency = 'ROUTINE' | 'STAT' | 'ON_SCHEDULED_DATE';
49
76
 
77
+ export type PriorityOption = {
78
+ label: string;
79
+ value: OrderUrgency;
80
+ };
81
+
50
82
  export interface OrderPost {
51
83
  urgency?: OrderUrgency;
52
84
  action?: OrderAction;
@@ -54,6 +86,19 @@ export interface OrderPost {
54
86
  careSetting?: string;
55
87
  orderer?: string;
56
88
  encounter?: string;
89
+ type?: string;
90
+ concept?: string;
91
+ dateActivated?: string;
92
+ previousOrder?: string;
93
+ asNeededCondition?: string;
94
+ orderReasonNonCoded?: string;
95
+ orderReason?: string;
96
+ instructions?: string;
97
+ accessionNumber?: string;
98
+ orderType?: string;
99
+ }
100
+
101
+ export interface DrugOrderPost extends OrderPost {
57
102
  drug?: string;
58
103
  dose?: number;
59
104
  doseUnits?: string;
@@ -63,21 +108,14 @@ export interface OrderPost {
63
108
  numRefills?: number;
64
109
  quantity?: number;
65
110
  quantityUnits?: string;
66
- type?: string;
67
111
  duration?: number;
68
112
  durationUnits?: string;
69
113
  dosingType?: 'org.openmrs.FreeTextDosingInstructions' | 'org.openmrs.SimpleDosingInstructions';
70
114
  dosingInstructions?: string;
71
- concept?: string;
72
- dateActivated?: string;
73
- previousOrder?: string;
74
- asNeededCondition?: string;
75
- orderReasonNonCoded?: string;
76
- orderReason?: string;
77
- instructions?: string;
78
- accessionNumber?: string;
79
115
  }
80
116
 
117
+ export interface TestOrderPost extends OrderPost {}
118
+
81
119
  export interface PatientOrderFetchResponse {
82
120
  results: Array<Order>;
83
121
  }
@@ -91,7 +129,7 @@ export interface Order {
91
129
  brandName?: string;
92
130
  careSetting: OpenmrsResource;
93
131
  commentToFulfiller: string;
94
- concept: OpenmrsResource;
132
+ concept: Concept;
95
133
  dateActivated: string;
96
134
  dateStopped?: string | null;
97
135
  dispenseAsWritten: boolean;
@@ -131,7 +169,7 @@ export interface Order {
131
169
  quantityUnits: OpenmrsResource;
132
170
  route: OpenmrsResource;
133
171
  scheduleDate: null;
134
- urgency: 'ROUTINE' | 'STAT' | 'ON_SCHEDULED_DATE';
172
+ urgency: OrderUrgency;
135
173
 
136
174
  // additional properties
137
175
  accessionNumber: string;
@@ -167,54 +205,10 @@ export interface OrderType {
167
205
  description: string;
168
206
  }
169
207
 
170
- export interface Drug {
171
- uuid: string;
172
- strength: string;
173
- concept: OpenmrsResource;
174
- dosageForm: OpenmrsResource;
175
- display: string;
176
- }
208
+ export type FulfillerStatus = 'EXCEPTION' | 'RECEIVED' | 'COMPLETED' | 'IN_PROGRESS' | 'ON_HOLD' | 'DECLINED';
177
209
 
178
210
  export type PostDataPrepFunction = (
179
211
  order: OrderBasketItem,
180
212
  patientUuid: string,
181
213
  encounterUuid: string | null,
182
214
  ) => OrderPost;
183
-
184
- // Adopted from @openmrs/esm-patient-medications-app package. We should consider maintaining a single shared types file
185
- export interface DrugOrderBasketItem extends OrderBasketItem {
186
- drug: Drug;
187
- unit: any;
188
- commonMedicationName: string;
189
- dosage: number;
190
- frequency: any;
191
- route: any;
192
- quantityUnits: any;
193
- patientInstructions: string;
194
- asNeeded: boolean;
195
- asNeededCondition: string;
196
- startDate: Date | string;
197
- durationUnit: any;
198
- duration: number | null;
199
- pillsDispensed: number;
200
- numRefills: number;
201
- indication: string;
202
- isFreeTextDosage: boolean;
203
- freeTextDosage: string;
204
- previousOrder?: string;
205
- template?: any;
206
- }
207
-
208
- export interface LabOrderBasketItem extends OrderBasketItem {
209
- testType?: {
210
- label: string;
211
- conceptUuid: string;
212
- };
213
- urgency?: OrderUrgency;
214
- instructions?: string;
215
- previousOrder?: string;
216
- orderReason?: string;
217
- orderNumber?: string;
218
- }
219
-
220
- export type FulfillerStatus = 'EXCEPTION' | 'RECEIVED' | 'COMPLETED' | 'IN_PROGRESS' | 'ON_HOLD' | 'DECLINED';
@@ -0,0 +1,23 @@
1
+ import { type FetchResponse, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
2
+ import { useMemo } from 'react';
3
+ import useSWRImmutable from 'swr/immutable';
4
+ import type { OrderTypeResponse } from './types';
5
+
6
+ export function useOrderType(orderTypeUuid: string) {
7
+ const { data, isLoading, isValidating, error } = useSWRImmutable<FetchResponse<OrderTypeResponse>>(
8
+ `${restBaseUrl}/ordertype/${orderTypeUuid}`,
9
+ openmrsFetch,
10
+ );
11
+
12
+ const results = useMemo(
13
+ () => ({
14
+ isLoadingOrderType: isLoading,
15
+ orderType: data?.data,
16
+ errorFetchingOrderType: error,
17
+ isValidatingOrderType: isValidating,
18
+ }),
19
+ [data?.data, error, isLoading, isValidating],
20
+ );
21
+
22
+ return results;
23
+ }
@@ -0,0 +1,24 @@
1
+ import { type FetchResponse, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
2
+ import useSWRImmutable from 'swr/immutable';
3
+ import type { OrderTypeFetchResponse } from './types';
4
+ import { useMemo } from 'react';
5
+
6
+ export function useOrderTypes() {
7
+ const orderTypesUrl = `${restBaseUrl}/ordertype`;
8
+ const { data, error, isLoading, isValidating } = useSWRImmutable<FetchResponse<OrderTypeFetchResponse>, Error>(
9
+ orderTypesUrl,
10
+ openmrsFetch,
11
+ );
12
+
13
+ const results = useMemo(
14
+ () => ({
15
+ data: data?.data?.results,
16
+ error,
17
+ isLoading,
18
+ isValidating,
19
+ }),
20
+ [data?.data?.results, error, isLoading, isValidating],
21
+ );
22
+
23
+ return results;
24
+ }
@@ -0,0 +1,65 @@
1
+ import { renderHook, waitFor } from '@testing-library/react';
2
+ import { getDefaultsFromConfigSchema, openmrsFetch, restBaseUrl, useConfig } from '@openmrs/esm-framework';
3
+ import { type ConfigObject, configSchema } from '../../../esm-patient-tests-app/src/config-schema';
4
+ import { useOrderableConceptSets } from './useOrderableConceptSets';
5
+
6
+ const mockOpenrsFetch = openmrsFetch as jest.Mock;
7
+ const mockUseConfig = jest.mocked(useConfig<ConfigObject>);
8
+
9
+ mockUseConfig.mockReturnValue({
10
+ ...getDefaultsFromConfigSchema(configSchema),
11
+ orders: {
12
+ labOrderableConcepts: [],
13
+ labOrderTypeUuid: 'lab-order-type-uuid',
14
+ },
15
+ });
16
+
17
+ mockOpenrsFetch.mockImplementation((url: string) => {
18
+ if (url.includes('concept?class=concept-class-uuid')) {
19
+ return Promise.resolve({ data: { results: [{ display: 'Test concept' }] } });
20
+ } else if (/.*concept\/[0-9a-f]+.*/.test(url)) {
21
+ return Promise.resolve({
22
+ data: {
23
+ display: 'Orderable set',
24
+ setMembers: [
25
+ { display: 'Configured concept', names: [{ display: 'Configured concept' }], uuid: 'concept-one' },
26
+ {
27
+ display: 'Another configured concept',
28
+ names: [{ display: 'Another configured concept' }],
29
+ uuid: 'concept-two',
30
+ },
31
+ ],
32
+ },
33
+ });
34
+ } else {
35
+ throw Error('Unexpected URL: ' + url);
36
+ }
37
+ });
38
+
39
+ describe('useOrderableConceptSets is configurable', () => {
40
+ it('should fetch orderable concept sets if passed', async () => {
41
+ const { result } = renderHook(() => useOrderableConceptSets('', ['concept-set-uuid']));
42
+ expect(openmrsFetch).toHaveBeenCalledWith(
43
+ `${restBaseUrl}/concept/concept-set-uuid?v=custom:(display,names:(display),uuid,setMembers:(display,uuid,names:(display),setMembers:(display,uuid,names:(display))))`,
44
+ );
45
+ await waitFor(() => expect(result.current.isLoading).toBeFalsy());
46
+ expect(result.current.concepts).toEqual([
47
+ expect.objectContaining({ display: 'Another configured concept' }),
48
+ expect.objectContaining({ display: 'Configured concept' }),
49
+ ]);
50
+ expect(result.current.error).toBeFalsy();
51
+ });
52
+
53
+ xit('should filter through fetched concepts sets based on the search term', async () => {
54
+ const { result } = renderHook(() => useOrderableConceptSets('another', ['concept-set-uuid']));
55
+ expect(openmrsFetch).toHaveBeenCalledWith(
56
+ `${restBaseUrl}/concept/concept-set-uuid?v=custom:(display,names:(display),uuid,setMembers:(display,uuid,names:(display),setMembers:(display,uuid,names:(display))))`,
57
+ );
58
+ await waitFor(() => expect(result.current.isLoading).toBeFalsy());
59
+ expect(result.current.concepts).toEqual([
60
+ expect.objectContaining({ display: 'Another configured concept' }),
61
+ // expect.objectContaining({ display: 'Configured concept' }),
62
+ ]);
63
+ expect(result.current.error).toBeFalsy();
64
+ });
65
+ });
@@ -0,0 +1,90 @@
1
+ import {
2
+ type FetchResponse,
3
+ openmrsFetch,
4
+ type OpenmrsResource,
5
+ reportError,
6
+ restBaseUrl,
7
+ showSnackbar,
8
+ } from '@openmrs/esm-framework';
9
+ import { useEffect, useMemo } from 'react';
10
+ import useSWRImmutable from 'swr/immutable';
11
+ import type { Concept } from './types';
12
+
13
+ type ConceptResult = FetchResponse<Concept>;
14
+ type ConceptResults = FetchResponse<{ results: Array<Concept> }>;
15
+
16
+ function openmrsFetchMultipleConcepts(urls: Array<string>) {
17
+ // SWR has an RFC for `useSWRList`:
18
+ // https://github.com/vercel/swr/discussions/1988
19
+ // If that ever is implemented we should switch to using that.
20
+ return Promise.all(urls.map((url) => openmrsFetch<{ results: Array<Concept> }>(url)));
21
+ }
22
+
23
+ function useOrderableConceptSWR(searchTerm: string, orderableConceptSets?: Array<string>) {
24
+ const { data, isLoading, error } = useSWRImmutable<Array<ConceptResult> | Array<ConceptResults>, Error>(
25
+ orderableConceptSets?.length
26
+ ? orderableConceptSets.map(
27
+ (c) =>
28
+ `${restBaseUrl}/concept/${c}?v=custom:(display,names:(display),uuid,setMembers:(display,uuid,names:(display),setMembers:(display,uuid,names:(display))))`,
29
+ )
30
+ : null,
31
+ openmrsFetchMultipleConcepts,
32
+ );
33
+
34
+ useEffect(() => {
35
+ if (error) {
36
+ reportError(error);
37
+ }
38
+ });
39
+
40
+ const results = useMemo(() => {
41
+ if (isLoading || error) return null;
42
+
43
+ if (orderableConceptSets) {
44
+ const concepts = (data as Array<ConceptResult>)?.flatMap((d) => d.data.setMembers);
45
+ if (searchTerm) {
46
+ return concepts?.filter((concept) =>
47
+ concept.names.some((name) => name.display.toLocaleLowerCase().includes(searchTerm.toLocaleLowerCase())),
48
+ );
49
+ }
50
+ return concepts;
51
+ } else {
52
+ return (data as Array<ConceptResults>)?.flatMap((d) => d.data.results) ?? ([] as Concept[]);
53
+ }
54
+ }, [isLoading, error, orderableConceptSets, data, searchTerm]);
55
+
56
+ return {
57
+ data: results,
58
+ isLoading,
59
+ error,
60
+ };
61
+ }
62
+
63
+ export interface OrderableConcept extends OpenmrsResource {}
64
+
65
+ export function useOrderableConceptSets(searchTerm: string, orderableConcepts: Array<string>) {
66
+ const { data, isLoading, error } = useOrderableConceptSWR(
67
+ searchTerm,
68
+ orderableConcepts?.length ? orderableConcepts : null,
69
+ );
70
+
71
+ useEffect(() => {
72
+ if (error) {
73
+ reportError(error);
74
+ }
75
+ }, [error]);
76
+
77
+ const concepts: Array<OrderableConcept> = useMemo(
78
+ () =>
79
+ data
80
+ ?.sort((testConcept1, testConcept2) => testConcept1.display.localeCompare(testConcept2.display))
81
+ ?.filter((item, pos, array) => !pos || array[pos - 1].uuid !== item.uuid),
82
+ [data],
83
+ );
84
+
85
+ return {
86
+ concepts,
87
+ isLoading: isLoading,
88
+ error: error,
89
+ };
90
+ }
@@ -1,7 +1,7 @@
1
1
  import { useCallback, useMemo } from 'react';
2
2
  import useSWR, { useSWRConfig } from 'swr';
3
- import { type FetchResponse, openmrsFetch, restBaseUrl, toOmrsIsoString } from '@openmrs/esm-framework';
4
- import { type OrderTypeFetchResponse, type PatientOrderFetchResponse } from '@openmrs/esm-patient-common-lib';
3
+ import { type FetchResponse, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
4
+ import type { PatientOrderFetchResponse, PriorityOption } from './types';
5
5
 
6
6
  export type Status = 'ACTIVE' | 'any';
7
7
  export const careSettingUuid = '6f0c9a92-6f24-11e3-af88-005056821db0';
@@ -56,21 +56,12 @@ export function usePatientOrders(
56
56
  };
57
57
  }
58
58
 
59
- export function useOrderTypes() {
60
- const orderTypesUrl = `${restBaseUrl}/ordertype`;
61
- const { data, error, isLoading, isValidating } = useSWR<FetchResponse<OrderTypeFetchResponse>, Error>(
62
- orderTypesUrl,
63
- openmrsFetch,
64
- );
65
-
66
- return {
67
- data: data?.data?.results ?? null,
68
- error,
69
- isLoading,
70
- isValidating,
71
- };
72
- }
73
-
74
59
  export function getDrugOrderByUuid(orderUuid: string) {
75
60
  return openmrsFetch(`${restBaseUrl}/order/${orderUuid}?v=${drugCustomRepresentation}`);
76
61
  }
62
+
63
+ // See the Urgency enum in https://github.com/openmrs/openmrs-core/blob/492dcd35b85d48730bd19da48f6db146cc882c22/api/src/main/java/org/openmrs/Order.java
64
+ export const priorityOptions: PriorityOption[] = [
65
+ { value: 'ROUTINE', label: 'Routine' },
66
+ { value: 'STAT', label: 'Stat' },
67
+ ];