Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [DHIS2-15967] allow enrolling TEIs with mandatory TET attributes #3455

Merged
merged 8 commits into from
Nov 16, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import type { ExistingUniqueValueDialogActionsComponent } from '../withErrorMess
import type { InputAttribute } from './hooks/useFormValues';
import { RenderFoundation, ProgramStage } from '../../../metaData';

type TrackedEntityAttributes = Array<{
attribute: string,
value: any,
}>;

export type EnrollmentPayload = {|
trackedEntity: string,
trackedEntityType: string,
orgUnit: string,
geometry: any,
attributes: TrackedEntityAttributes,
enrollments: [
{|
occurredAt: string,
Expand All @@ -23,10 +29,7 @@ export type EnrollmentPayload = {|
events: Array<{
orgUnit: string,
}>,
attributes: Array<{
attribute: string,
value: any,
}>,
attributes: TrackedEntityAttributes,
|}
]
|}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,15 @@ export const useBuildEnrollmentPayload = ({
? [firstStageDuringRegistrationEvent, ...autoGenerateEvents]
: autoGenerateEvents;

const attributes = deriveAttributesFromFormValues(formServerValues);

const enrollment = {
program: programId,
status: 'ACTIVE',
orgUnit: orgUnitId,
occurredAt,
enrolledAt,
attributes: deriveAttributesFromFormValues(formServerValues),
attributes,
events: allEventsToBeCreated,
};

Expand All @@ -177,6 +179,7 @@ export const useBuildEnrollmentPayload = ({
trackedEntity: teiId || generateUID(),
orgUnit: orgUnitId,
trackedEntityType: trackedEntityTypeId,
attributes,
geometry,
enrollments: [enrollment],
};
Expand Down
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 } from '../../../../../metaData';
import { scopeTypes, Section } from '../../../../../metaData';
import { useProgramFromIndexedDB } from '../../../../../utils/cachedDataHooks/useProgramFromIndexedDB';
import { useScopeInfo } from '../../../../../hooks/useScopeInfo';
import { useTrackedEntityTypeCollection } from './hooks/useTrackedEntityTypeCollection';
Expand Down Expand Up @@ -72,6 +72,20 @@ 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding the compulsory logic when the DataElement is built rather than when the metadata is retrieved from IndexedDB.
To achieve this, you can pass trackedEntityTypeCollection to DataElementFactory inside EnrollmentFactory. Then it can be used inside _setBaseProperties method to calculate the dataElement.compulsory.
WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right - I did miss a spot with the profile widget! I'll try your suggestion : )

}
}
}
}
}

if (scopeType === scopeTypes.TRACKED_ENTITY_TYPE && trackedEntityTypeCollection && tetId) {
return {
name: trackedEntityTypeCollection.name,
Expand Down
Loading