Skip to content

Commit

Permalink
fix: compulsory tet attributes in profile widget
Browse files Browse the repository at this point in the history
  • Loading branch information
superskip committed Nov 2, 2023
1 parent 601d0a2 commit 9c7ea0d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import log from 'loglevel';
import i18n from '@dhis2/d2-i18n';
import { pipe, errorCreator } from 'capture-core-utils';

import type { ProgramTrackedEntityAttribute, TrackedEntityAttribute, OptionSet as OptionSetType } from './types';
import type { ProgramTrackedEntityAttribute, TrackedEntityAttribute, TrackedEntityType, OptionSet as OptionSetType } from './types';
import {
DataElement,
DateDataElement,
Expand Down Expand Up @@ -101,21 +101,39 @@ const buildDataElementUnique = (
}
});

const isCompulsoryAttribute = (
id: string,
mandatory: boolean,
trackedEntityType: TrackedEntityType,
) => {
if (mandatory || !trackedEntityType.trackedEntityTypeAttributes) {
return mandatory;
}
for (const attribute of trackedEntityType.trackedEntityTypeAttributes) {
if (id === attribute.trackedEntityAttribute.id) {
return attribute.mandatory;
}
}
return false;
};

const setBaseProperties = async ({
dataElement,
optionSets,
programTrackedEntityAttribute,
trackedEntityAttribute,
trackedEntityType,
querySingleResource,
}: {
dataElement: DataElement,
optionSets: Array<OptionSetType>,
programTrackedEntityAttribute: ProgramTrackedEntityAttribute,
trackedEntityAttribute: TrackedEntityAttribute,
trackedEntityType: TrackedEntityType,
querySingleResource: QuerySingleResource,
}) => {
dataElement.id = trackedEntityAttribute.id;
dataElement.compulsory = programTrackedEntityAttribute.mandatory;
dataElement.compulsory = isCompulsoryAttribute(dataElement.id, programTrackedEntityAttribute.mandatory, trackedEntityType);
dataElement.name = trackedEntityAttribute.displayName;
dataElement.shortName = trackedEntityAttribute.displayShortName;
dataElement.formName = trackedEntityAttribute.displayFormName;
Expand Down Expand Up @@ -144,6 +162,7 @@ const buildBaseDataElement = async (
optionSets: Array<OptionSetType>,
programTrackedEntityAttribute: ProgramTrackedEntityAttribute,
trackedEntityAttribute: TrackedEntityAttribute,
trackedEntityType: TrackedEntityType,
querySingleResource: QuerySingleResource,
) => {
const dataElement = new DataElement();
Expand All @@ -153,6 +172,7 @@ const buildBaseDataElement = async (
optionSets,
programTrackedEntityAttribute,
trackedEntityAttribute,
trackedEntityType,
querySingleResource,
});
if (isNotValidOptionSet(dataElement.type, dataElement.optionSet)) {
Expand All @@ -166,6 +186,7 @@ const buildDateDataElement = async (
optionSets: Array<OptionSetType>,
programTrackedEntityAttribute: ProgramTrackedEntityAttribute,
trackedEntityAttribute: TrackedEntityAttribute,
trackedEntityType: TrackedEntityType,
querySingleResource: QuerySingleResource,
) => {
const dateDataElement = new DateDataElement();
Expand All @@ -176,6 +197,7 @@ const buildDateDataElement = async (
optionSets,
programTrackedEntityAttribute,
trackedEntityAttribute,
trackedEntityType,
querySingleResource,
});
return dateDataElement;
Expand Down Expand Up @@ -247,6 +269,7 @@ export const buildTetFeatureType = (featureType: 'POINT' | 'POLYGON') => {
export const buildDataElement = (
programTrackedEntityAttribute: ProgramTrackedEntityAttribute,
trackedEntityAttributes: Array<TrackedEntityAttribute>,
trackedEntityType: TrackedEntityType,
optionSets: Array<OptionSetType>,
querySingleResource: QuerySingleResource,
) => {
Expand All @@ -267,6 +290,6 @@ export const buildDataElement = (
}

return trackedEntityAttribute.valueType === dataElementTypes.DATE
? buildDateDataElement(optionSets, programTrackedEntityAttribute, trackedEntityAttribute, querySingleResource)
: buildBaseDataElement(optionSets, programTrackedEntityAttribute, trackedEntityAttribute, querySingleResource);
? buildDateDataElement(optionSets, programTrackedEntityAttribute, trackedEntityAttribute, trackedEntityType, querySingleResource)
: buildBaseDataElement(optionSets, programTrackedEntityAttribute, trackedEntityAttribute, trackedEntityType, querySingleResource);
};
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const buildMainSection = async ({
await buildElementsForSection({
programTrackedEntityAttributes,
trackedEntityAttributes,
trackedEntityType,
optionSets,
section,
querySingleResource,
Expand All @@ -89,12 +90,14 @@ const buildMainSection = async ({
const buildElementsForSection = async ({
programTrackedEntityAttributes,
trackedEntityAttributes,
trackedEntityType,
optionSets,
section,
querySingleResource,
}: {
programTrackedEntityAttributes: Array<ProgramTrackedEntityAttribute>,
trackedEntityAttributes: Array<TrackedEntityAttribute>,
trackedEntityType: TrackedEntityType,
optionSets: Array<OptionSet>,
section: Section,
querySingleResource: QuerySingleResource,
Expand All @@ -104,6 +107,7 @@ const buildElementsForSection = async ({
const element = await buildDataElement(
trackedEntityAttribute,
trackedEntityAttributes,
trackedEntityType,
optionSets,
querySingleResource,
);
Expand All @@ -115,13 +119,15 @@ const buildElementsForSection = async ({
const buildSection = async ({
programTrackedEntityAttributes,
trackedEntityAttributes,
trackedEntityType,
optionSets,
sectionCustomLabel,
sectionCustomId,
querySingleResource,
}: {
programTrackedEntityAttributes?: Array<ProgramTrackedEntityAttribute>,
trackedEntityAttributes: Array<TrackedEntityAttribute>,
trackedEntityType: TrackedEntityType,
optionSets: Array<OptionSet>,
sectionCustomLabel: string,
sectionCustomId: string,
Expand All @@ -139,6 +145,7 @@ const buildSection = async ({
await buildElementsForSection({
programTrackedEntityAttributes,
trackedEntityAttributes,
trackedEntityType,
optionSets,
section,
querySingleResource,
Expand Down Expand Up @@ -185,6 +192,7 @@ export const buildFormFoundation = async (program: any, querySingleResource: Que
section = await buildSection({
programTrackedEntityAttributes: builtProgramSection.map(id => trackedEntityAttributeDictionary[id]),
trackedEntityAttributes,
trackedEntityType,
optionSets,
sectionCustomLabel: programSection.displayFormName,
sectionCustomId: programSection.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ type OptionGroup = {
};

type TrackedEntityTypeAttribute = {
trackedEntityAttributeId: string,
trackedEntityAttribute: { id: string },
displayInList: boolean,
mandatory: boolean,
mandatory: ?boolean,
searchable: boolean,
renderOptionsAsRadio: ?boolean,
};
Expand Down

0 comments on commit 9c7ea0d

Please sign in to comment.