@openmrs/esm-form-engine-lib 2.1.0-pre.1585 → 2.1.0-pre.1594

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-form-engine-lib",
3
- "version": "2.1.0-pre.1585",
3
+ "version": "2.1.0-pre.1594",
4
4
  "description": "React Form Engine for O3",
5
5
  "browser": "dist/openmrs-esm-form-engine-lib.js",
6
6
  "main": "src/index.ts",
@@ -83,7 +83,7 @@ export const ObsAdapter: FormFieldValueAdapter = {
83
83
  return null;
84
84
  }
85
85
  if (hasRendering(field, 'checkbox')) {
86
- return handleMultiSelect(field, value);
86
+ return handleMultiSelect(field, Array.isArray(value) ? value : [value]);
87
87
  }
88
88
  if (!isEmpty(value) && hasPreviousObsValueChanged(field, value)) {
89
89
  return gracefullySetSubmission(field, editObs(field, value), undefined);
package/src/api/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { fhirBaseUrl, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
2
2
  import { encounterRepresentation } from '../constants';
3
- import { type OpenmrsForm, type PatientIdentifier, type PatientProgramPayload } from '../types';
3
+ import type { FHIRObsResource, OpenmrsForm, PatientIdentifier, PatientProgramPayload } from '../types';
4
4
  import { isUuid } from '../utils/boolean-utils';
5
5
 
6
6
  export function saveEncounter(abortController: AbortController, payload, encounterUuid?: string) {
@@ -76,15 +76,18 @@ export async function getPreviousEncounter(patientUuid: string, encounterType: s
76
76
  return null;
77
77
  }
78
78
 
79
- export function getLatestObs(patientUuid: string, conceptUuid: string, encounterTypeUuid?: string) {
79
+ export async function getLatestObs(
80
+ patientUuid: string,
81
+ conceptUuid: string,
82
+ encounterTypeUuid?: string,
83
+ ): Promise<FHIRObsResource> {
80
84
  let params = `patient=${patientUuid}&code=${conceptUuid}${
81
85
  encounterTypeUuid ? `&encounter.type=${encounterTypeUuid}` : ''
82
86
  }`;
83
87
  // the latest obs
84
88
  params += '&_sort=-date&_count=1';
85
- return openmrsFetch(`${fhirBaseUrl}/Observation?${params}`).then(({ data }) => {
86
- return data.entry?.length ? data.entry[0].resource : null;
87
- });
89
+ const { data } = await openmrsFetch(`${fhirBaseUrl}/Observation?${params}`);
90
+ return data.entry?.length ? data.entry[0].resource : null;
88
91
  }
89
92
 
90
93
  /**
@@ -15,20 +15,80 @@ export interface OpenmrsEncounter {
15
15
  }
16
16
 
17
17
  export interface OpenmrsObs extends OpenmrsResource {
18
- concept: any;
19
- obsDatetime: string | Date;
20
- obsGroup: OpenmrsObs;
21
- groupMembers: Array<OpenmrsObs>;
22
- comment: string;
23
- location: OpenmrsResource;
24
- order: OpenmrsResource;
25
- encounter: OpenmrsResource;
26
- voided: boolean;
27
- value: any;
28
- formFieldPath: string;
29
- formFieldNamespace: string;
30
- status: string;
31
- interpretation: string;
18
+ concept?: any;
19
+ obsDatetime?: string | Date;
20
+ obsGroup?: OpenmrsObs;
21
+ groupMembers?: Array<OpenmrsObs>;
22
+ comment?: string;
23
+ location?: OpenmrsResource;
24
+ order?: OpenmrsResource;
25
+ encounter?: OpenmrsResource;
26
+ voided?: boolean;
27
+ value?: any;
28
+ formFieldPath?: string;
29
+ formFieldNamespace?: string;
30
+ status?: string;
31
+ interpretation?: string;
32
+ }
33
+
34
+ export interface FHIRObsResource {
35
+ resourceType: string;
36
+ id: string;
37
+ category: Array<{
38
+ coding: Array<{
39
+ system: string;
40
+ code: string;
41
+ display: string;
42
+ }>;
43
+ }>;
44
+ code: {
45
+ coding: Array<{
46
+ code: string;
47
+ display: string;
48
+ }>;
49
+ text: string;
50
+ };
51
+ encounter?: {
52
+ reference: string;
53
+ type: string;
54
+ };
55
+ effectiveDateTime: string;
56
+ issued: string;
57
+ valueBoolean?: boolean;
58
+ valueString?: string;
59
+ valueDateTime?: string;
60
+ valueQuantity?: {
61
+ value: number;
62
+ unit: string;
63
+ system: string;
64
+ code: string;
65
+ };
66
+ valueCodeableConcept?: {
67
+ coding: [
68
+ {
69
+ code: string;
70
+ display: string;
71
+ },
72
+ ];
73
+ text: string;
74
+ };
75
+ referenceRange: Array<{
76
+ low?: {
77
+ value: number;
78
+ };
79
+ high?: {
80
+ value: number;
81
+ };
82
+ type: {
83
+ coding: Array<{
84
+ system: string;
85
+ code: string;
86
+ }>;
87
+ };
88
+ }>;
89
+ hasMember?: Array<{
90
+ reference: string;
91
+ }>;
32
92
  }
33
93
 
34
94
  export interface OpenmrsForm {
@@ -1,14 +1,12 @@
1
1
  import {
2
2
  findConceptByReference,
3
- evaluateConditionalAnswered,
4
3
  evaluateFieldReadonlyProp,
5
4
  evaluateDisabled,
6
5
  isPageContentVisible,
6
+ extractObsValueAndDisplay,
7
7
  } from './form-helper';
8
- import { DefaultValueValidator } from '../validators/default-value-validator';
9
- import { type LayoutType } from '@openmrs/esm-framework';
10
8
  import { ConceptTrue } from '../constants';
11
- import { type FormPage, type FormField, type OpenmrsEncounter, type SessionMode } from '../types';
9
+ import type { FormPage, FormField } from '../types';
12
10
 
13
11
  jest.mock('../validators/default-value-validator');
14
12
 
@@ -507,4 +505,160 @@ describe('Form Engine Helper', () => {
507
505
  expect(isPageContentVisible(page)).toBe(false);
508
506
  });
509
507
  });
508
+
509
+ describe('extractObsValueAndDisplay', () => {
510
+ // Mock form field types
511
+ const mockFormFields = {
512
+ codedField: {
513
+ questionOptions: {
514
+ rendering: 'select',
515
+ answers: [{ concept: '2395de62-f5a6-49b6-ab0f-57a21a9029c1', label: 'Sneezing Symptom' }],
516
+ },
517
+ },
518
+ toggleField: {
519
+ questionOptions: {
520
+ rendering: 'toggle',
521
+ answers: [],
522
+ },
523
+ },
524
+ dateField: {
525
+ questionOptions: {
526
+ rendering: 'date',
527
+ },
528
+ },
529
+ stringField: {
530
+ questionOptions: {
531
+ rendering: 'text',
532
+ },
533
+ },
534
+ } as any;
535
+
536
+ // Primitive value tests
537
+ describe('Primitive Value Handling', () => {
538
+ it('should handle string primitive value', () => {
539
+ const result = extractObsValueAndDisplay(mockFormFields.stringField, 'Hello World');
540
+ expect(result).toEqual({
541
+ value: 'Hello World',
542
+ display: 'Hello World',
543
+ });
544
+ });
545
+
546
+ it('should handle number primitive value', () => {
547
+ const result = extractObsValueAndDisplay(mockFormFields.stringField, 42);
548
+ expect(result).toEqual({
549
+ value: 42,
550
+ display: 42,
551
+ });
552
+ });
553
+ });
554
+
555
+ // FHIR Observation tests
556
+ describe('FHIR Observation Handling', () => {
557
+ const codedFHIRObs = {
558
+ resourceType: 'Observation',
559
+ code: {
560
+ coding: {
561
+ code: '1095de62-b5a6-49v6-ab0f-57a21a9029cb',
562
+ },
563
+ },
564
+ valueCodeableConcept: {
565
+ coding: [
566
+ {
567
+ code: '2395de62-f5a6-49b6-ab0f-57a21a9029c1',
568
+ display: 'Sneezing',
569
+ },
570
+ ],
571
+ },
572
+ };
573
+
574
+ const booleanFHIRObs = {
575
+ resourceType: 'Observation',
576
+ code: {
577
+ coding: {
578
+ code: 'b095deb2-b5a6-49v6-ab0f-57a21a9029cx',
579
+ },
580
+ },
581
+ valueBoolean: true,
582
+ };
583
+
584
+ const dateFHIRObs = {
585
+ resourceType: 'Observation',
586
+ code: {
587
+ coding: {
588
+ code: 'e095de62-b5a6-49v6-ab0f-57a21a9029cy',
589
+ },
590
+ },
591
+ valueDateTime: '2024-07-31T01:33:19+00:00',
592
+ };
593
+
594
+ it('should handle coded FHIR observation', () => {
595
+ const result = extractObsValueAndDisplay(mockFormFields.codedField, codedFHIRObs);
596
+ expect(result).toEqual({
597
+ value: '2395de62-f5a6-49b6-ab0f-57a21a9029c1',
598
+ display: 'Sneezing Symptom',
599
+ });
600
+ });
601
+
602
+ it('should handle boolean FHIR observation', () => {
603
+ const result = extractObsValueAndDisplay(mockFormFields.toggleField, booleanFHIRObs);
604
+ expect(result).toEqual({
605
+ value: ConceptTrue,
606
+ display: undefined,
607
+ });
608
+ });
609
+
610
+ it('should handle date FHIR observation', () => {
611
+ const result = extractObsValueAndDisplay(mockFormFields.dateField, dateFHIRObs);
612
+ expect(result).toEqual({
613
+ value: expect.any(Date),
614
+ display: expect.stringContaining('2024-07-31'),
615
+ });
616
+ });
617
+ });
618
+
619
+ // OpenMRS Observation tests
620
+ describe('OpenMRS Observation Handling', () => {
621
+ const codedOpenMRSObs = {
622
+ value: {
623
+ uuid: '2395de62-f5a6-49b6-ab0f-57a21a9029c1',
624
+ name: { name: 'Sneezing' },
625
+ },
626
+ };
627
+
628
+ const booleanOpenMRSObs = {
629
+ value: {
630
+ uuid: 'cf82933b-3f3f-45e7-a5ab-5d31aaee3da3',
631
+ name: { name: 'True' },
632
+ },
633
+ };
634
+
635
+ const dateOpenMRSObs = {
636
+ value: '2024-07-31T01:33:19+00:00',
637
+ };
638
+
639
+ it('should handle coded OpenMRS observation', () => {
640
+ const result = extractObsValueAndDisplay(mockFormFields.codedField, codedOpenMRSObs);
641
+ expect(result).toEqual({
642
+ value: '2395de62-f5a6-49b6-ab0f-57a21a9029c1',
643
+ display: 'Sneezing Symptom',
644
+ });
645
+ });
646
+
647
+ it('should handle boolean OpenMRS observation', () => {
648
+ const result = extractObsValueAndDisplay(mockFormFields.toggleField, booleanOpenMRSObs);
649
+ expect(result).toEqual({
650
+ value: 'cf82933b-3f3f-45e7-a5ab-5d31aaee3da3',
651
+ display: 'True',
652
+ });
653
+ });
654
+
655
+ it('should handle date OpenMRS observation', () => {
656
+ const result = extractObsValueAndDisplay(mockFormFields.dateField, dateOpenMRSObs);
657
+ expect(result).toEqual({
658
+ value: expect.any(Date),
659
+ display: expect.stringMatching(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/),
660
+ });
661
+ });
662
+ });
663
+ });
510
664
  });
@@ -1,8 +1,9 @@
1
1
  import { type LayoutType } from '@openmrs/esm-framework';
2
- import { type OpenmrsObs, type FormField, type FormPage, type FormSection, type SessionMode } from '../types';
2
+ import type { FormField, FormPage, FormSection, SessionMode, FHIRObsResource, RenderType } from '../types';
3
3
  import { isEmpty } from '../validators/form-validator';
4
4
  import { parseToLocalDateTime } from './common-utils';
5
5
  import dayjs from 'dayjs';
6
+ import { ConceptFalse, ConceptTrue } from '../constants';
6
7
 
7
8
  export function shouldUseInlineLayout(
8
9
  renderingType: 'single-line' | 'multiline' | 'automatic',
@@ -172,24 +173,30 @@ export function scrollIntoView(viewId: string, shouldFocus: boolean = false) {
172
173
  }
173
174
  }
174
175
 
175
- export const extractObsValueAndDisplay = (field: FormField, obs: OpenmrsObs) => {
176
+ export const extractObsValueAndDisplay = (field: FormField, obs: any) => {
176
177
  const rendering = field.questionOptions.rendering;
177
-
178
- if (typeof obs.value === 'string' || typeof obs.value === 'number') {
178
+ if (typeof obs !== 'object') {
179
+ return { value: obs, display: obs };
180
+ }
181
+ const omrsObs = obs.resourceType === 'Observation' ? mapFHIRObsToOpenMRS(obs, rendering) : obs;
182
+ if (!omrsObs) {
183
+ return { value: null, display: null };
184
+ }
185
+ if (typeof omrsObs.value === 'string' || typeof omrsObs.value === 'number') {
179
186
  if (rendering === 'date' || rendering === 'datetime') {
180
- const dateObj = parseToLocalDateTime(`${obs.value}`);
187
+ const dateObj = parseToLocalDateTime(`${omrsObs.value}`);
181
188
  return { value: dateObj, display: dayjs(dateObj).format('YYYY-MM-DD HH:mm') };
182
189
  }
183
- return { value: obs.value, display: obs.value };
190
+ return { value: omrsObs.value, display: omrsObs.value };
184
191
  } else if (['toggle', 'checkbox'].includes(rendering)) {
185
192
  return {
186
- value: obs.value?.uuid,
187
- display: obs.value?.name?.name,
193
+ value: omrsObs.value?.uuid,
194
+ display: omrsObs.value?.name?.name,
188
195
  };
189
196
  } else {
190
197
  return {
191
- value: obs.value?.uuid,
192
- display: field.questionOptions.answers?.find((option) => option.concept === obs.value?.uuid)?.label,
198
+ value: omrsObs.value?.uuid,
199
+ display: field.questionOptions.answers?.find((option) => option.concept === omrsObs.value?.uuid)?.label,
193
200
  };
194
201
  }
195
202
  };
@@ -212,3 +219,48 @@ export function isPageContentVisible(page: FormPage) {
212
219
  }) ?? false
213
220
  );
214
221
  }
222
+
223
+ function mapFHIRObsToOpenMRS(fhirObs: FHIRObsResource, rendering: RenderType) {
224
+ try {
225
+ return {
226
+ obsDatetime: fhirObs.effectiveDateTime,
227
+ uuid: fhirObs.id,
228
+ concept: {
229
+ uuid: fhirObs.code.coding[0]?.code,
230
+ display: fhirObs.code.coding[0]?.display,
231
+ },
232
+ value: extractFHIRObsValue(fhirObs, rendering),
233
+ };
234
+ } catch (error) {
235
+ console.error('Error converting FHIR Obs to OpenMRS modelling', error);
236
+ return null;
237
+ }
238
+ }
239
+
240
+ function extractFHIRObsValue(fhirObs: FHIRObsResource, rendering: RenderType) {
241
+ switch (rendering) {
242
+ case 'toggle':
243
+ return fhirObs.valueBoolean ? { uuid: ConceptTrue } : { uuid: ConceptFalse };
244
+
245
+ case 'date':
246
+ case 'datetime':
247
+ return fhirObs.valueDateTime;
248
+
249
+ case 'number':
250
+ return fhirObs.valueQuantity?.value ?? null;
251
+
252
+ case 'radio':
253
+ case 'checkbox':
254
+ case 'select':
255
+ case 'content-switcher':
256
+ return fhirObs.valueCodeableConcept?.coding[0]
257
+ ? {
258
+ uuid: fhirObs.valueCodeableConcept?.coding[0].code,
259
+ name: { name: fhirObs.valueCodeableConcept?.coding[0].display },
260
+ }
261
+ : null;
262
+
263
+ default:
264
+ return fhirObs.valueString;
265
+ }
266
+ }