@iapps/ng-dhis2-ui 1.3.3 → 1.3.5

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.
@@ -11939,6 +11939,9 @@ class ProgramEntryFormFieldUtil {
11939
11939
  });
11940
11940
  }
11941
11941
  get dataElements() {
11942
+ if (this.config.excludeProgramStages) {
11943
+ return [];
11944
+ }
11942
11945
  const programStages = this.config.programStage
11943
11946
  ? this.program.programStages?.filter((programStage) => programStage.id === this.config.programStage)
11944
11947
  : this.program.programStages;
@@ -12920,7 +12923,7 @@ class ProgramEntryFormMetaData {
12920
12923
  });
12921
12924
  }
12922
12925
  get programStageSections() {
12923
- if (!this.program) {
12926
+ if (!this.program || this.config.excludeProgramStages) {
12924
12927
  return [];
12925
12928
  }
12926
12929
  const { displayType, formType } = this.config;
@@ -13094,9 +13097,16 @@ class ProgramEntryFormModule {
13094
13097
  if (!this.instance()) {
13095
13098
  return {};
13096
13099
  }
13100
+ const attributeDataValues = this.trackedEntityInstance()
13101
+ ? this.#getTrackedEntityDataValues(this.trackedEntityInstance())
13102
+ : {};
13103
+ // TODO: Consider refactoring this logic to avoid potential issues with nested data values. The current implementation may not handle deeply nested structures correctly, which could lead to unexpected behavior when updating the instance with new data values.
13104
+ const currentInstanceDataValues = this.config().formType === 'TRACKER'
13105
+ ? this.#getTrackedEntityDataValues(this.instance())
13106
+ : this.instance();
13097
13107
  return {
13098
- ...(this.trackedEntityInstance() || {}),
13099
- ...(this.instance() || {}),
13108
+ ...attributeDataValues,
13109
+ ...(currentInstanceDataValues || {}),
13100
13110
  };
13101
13111
  }, /* @ts-ignore */
13102
13112
  ...(ngDevMode ? [{ debugName: "dataEntities" }] : /* istanbul ignore next */ []));
@@ -13134,6 +13144,16 @@ class ProgramEntryFormModule {
13134
13144
  }, /* @ts-ignore */
13135
13145
  ...(ngDevMode ? [{ debugName: "FormActionButtons" }] : /* istanbul ignore next */ []));
13136
13146
  }
13147
+ #getTrackedEntityDataValues(trackedEntity) {
13148
+ const attributeDataValues = Object.keys(trackedEntity?.attributeEntities ?? {}).reduce((acc, key) => {
13149
+ acc[key] = trackedEntity?.attributeEntities?.[key]?.value;
13150
+ return acc;
13151
+ }, {});
13152
+ return {
13153
+ ...attributeDataValues,
13154
+ ...(trackedEntity?.reportEntities ?? {}),
13155
+ };
13156
+ }
13137
13157
  async ngOnInit() {
13138
13158
  this.loading.set(true);
13139
13159
  try {
@@ -13197,44 +13217,6 @@ class ProgramEntryFormModule {
13197
13217
  }
13198
13218
  return configuredKey;
13199
13219
  }
13200
- // #updateInstanceWithAutoAssignedValues(
13201
- // autoAssignedValues: AutoAssignedValues[]
13202
- // ) {
13203
- // const assignedDataValues = autoAssignedValues.reduce(
13204
- // (entities, assignedValue) => {
13205
- // return {
13206
- // ...entities,
13207
- // [assignedValue.field]: assignedValue.value,
13208
- // };
13209
- // },
13210
- // {}
13211
- // );
13212
- // this.#updateInstance(assignedDataValues);
13213
- // }
13214
- // #updateInstanceWithAutoAssignedValues(
13215
- // autoAssignedValues: AutoAssignedValues[]
13216
- // ) {
13217
- // const valueCount = autoAssignedValues?.length ?? 0;
13218
- // if (valueCount === 0) {
13219
- // return;
13220
- // }
13221
- // const metaData = this.metaData();
13222
- // const aggregatedValues: Record<string, unknown> = {};
13223
- // for (let index = 0; index < valueCount; index++) {
13224
- // const { field: configuredKey, value } = autoAssignedValues[index];
13225
- // const semanticKey = metaData
13226
- // ? this.#resolveFieldRuntimeId(configuredKey, metaData)
13227
- // : configuredKey;
13228
- // aggregatedValues[semanticKey] = value;
13229
- // if (configuredKey !== semanticKey) {
13230
- // aggregatedValues[configuredKey] = value;
13231
- // }
13232
- // }
13233
- // if (Object.keys(aggregatedValues).length === 0) {
13234
- // return;
13235
- // }
13236
- // this.#updateInstance(aggregatedValues);
13237
- // }
13238
13220
  #updateInstanceWithAutoAssignedValues(autoAssignedValues) {
13239
13221
  const trackedEntityId = this.trackedEntity?.();
13240
13222
  if (typeof trackedEntityId === 'string' &&
@@ -13262,21 +13244,6 @@ class ProgramEntryFormModule {
13262
13244
  }
13263
13245
  this.#updateInstance(aggregatedValues);
13264
13246
  }
13265
- // #updateInstance(dataValues: Record<string, unknown>) {
13266
- // this.instance.update((instance) => {
13267
- // instance?.updateDataValues(dataValues, this.config().updateTeiOrgUnit);
13268
- // return instance;
13269
- // });
13270
- // }
13271
- // #updateInstance(dataValues: Record<string, unknown>) {
13272
- // this.instance.update((instance) => {
13273
- // if (!instance) {
13274
- // return instance;
13275
- // }
13276
- // instance.updateDataValues(dataValues, this.config().updateTeiOrgUnit);
13277
- // return instance;
13278
- // });
13279
- // }
13280
13247
  #updateInstance(dataValues) {
13281
13248
  if (!dataValues || Object.keys(dataValues).length === 0) {
13282
13249
  return;
@@ -13307,7 +13274,8 @@ class ProgramEntryFormModule {
13307
13274
  if (this.config().autoComplete) {
13308
13275
  this.instance().complete();
13309
13276
  }
13310
- return this.instanceQuery
13277
+ const instanceQuery = this.#getQuery('TRACKER');
13278
+ return instanceQuery
13311
13279
  .setData(this.instance())
13312
13280
  .save();
13313
13281
  }
@@ -13315,9 +13283,8 @@ class ProgramEntryFormModule {
13315
13283
  if (this.config().autoComplete) {
13316
13284
  this.instance().complete();
13317
13285
  }
13318
- return this.instanceQuery
13319
- .setData(this.instance())
13320
- .save();
13286
+ const instanceQuery = this.#getQuery('EVENT');
13287
+ return instanceQuery.setData(this.instance()).save();
13321
13288
  }
13322
13289
  default:
13323
13290
  return firstValueFrom(of(null));
@@ -13335,43 +13302,51 @@ class ProgramEntryFormModule {
13335
13302
  });
13336
13303
  return instance;
13337
13304
  }
