diff --git a/src/core_modules/capture-core/components/DataEntries/common/TEIAndEnrollment/useMetadataForRegistrationForm/useMetadataForRegistrationForm.js b/src/core_modules/capture-core/components/DataEntries/common/TEIAndEnrollment/useMetadataForRegistrationForm/useMetadataForRegistrationForm.js index d6cb49c504..97488cf7fd 100644 --- a/src/core_modules/capture-core/components/DataEntries/common/TEIAndEnrollment/useMetadataForRegistrationForm/useMetadataForRegistrationForm.js +++ b/src/core_modules/capture-core/components/DataEntries/common/TEIAndEnrollment/useMetadataForRegistrationForm/useMetadataForRegistrationForm.js @@ -1,7 +1,7 @@ // @flow import { useMemo } from 'react'; import { useOptionSetsForAttributes } from './hooks/useOptionSetsForAttributes'; -import { scopeTypes, Section } from '../../../../../metaData'; +import { scopeTypes } from '../../../../../metaData'; import { useProgramFromIndexedDB } from '../../../../../utils/cachedDataHooks/useProgramFromIndexedDB'; import { useScopeInfo } from '../../../../../hooks/useScopeInfo'; import { useTrackedEntityTypeCollection } from './hooks/useTrackedEntityTypeCollection'; @@ -72,20 +72,6 @@ export const useMetadataForRegistrationForm = ({ selectedScopeId }: Props) => { locale, }); - if (trackedEntityType && enrollment) { - for (const tetAttribute of trackedEntityType.trackedEntityTypeAttributes) { - if (tetAttribute.mandatory) { - const section = enrollment.enrollmentForm.getSection(Section.MAIN_SECTION_ID); - if (section) { - const enrollmentAttribute = section.elements && section.elements.get(tetAttribute.trackedEntityAttributeId); - if (enrollmentAttribute) { - enrollmentAttribute.compulsory = true; - } - } - } - } - } - if (scopeType === scopeTypes.TRACKED_ENTITY_TYPE && trackedEntityTypeCollection && tetId) { return { name: trackedEntityTypeCollection.name, diff --git a/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/enrollment/DataElementFactory.js b/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/enrollment/DataElementFactory.js index 18e56e1869..badd1853ea 100644 --- a/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/enrollment/DataElementFactory.js +++ b/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/enrollment/DataElementFactory.js @@ -16,6 +16,7 @@ import { dataElementUniqueScope, dataElementTypes, Section, + TrackedEntityType, } from '../../../../metaData'; import { OptionSetFactory } from '../../../common/factory'; import { convertFormToClient, convertClientToServer } from '../../../../converters'; @@ -127,6 +128,21 @@ export class DataElementFactory { }); } + static _isCompulsoryAttribute( + cachedProgramTrackedEntityAttribute: CachedProgramTrackedEntityAttribute, + trackedEntityType: TrackedEntityType, + ) { + if (cachedProgramTrackedEntityAttribute.mandatory) { + return true; + } + for (const attribute of trackedEntityType._attributes) { + if (attribute.id === cachedProgramTrackedEntityAttribute.trackedEntityAttributeId) { + return attribute.compulsory; + } + } + return false; + } + locale: ?string; optionSetFactory: OptionSetFactory; cachedTrackedEntityAttributes: Map; @@ -159,9 +175,10 @@ export class DataElementFactory { dataElement: DataElement, cachedProgramTrackedEntityAttribute: CachedProgramTrackedEntityAttribute, cachedTrackedEntityAttribute: CachedTrackedEntityAttribute, + trackedEntityType: TrackedEntityType, ) { dataElement.id = cachedTrackedEntityAttribute.id; - dataElement.compulsory = cachedProgramTrackedEntityAttribute.mandatory; + dataElement.compulsory = DataElementFactory._isCompulsoryAttribute(cachedProgramTrackedEntityAttribute, trackedEntityType); dataElement.code = cachedTrackedEntityAttribute.code; dataElement.attributeValues = cachedTrackedEntityAttribute.attributeValues; dataElement.name = @@ -208,6 +225,7 @@ export class DataElementFactory { async _buildBaseDataElement( cachedProgramTrackedEntityAttribute: CachedProgramTrackedEntityAttribute, cachedTrackedEntityAttribute: CachedTrackedEntityAttribute, + trackedEntityType: TrackedEntityType, section?: Section, ) { const dataElement = new DataElement(); @@ -217,6 +235,7 @@ export class DataElementFactory { dataElement, cachedProgramTrackedEntityAttribute, cachedTrackedEntityAttribute, + trackedEntityType, ); if (isNotValidOptionSet(dataElement.type, dataElement.optionSet)) { log.error(errorCreator(DataElementFactory.errorMessages.MULIT_TEXT_WITH_NO_OPTIONS_SET)({ dataElement })); @@ -228,6 +247,7 @@ export class DataElementFactory { async _buildDateDataElement( cachedProgramTrackedEntityAttribute: CachedProgramTrackedEntityAttribute, cachedTrackedEntityAttribute: CachedTrackedEntityAttribute, + trackedEntityType: TrackedEntityType, section?: Section, ) { const dateDataElement = new DateDataElement(); @@ -238,12 +258,14 @@ export class DataElementFactory { dateDataElement, cachedProgramTrackedEntityAttribute, cachedTrackedEntityAttribute, + trackedEntityType, ); return dateDataElement; } build( cachedProgramTrackedEntityAttribute: CachedProgramTrackedEntityAttribute, + trackedEntityType: TrackedEntityType, section?: Section, ) { const cachedTrackedEntityAttribute = cachedProgramTrackedEntityAttribute.trackedEntityAttributeId && @@ -260,7 +282,7 @@ export class DataElementFactory { } return cachedTrackedEntityAttribute.valueType === dataElementTypes.DATE ? - this._buildDateDataElement(cachedProgramTrackedEntityAttribute, cachedTrackedEntityAttribute, section) : - this._buildBaseDataElement(cachedProgramTrackedEntityAttribute, cachedTrackedEntityAttribute, section); + this._buildDateDataElement(cachedProgramTrackedEntityAttribute, cachedTrackedEntityAttribute, trackedEntityType, section) : + this._buildBaseDataElement(cachedProgramTrackedEntityAttribute, cachedTrackedEntityAttribute, trackedEntityType, section); } } diff --git a/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/enrollment/EnrollmentFactory.js b/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/enrollment/EnrollmentFactory.js index 3358d5f648..86d56c468a 100644 --- a/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/enrollment/EnrollmentFactory.js +++ b/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/enrollment/EnrollmentFactory.js @@ -116,6 +116,7 @@ export class EnrollmentFactory { async _buildMainSection( cachedProgramTrackedEntityAttributes?: ?Array, cachedProgramTrackedEntityTypeId?: ?string, + trackedEntityType: TrackedEntityType, ) { const section = new Section((o) => { o.id = Section.MAIN_SECTION_ID; @@ -130,12 +131,13 @@ export class EnrollmentFactory { featureTypeField && section.addElement(featureTypeField); } - await this._buildElementsForSection(cachedProgramTrackedEntityAttributes, section); + await this._buildElementsForSection(cachedProgramTrackedEntityAttributes, trackedEntityType, section); return section; } async _buildElementsForSection( cachedProgramTrackedEntityAttributes: ?Array, + trackedEntityType: TrackedEntityType, section: Section, ) { // $FlowFixMe @@ -157,7 +159,7 @@ export class EnrollmentFactory { await trackedEntityAttribute.fieldMap.asyncForEach(async (field) => { if (field.objectType) { - const fieldElement = await this.dataElementFactory.build(field); + const fieldElement = await this.dataElementFactory.build(field, trackedEntityType); if (!fieldElement) return; const fieldMetadata = formatPluginConfig(fieldElement, { attributes }); @@ -168,7 +170,7 @@ export class EnrollmentFactory { element && section.addElement(element); } else { - const element = await this.dataElementFactory.build(trackedEntityAttribute, section); + const element = await this.dataElementFactory.build(trackedEntityAttribute, trackedEntityType, section); element && section.addElement(element); } }); @@ -179,6 +181,7 @@ export class EnrollmentFactory { cachedProgramTrackedEntityAttributes?: Array, cachedSectionCustomLabel: string, cachedSectionCustomId: string, + trackedEntityType: TrackedEntityType, ) { if (!cachedProgramTrackedEntityAttributes?.length) { return null; @@ -190,7 +193,7 @@ export class EnrollmentFactory { o.group = Section.groups.ENROLLMENT; }); - await this._buildElementsForSection(cachedProgramTrackedEntityAttributes, section); + await this._buildElementsForSection(cachedProgramTrackedEntityAttributes, trackedEntityType, section); return section; } @@ -198,6 +201,7 @@ export class EnrollmentFactory { enrollmentForm: RenderFoundation, dataEntryForm: CachedDataEntryForm, cachedProgramTrackedEntityAttributes: ?Array, + trackedEntityType: TrackedEntityType, ) { if (!cachedProgramTrackedEntityAttributes) { return null; } @@ -208,7 +212,7 @@ export class EnrollmentFactory { section.showContainer = false; - section = await this._buildElementsForSection(cachedProgramTrackedEntityAttributes, section); + section = await this._buildElementsForSection(cachedProgramTrackedEntityAttributes, trackedEntityType, section); section && enrollmentForm.addSection(section); try { section.customForm = new CustomForm((o) => { @@ -233,6 +237,11 @@ export class EnrollmentFactory { o.name = cachedProgram.displayName; }); + const trackedEntityType = this.trackedEntityTypeCollection.get(cachedProgram.trackedEntityTypeId || ''); + if (!trackedEntityType) { + return enrollmentForm; + } + let section; if (cachedProgram.dataEntryForm) { if (cachedProgram.trackedEntityTypeId) { @@ -244,6 +253,7 @@ export class EnrollmentFactory { enrollmentForm, cachedProgram.dataEntryForm, cachedProgramTrackedEntityAttributes, + trackedEntityType, ); } else if (cachedProgramSections?.length || this.dataEntryFormConfig) { if (cachedProgram.trackedEntityTypeId) { @@ -289,6 +299,7 @@ export class EnrollmentFactory { attributes, formConfigSection.name, formConfigSection.id, + trackedEntityType, ); section && enrollmentForm.addSection(section); }); @@ -299,6 +310,7 @@ export class EnrollmentFactory { programSection.trackedEntityAttributes.map(id => trackedEntityAttributeDictionary[id]), programSection.displayFormName, programSection.id, + trackedEntityType, ); section && enrollmentForm.addSection(section); }); @@ -308,6 +320,7 @@ export class EnrollmentFactory { section = await this._buildMainSection( cachedProgramTrackedEntityAttributes, cachedProgram.trackedEntityTypeId, + trackedEntityType, ); section && enrollmentForm.addSection(section); } diff --git a/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/program/ProgramFactory.js b/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/program/ProgramFactory.js index 28ff91fe67..e7842bc13a 100644 --- a/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/program/ProgramFactory.js +++ b/src/core_modules/capture-core/metaDataMemoryStoreBuilders/programs/factory/program/ProgramFactory.js @@ -110,10 +110,13 @@ export class ProgramFactory { }); } - async _buildProgramAttributes(cachedProgramTrackedEntityAttributes: Array) { + async _buildProgramAttributes( + cachedProgramTrackedEntityAttributes: Array, + trackedEntityType: TrackedEntityType, + ) { const attributePromises = cachedProgramTrackedEntityAttributes.map(async (ptea) => { // $FlowFixMe[incompatible-call] automated comment - const dataElement = await this.dataElementFactory.build(ptea); + const dataElement = await this.dataElementFactory.build(ptea, trackedEntityType); return dataElement; }); @@ -124,6 +127,8 @@ export class ProgramFactory { } async build(cachedProgram: CachedProgram) { + const trackedEntityType = this.trackedEntityTypeCollection.get(cachedProgram.trackedEntityTypeId || ''); + let program; if (cachedProgram.programType === 'WITHOUT_REGISTRATION') { program = new EventProgram((o) => { @@ -146,7 +151,7 @@ export class ProgramFactory { o.name = cachedProgram.displayName; o.shortName = cachedProgram.displayShortName; // $FlowFixMe - o.trackedEntityType = this.trackedEntityTypeCollection.get(cachedProgram.trackedEntityTypeId); + o.trackedEntityType = trackedEntityType; }); if (cachedProgram.programTrackedEntityAttributes) { @@ -156,7 +161,7 @@ export class ProgramFactory { ); // $FlowFixMe - program.attributes = await this._buildProgramAttributes(cachedProgram.programTrackedEntityAttributes); + program.attributes = await this._buildProgramAttributes(cachedProgram.programTrackedEntityAttributes, trackedEntityType); } // $FlowFixMe