@openmrs/esm-patient-common-lib 8.0.2-pre.4619 → 8.0.2-pre.4638

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.0.2-pre.4619",
3
+ "version": "8.0.2-pre.4638",
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",
@@ -1,8 +1,58 @@
1
- import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
1
+ import { openmrsFetch, type OpenmrsResource, parseDate, restBaseUrl, type Visit } from '@openmrs/esm-framework';
2
2
  import { getPatientUuidFromUrl } from '../get-patient-uuid-from-url';
3
3
  import { type OrderBasketStore, orderBasketStore } from './store';
4
- import { type ExtractedOrderErrorObject, type OrderBasketItem, type OrderPost } from './types';
5
- import { type OrderErrorObject } from './types';
4
+ import { type ExtractedOrderErrorObject, type OrderBasketItem, type OrderErrorObject, type OrderPost } from './types';
5
+
6
+ export async function postOrdersOnNewEncounter(
7
+ patientUuid: string,
8
+ orderEncounterType: string,
9
+ activeVisit: Visit | null,
10
+ sessionLocationUuid: string,
11
+ abortController?: AbortController,
12
+ ) {
13
+ const now = new Date();
14
+ const visitStartDate = parseDate(activeVisit?.startDatetime);
15
+ const visitEndDate = parseDate(activeVisit?.stopDatetime);
16
+ let encounterDate: Date;
17
+ if (!activeVisit || (visitStartDate < now && (!visitEndDate || visitEndDate > now))) {
18
+ encounterDate = now;
19
+ } else {
20
+ console.warn(
21
+ 'postOrdersOnNewEncounter received an active visit that is not currently active. This is a programming error. Attempting to place the order using the visit start date.',
22
+ );
23
+ encounterDate = visitStartDate;
24
+ }
25
+
26
+ const { items, postDataPrepFunctions }: OrderBasketStore = orderBasketStore.getState();
27
+ const patientItems = items[patientUuid];
28
+
29
+ const orders: Array<OrderPost> = [];
30
+
31
+ Object.entries(patientItems).forEach(([grouping, groupOrders]) => {
32
+ groupOrders.forEach((order) => {
33
+ orders.push(postDataPrepFunctions[grouping](order, patientUuid, null));
34
+ });
35
+ });
36
+
37
+ const encounterPostData = {
38
+ patient: patientUuid,
39
+ location: sessionLocationUuid,
40
+ encounterType: orderEncounterType,
41
+ encounterDatetime: encounterDate,
42
+ visit: activeVisit?.uuid,
43
+ obs: [],
44
+ orders,
45
+ };
46
+
47
+ return openmrsFetch<OpenmrsResource>(`${restBaseUrl}/encounter`, {
48
+ headers: {
49
+ 'Content-Type': 'application/json',
50
+ },
51
+ method: 'POST',
52
+ body: encounterPostData,
53
+ signal: abortController?.signal,
54
+ }).then((res) => res?.data?.uuid);
55
+ }
6
56
 
7
57
  export async function postOrders(encounterUuid: string, abortController: AbortController) {
8
58
  const patientUuid = getPatientUuidFromUrl();
@@ -169,7 +169,11 @@ export interface Drug {
169
169
  display: string;
170
170
  }
171
171
 
172
- export type PostDataPrepFunction = (order: OrderBasketItem, patientUuid: string, encounterUuid: string) => OrderPost;
172
+ export type PostDataPrepFunction = (
173
+ order: OrderBasketItem,
174
+ patientUuid: string,
175
+ encounterUuid: string | null,
176
+ ) => OrderPost;
173
177
 
174
178
  // Adopted from @openmrs/esm-patient-medications-app package. We should consider maintaining a single shared types file
175
179
  export interface DrugOrderBasketItem extends OrderBasketItem {