@openmrs/esm-form-engine-lib 5.0.1-pre.2750 → 5.0.1-pre.2754

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.
@@ -8,7 +8,7 @@ export let assignedObsIds = [];
8
8
  export const ObsAdapter = {
9
9
  async getInitialValue (field, sourceObject, context) {
10
10
  const encounter = sourceObject ?? context.domainObjectValue;
11
- const matchingObs = findObsByFormField(flattenObsList(encounter.obs), assignedObsIds, field);
11
+ const matchingObs = findObsByFormField(flattenObsList(encounter.obs), assignedObsIds, field, context.formFields);
12
12
  if (hasRendering(field, 'file') && matchingObs?.length) {
13
13
  return resolveAttachmentsFromObs(field, matchingObs);
14
14
  }
@@ -19,7 +19,7 @@ export const ObsAdapter = {
19
19
  if (encounter) {
20
20
  // the previous encounter's obs must not become this field's obs to edit or void;
21
21
  // an obs already loaded for this encounter stays bound to the field
22
- const value = extractFieldValue(field, findObsByFormField(flattenObsList(encounter.obs), assignedObsIds, field), false);
22
+ const value = extractFieldValue(field, findObsByFormField(flattenObsList(encounter.obs), assignedObsIds, field, context.formFields), false);
23
23
  if (!isEmpty(value)) {
24
24
  return {
25
25
  value,
@@ -229,8 +229,9 @@ function handleAttachments(field, attachments = []) {
229
229
  *
230
230
  * Notes:
231
231
  * If the query by field-path returns an empty list, the function falls back to querying
232
- * by concept and uses `claimedObsIds` to exclude already assigned observations.
233
- */ export function findObsByFormField(obsList, claimedObsIds, field) {
232
+ * by concept and uses `claimedObsIds` to exclude already assigned observations. Given the
233
+ * form's fields, it also leaves out obs whose path names another of them or a repeat of one.
234
+ */ export function findObsByFormField(obsList, claimedObsIds, field, formFields) {
234
235
  const obs = obsList.filter((candidate)=>{
235
236
  // we ignore the concept for attachments because they're managed from the backend
236
237
  if (hasRendering(field, 'file') && candidate.formFieldPath == `rfe-forms-${field.id}`) {
@@ -241,11 +242,37 @@ function handleAttachments(field, attachments = []) {
241
242
  // We shall fall back to mapping by the associated concept
242
243
  // That being said, we shall find all matching obs and pick the one that wasn't previously claimed.
243
244
  if (!obs?.length) {
244
- const obsByConcept = obsList.filter((obs)=>obs.concept.uuid == field.questionOptions.concept);
245
+ const obsByConcept = obsList.filter((obs)=>obs.concept.uuid == field.questionOptions.concept && !isBoundToAnotherField(obs, field, formFields));
245
246
  return claimedObsIds?.length ? obsByConcept.filter((obs)=>!claimedObsIds.includes(obs.uuid)) : obsByConcept;
246
247
  }
247
248
  return obs;
248
249
  }
250
+ const repeatedFieldId = (fieldId)=>fieldId.match(/^(.+)_\d+$/)?.[1];
251
+ /**
252
+ * Whether the obs's form field path names another field of the form, or a repeat of one such as `dateStopped_1`,
253
+ * which hydrates it by that path. The concept fallback leaves such an obs to it, as a field that recorded nothing
254
+ * would otherwise take a value from a repeat of itself or from another question on the same concept.
255
+ * A repeat still takes an obs saved under its question's path or that of any repeat of it: it is hydrated from its
256
+ * own group's obs only, and as repeats are numbered in order on hydration, one saved as `dateStopped_2` reopens as
257
+ * `dateStopped_1` once the row before it is deleted. A field of the form whose id only reads like a repeat, such as
258
+ * `item_1` beside `item`, is not one.
259
+ * A repeat of the field itself is told by its path alone, so the field never takes one even without the form's fields.
260
+ */ function isBoundToAnotherField(obs, field, formFields = []) {
261
+ const path = obs.formFieldPath;
262
+ if (!path?.startsWith('rfe-forms-')) {
263
+ return false;
264
+ }
265
+ const fieldId = path.slice('rfe-forms-'.length);
266
+ if (repeatedFieldId(fieldId) === field.id) {
267
+ return true;
268
+ }
269
+ // Repeats are cloned while the encounter is hydrated, so they are not yet among the form's fields
270
+ const repeated = formFields.some((one)=>one.id === field.id) ? undefined : repeatedFieldId(field.id);
271
+ if (repeated && (fieldId === repeated || repeatedFieldId(fieldId) === repeated)) {
272
+ return false;
273
+ }
274
+ return formFields.some((one)=>one.id && (one.id === fieldId || one.id === repeatedFieldId(fieldId)));
275
+ }
249
276
  async function resolveAttachmentsFromObs(field, obs) {
250
277
  const abortController = new AbortController();
251
278
  const attachments = await Promise.all(obs.map((obs)=>getAttachmentByUuid(obs.uuid, abortController).then((response)=>response.data).catch((error)=>{
@@ -18,7 +18,8 @@ export declare function hasPreviousObsValueChanged(field: FormField, newValue: a
18
18
  *
19
19
  * Notes:
20
20
  * If the query by field-path returns an empty list, the function falls back to querying
21
- * by concept and uses `claimedObsIds` to exclude already assigned observations.
21
+ * by concept and uses `claimedObsIds` to exclude already assigned observations. Given the
22
+ * form's fields, it also leaves out obs whose path names another of them or a repeat of one.
22
23
  */
23
- export declare function findObsByFormField(obsList: Array<OpenmrsObs>, claimedObsIds: string[], field: FormField): OpenmrsObs[];
24
+ export declare function findObsByFormField(obsList: Array<OpenmrsObs>, claimedObsIds: string[], field: FormField, formFields?: Array<FormField>): OpenmrsObs[];
24
25
  //# sourceMappingURL=obs-adapter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"obs-adapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/obs-adapter.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,KAAK,UAAU,EACf,KAAK,SAAS,EAGd,KAAK,qBAAqB,EAC3B,MAAM,UAAU,CAAC;AAelB,eAAO,IAAI,cAAc,EAAE,MAAM,EAAO,CAAC;AAEzC,eAAO,MAAM,UAAU,EAAE,qBAgFxB,CAAC;AAuDF,wBAAgB,YAAY,CAAC,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,UAAU,CAAC,CAgB9E;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,UAAU;;;EAEtC;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,GAAG;;;;;EAWtD;AAiBD,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,GAAG,WAgBzE;AA+CD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,KAAK,CAAC,UAAU,CAAC,EAC1B,aAAa,EAAE,MAAM,EAAE,EACvB,KAAK,EAAE,SAAS,GACf,UAAU,EAAE,CAmBd"}
1
+ {"version":3,"file":"obs-adapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/obs-adapter.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,KAAK,UAAU,EACf,KAAK,SAAS,EAGd,KAAK,qBAAqB,EAC3B,MAAM,UAAU,CAAC;AAelB,eAAO,IAAI,cAAc,EAAE,MAAM,EAAO,CAAC;AAEzC,eAAO,MAAM,UAAU,EAAE,qBAgFxB,CAAC;AAuDF,wBAAgB,YAAY,CAAC,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,UAAU,CAAC,CAgB9E;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,UAAU;;;EAEtC;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,GAAG;;;;;EAWtD;AAiBD,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,GAAG,WAgBzE;AA+CD;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,KAAK,CAAC,UAAU,CAAC,EAC1B,aAAa,EAAE,MAAM,EAAE,EACvB,KAAK,EAAE,SAAS,EAChB,UAAU,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC,GAC5B,UAAU,EAAE,CAqBd"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-form-engine-lib",
3
- "version": "5.0.1-pre.2750",
3
+ "version": "5.0.1-pre.2754",
4
4
  "description": "React Form Engine for O3",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -27,7 +27,7 @@ export let assignedObsIds: string[] = [];
27
27
  export const ObsAdapter: FormFieldValueAdapter = {
28
28
  async getInitialValue(field: FormField, sourceObject: any, context: FormContextProps) {
29
29
  const encounter = sourceObject ?? (context.domainObjectValue as OpenmrsEncounter);
30
- const matchingObs = findObsByFormField(flattenObsList(encounter.obs), assignedObsIds, field);
30
+ const matchingObs = findObsByFormField(flattenObsList(encounter.obs), assignedObsIds, field, context.formFields);
31
31
  if (hasRendering(field, 'file') && matchingObs?.length) {
32
32
  return resolveAttachmentsFromObs(field, matchingObs);
33
33
  }
@@ -40,7 +40,7 @@ export const ObsAdapter: FormFieldValueAdapter = {
40
40
  // an obs already loaded for this encounter stays bound to the field
41
41
  const value = extractFieldValue(
42
42
  field,
43
- findObsByFormField(flattenObsList(encounter.obs), assignedObsIds, field),
43
+ findObsByFormField(flattenObsList(encounter.obs), assignedObsIds, field, context.formFields),
44
44
  false,
45
45
  );
46
46
  if (!isEmpty(value)) {
@@ -277,12 +277,14 @@ function handleAttachments(field: FormField, attachments: Attachment[] = []) {
277
277
  *
278
278
  * Notes:
279
279
  * If the query by field-path returns an empty list, the function falls back to querying
280
- * by concept and uses `claimedObsIds` to exclude already assigned observations.
280
+ * by concept and uses `claimedObsIds` to exclude already assigned observations. Given the
281
+ * form's fields, it also leaves out obs whose path names another of them or a repeat of one.
281
282
  */
282
283
  export function findObsByFormField(
283
284
  obsList: Array<OpenmrsObs>,
284
285
  claimedObsIds: string[],
285
286
  field: FormField,
287
+ formFields?: Array<FormField>,
286
288
  ): OpenmrsObs[] {
287
289
  const obs = obsList.filter((candidate) => {
288
290
  // we ignore the concept for attachments because they're managed from the backend
@@ -297,13 +299,44 @@ export function findObsByFormField(
297
299
  // We shall fall back to mapping by the associated concept
298
300
  // That being said, we shall find all matching obs and pick the one that wasn't previously claimed.
299
301
  if (!obs?.length) {
300
- const obsByConcept = obsList.filter((obs) => obs.concept.uuid == field.questionOptions.concept);
302
+ const obsByConcept = obsList.filter(
303
+ (obs) => obs.concept.uuid == field.questionOptions.concept && !isBoundToAnotherField(obs, field, formFields),
304
+ );
301
305
  return claimedObsIds?.length ? obsByConcept.filter((obs) => !claimedObsIds.includes(obs.uuid)) : obsByConcept;
302
306
  }
303
307
 
304
308
  return obs;
305
309
  }
306
310
 
311
+ const repeatedFieldId = (fieldId: string) => fieldId.match(/^(.+)_\d+$/)?.[1];
312
+
313
+ /**
314
+ * Whether the obs's form field path names another field of the form, or a repeat of one such as `dateStopped_1`,
315
+ * which hydrates it by that path. The concept fallback leaves such an obs to it, as a field that recorded nothing
316
+ * would otherwise take a value from a repeat of itself or from another question on the same concept.
317
+ * A repeat still takes an obs saved under its question's path or that of any repeat of it: it is hydrated from its
318
+ * own group's obs only, and as repeats are numbered in order on hydration, one saved as `dateStopped_2` reopens as
319
+ * `dateStopped_1` once the row before it is deleted. A field of the form whose id only reads like a repeat, such as
320
+ * `item_1` beside `item`, is not one.
321
+ * A repeat of the field itself is told by its path alone, so the field never takes one even without the form's fields.
322
+ */
323
+ function isBoundToAnotherField(obs: OpenmrsObs, field: FormField, formFields: Array<FormField> = []) {
324
+ const path = obs.formFieldPath;
325
+ if (!path?.startsWith('rfe-forms-')) {
326
+ return false;
327
+ }
328
+ const fieldId = path.slice('rfe-forms-'.length);
329
+ if (repeatedFieldId(fieldId) === field.id) {
330
+ return true;
331
+ }
332
+ // Repeats are cloned while the encounter is hydrated, so they are not yet among the form's fields
333
+ const repeated = formFields.some((one) => one.id === field.id) ? undefined : repeatedFieldId(field.id);
334
+ if (repeated && (fieldId === repeated || repeatedFieldId(fieldId) === repeated)) {
335
+ return false;
336
+ }
337
+ return formFields.some((one) => one.id && (one.id === fieldId || one.id === repeatedFieldId(fieldId)));
338
+ }
339
+
307
340
  async function resolveAttachmentsFromObs(field: FormField, obs: OpenmrsObs[]) {
308
341
  const abortController = new AbortController();
309
342
  const attachments = await Promise.all(