13338
- async #getTrackerInstance() {
13305
+ #getQuery(instanceType) {
13339
13306
  const d2 = window.d2Web;
13340
- this.instanceQuery = d2?.trackerModule?.trackedEntity
13341
- ?.setProgram(this.config().program)
13342
- ?.setOrgUnit(this.orgUnit());
13343
- if (!this.instanceQuery) {
13307
+ switch (instanceType) {
13308
+ case 'TRACKER':
13309
+ return d2.trackerModule.trackedEntity
13310
+ .setProgram(this.config().program)
13311
+ .setOrgUnit(this.orgUnit());
13312
+ case 'EVENT':
13313
+ return d2.eventModule.event
13314
+ .setProgram(this.config().program)
13315
+ .setProgramStage(this.config().programStage)
13316
+ .setEnrollment(this.enrollment())
13317
+ .setTrackedEntity(this.trackedEntity())
13318
+ .setOrgUnit(this.orgUnit());
13319
+ default:
13320
+ throw new Error('Invalid form type');
13321
+ }
13322
+ }
13323
+ async #getTrackerInstance() {
13324
+ const instanceQuery = this.#getQuery('TRACKER');
13325
+ if (!instanceQuery) {
13344
13326
  throw new Error('Could not initialize tracker query');
13345
13327
  }
13346
13328
  if (!this.trackedEntity()) {
13347
- return await this.instanceQuery.create();
13329
+ return await instanceQuery.create();
13348
13330
  }
13349
- const instanceResult = (await this.instanceQuery
13350
- .setTrackedEntity(this.trackedEntity())
13351
- .get()).data;
13331
+ const instanceResult = (await instanceQuery.setTrackedEntity(this.trackedEntity()).get()).data;
13352
13332
  if (!instanceResult) {
13353
- return await this.instanceQuery.create();
13333
+ return await instanceQuery.create();
13354
13334
  }
13355
- this.instanceQuery.setInstanceFields(this.metaData()?.program);
13356
- const instance = await this.instanceQuery.setReservedValues();
13335
+ instanceQuery.setInstanceFields(this.metaData()?.program);
13336
+ const instance = await instanceQuery.setReservedValues();
13357
13337
  if (this.orgUnit()) {
13358
13338
  instance.setOrgUnit(this.orgUnit());
13359
13339
  }
13360
13340
  return instance;
13361
13341
  }
13362
13342
  async #getEventInstance() {
13363
- this.instanceQuery = this.d2.eventModule.event
13364
- .setProgram(this.config().program)
13365
- .setProgramStage(this.config().programStage)
13366
- .setEnrollment(this.enrollment())
13367
- .setTrackedEntity(this.trackedEntity())
13368
- .setOrgUnit(this.orgUnit());
13343
+ const instanceQuery = this.#getQuery('EVENT');
13369
13344
  if (!this.event()) {
13370
- return await this.instanceQuery.create();
13345
+ return await instanceQuery.create();
13371
13346
  }
13372
- const instance = (await this.instanceQuery.setEvent(this.event()).get()).data;
13347
+ const instance = (await instanceQuery.setEvent(this.event()).get()).data;
13373
13348
  if (!instance) {
13374
- return await this.instanceQuery.create();
13349
+ return await instanceQuery.create();
13375
13350
  }
13376
13351
  if (this.metaData()?.program) {
13377
13352
  instance.setFields(this.metaData().program);