Skip to content

Commit

Permalink
refactor: modify compulsory in formFoundation
Browse files Browse the repository at this point in the history
  • Loading branch information
superskip committed Nov 2, 2023
1 parent 53b088a commit 601d0a2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
dataElementUniqueScope,
dataElementTypes,
Section,
TrackedEntityType,
} from '../../../../metaData';
import { OptionSetFactory } from '../../../common/factory';
import { convertFormToClient, convertClientToServer } from '../../../../converters';
Expand Down Expand Up @@ -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<string, CachedTrackedEntityAttribute>;
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -208,6 +225,7 @@ export class DataElementFactory {
async _buildBaseDataElement(
cachedProgramTrackedEntityAttribute: CachedProgramTrackedEntityAttribute,
cachedTrackedEntityAttribute: CachedTrackedEntityAttribute,
trackedEntityType: TrackedEntityType,
section?: Section,
) {
const dataElement = new DataElement();
Expand All @@ -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 }));
Expand All @@ -228,6 +247,7 @@ export class DataElementFactory {
async _buildDateDataElement(
cachedProgramTrackedEntityAttribute: CachedProgramTrackedEntityAttribute,
cachedTrackedEntityAttribute: CachedTrackedEntityAttribute,
trackedEntityType: TrackedEntityType,
section?: Section,
) {
const dateDataElement = new DateDataElement();
Expand All @@ -238,12 +258,14 @@ export class DataElementFactory {
dateDataElement,
cachedProgramTrackedEntityAttribute,
cachedTrackedEntityAttribute,
trackedEntityType,
);
return dateDataElement;
}

build(
cachedProgramTrackedEntityAttribute: CachedProgramTrackedEntityAttribute,
trackedEntityType: TrackedEntityType,
section?: Section,
) {
const cachedTrackedEntityAttribute = cachedProgramTrackedEntityAttribute.trackedEntityAttributeId &&
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class EnrollmentFactory {
async _buildMainSection(
cachedProgramTrackedEntityAttributes?: ?Array<CachedProgramTrackedEntityAttribute>,
cachedProgramTrackedEntityTypeId?: ?string,
trackedEntityType: TrackedEntityType,
) {
const section = new Section((o) => {
o.id = Section.MAIN_SECTION_ID;
Expand All @@ -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<CachedProgramTrackedEntityAttribute>,
trackedEntityType: TrackedEntityType,
section: Section,
) {
// $FlowFixMe
Expand All @@ -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 });
Expand All @@ -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);
}
});
Expand All @@ -179,6 +181,7 @@ export class EnrollmentFactory {
cachedProgramTrackedEntityAttributes?: Array<CachedProgramTrackedEntityAttribute>,
cachedSectionCustomLabel: string,
cachedSectionCustomId: string,
trackedEntityType: TrackedEntityType,
) {
if (!cachedProgramTrackedEntityAttributes?.length) {
return null;
Expand All @@ -190,14 +193,15 @@ export class EnrollmentFactory {
o.group = Section.groups.ENROLLMENT;
});

await this._buildElementsForSection(cachedProgramTrackedEntityAttributes, section);
await this._buildElementsForSection(cachedProgramTrackedEntityAttributes, trackedEntityType, section);
return section;
}

async _buildCustomEnrollmentForm(
enrollmentForm: RenderFoundation,
dataEntryForm: CachedDataEntryForm,
cachedProgramTrackedEntityAttributes: ?Array<CachedProgramTrackedEntityAttribute>,
trackedEntityType: TrackedEntityType,
) {
if (!cachedProgramTrackedEntityAttributes) { return null; }

Expand All @@ -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) => {
Expand All @@ -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) {
Expand All @@ -244,6 +253,7 @@ export class EnrollmentFactory {
enrollmentForm,
cachedProgram.dataEntryForm,
cachedProgramTrackedEntityAttributes,
trackedEntityType,
);
} else if (cachedProgramSections?.length || this.dataEntryFormConfig) {
if (cachedProgram.trackedEntityTypeId) {
Expand Down Expand Up @@ -289,6 +299,7 @@ export class EnrollmentFactory {
attributes,
formConfigSection.name,
formConfigSection.id,
trackedEntityType,
);
section && enrollmentForm.addSection(section);
});
Expand All @@ -299,6 +310,7 @@ export class EnrollmentFactory {
programSection.trackedEntityAttributes.map(id => trackedEntityAttributeDictionary[id]),
programSection.displayFormName,
programSection.id,
trackedEntityType,
);
section && enrollmentForm.addSection(section);
});
Expand All @@ -308,6 +320,7 @@ export class EnrollmentFactory {
section = await this._buildMainSection(
cachedProgramTrackedEntityAttributes,
cachedProgram.trackedEntityTypeId,
trackedEntityType,
);
section && enrollmentForm.addSection(section);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,13 @@ export class ProgramFactory {
});
}

async _buildProgramAttributes(cachedProgramTrackedEntityAttributes: Array<CachedProgramTrackedEntityAttribute>) {
async _buildProgramAttributes(
cachedProgramTrackedEntityAttributes: Array<CachedProgramTrackedEntityAttribute>,
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;
});

Expand All @@ -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) => {
Expand All @@ -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) {
Expand All @@ -156,7 +161,7 @@ export class ProgramFactory {
);

// $FlowFixMe
program.attributes = await this._buildProgramAttributes(cachedProgram.programTrackedEntityAttributes);
program.attributes = await this._buildProgramAttributes(cachedProgram.programTrackedEntityAttributes, trackedEntityType);
}

// $FlowFixMe
Expand Down

0 comments on commit 601d0a2

Please sign in to comment.