From d7b168ba69197a43112472e80d69b171d4c5ed0f Mon Sep 17 00:00:00 2001 From: Tony Valle Date: Wed, 15 Nov 2023 18:15:23 +0100 Subject: [PATCH 1/2] fix: adjustments to rules engine input in profile widget --- .../DataEntry/ProgramRules/rulesContainer.js | 3 +-- .../DataEntry/hooks/useEvents.js | 6 ++--- .../DataEntry/hooks/useLifecycle.js | 2 +- .../WidgetProfile/hooks/useProgram.js | 23 ++++++++++++++++++- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/core_modules/capture-core/components/WidgetProfile/DataEntry/ProgramRules/rulesContainer.js b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/ProgramRules/rulesContainer.js index bb75e2a61d..65c90edabf 100644 --- a/src/core_modules/capture-core/components/WidgetProfile/DataEntry/ProgramRules/rulesContainer.js +++ b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/ProgramRules/rulesContainer.js @@ -1,13 +1,12 @@ // @flow import type { ProgramRulesContainer } from '@dhis2/rules-engine-javascript'; -import { getTrackedEntityAttributeId, getProgramId, getProgramRuleActions, getProgramStageId } from '../helpers'; +import { getProgramId, getProgramRuleActions, getProgramStageId } from '../helpers'; import { getRulesAndVariablesFromProgramIndicators } from '../../../../metaDataMemoryStoreBuilders/programs/getRulesAndVariablesFromIndicators'; const addProgramVariables = (program, programRuleVariables) => { program.programRuleVariables = programRuleVariables.map(programRulesVariable => ({ ...programRulesVariable, programId: getProgramId(programRulesVariable), - trackedEntityAttributeId: getTrackedEntityAttributeId(programRulesVariable), })); }; diff --git a/src/core_modules/capture-core/components/WidgetProfile/DataEntry/hooks/useEvents.js b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/hooks/useEvents.js index 0c900af9a4..6e606a08d4 100644 --- a/src/core_modules/capture-core/components/WidgetProfile/DataEntry/hooks/useEvents.js +++ b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/hooks/useEvents.js @@ -10,7 +10,7 @@ const getClientFormattedDataValuesAsObject = (dataValues, elementsById) => dataValues.reduce((acc, { dataElement: id, value }) => { const dataElement = elementsById[id]; if (dataElement) { - acc[id] = convertValue(value, elementsById[id].type); + acc[id] = convertValue(value, dataElement.valueType); } return acc; }, {}); @@ -39,8 +39,8 @@ export const useEvents = (enrollment: any, elementsById: Array) => { enrollmentId: event.enrollment, enrollmentStatus: event.enrollmentStatus, status: event.status, - eventDate: convertDate(event.eventDate), - dueDate: convertDate(event.dueDate), + occurredAt: convertDate(event.occurredAt), + scheduledAt: convertDate(event.scheduledAt), ...getClientFormattedDataValuesAsObject(event.dataValues, elementsById), })), [elementsById, enrollment, orgUnitNames], diff --git a/src/core_modules/capture-core/components/WidgetProfile/DataEntry/hooks/useLifecycle.js b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/hooks/useLifecycle.js index ce75b815f8..c60464a921 100644 --- a/src/core_modules/capture-core/components/WidgetProfile/DataEntry/hooks/useLifecycle.js +++ b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/hooks/useLifecycle.js @@ -49,7 +49,7 @@ export const useLifecycle = ({ const state = useSelector(stateArg => stateArg); const enrollment = useSelector(({ enrollmentDomain }) => enrollmentDomain?.enrollment); const dataElements: DataElements = useDataElements(programAPI); - const otherEvents = useEvents(enrollment, programAPI); + const otherEvents = useEvents(enrollment, dataElements); const orgUnit: ?OrgUnit = useOrganisationUnit(orgUnitId).orgUnit; const rulesContainer: ProgramRulesContainer = useRulesContainer(programAPI); const formFoundation: RenderFoundation = useFormFoundation(programAPI); diff --git a/src/core_modules/capture-core/components/WidgetProfile/hooks/useProgram.js b/src/core_modules/capture-core/components/WidgetProfile/hooks/useProgram.js index caf940d79e..154d1cc532 100644 --- a/src/core_modules/capture-core/components/WidgetProfile/hooks/useProgram.js +++ b/src/core_modules/capture-core/components/WidgetProfile/hooks/useProgram.js @@ -35,5 +35,26 @@ export const useProgram = (programId: string) => { ), ); - return { error, loading, program: !loading && data?.programs }; + const program = useMemo(() => { + if (data) { + const adaptedProgram = data.programs; + if (adaptedProgram.programRuleVariables) { + adaptedProgram.programRuleVariables = adaptedProgram.programRuleVariables.map( + ({ trackedEntityAttribute, dataElement, ...variable }) => { + if (dataElement) { + variable.dataElementId = dataElement.id; + } + if (trackedEntityAttribute) { + variable.trackedEntityAttributeId = trackedEntityAttribute.id; + } + return variable; + }, + ); + } + return adaptedProgram; + } + return data; + }, [data]); + + return { error, loading, program: !loading && program }; }; From 1a5614f1e873595dc873010f39886364c9c74643 Mon Sep 17 00:00:00 2001 From: Tony Valle Date: Thu, 16 Nov 2023 13:01:25 +0100 Subject: [PATCH 2/2] refactor: carry out suggestion in last comment --- .../DataEntry/ProgramRules/rulesContainer.js | 4 +++- .../WidgetProfile/hooks/useProgram.js | 23 +------------------ 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/src/core_modules/capture-core/components/WidgetProfile/DataEntry/ProgramRules/rulesContainer.js b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/ProgramRules/rulesContainer.js index 65c90edabf..554648322a 100644 --- a/src/core_modules/capture-core/components/WidgetProfile/DataEntry/ProgramRules/rulesContainer.js +++ b/src/core_modules/capture-core/components/WidgetProfile/DataEntry/ProgramRules/rulesContainer.js @@ -1,12 +1,14 @@ // @flow import type { ProgramRulesContainer } from '@dhis2/rules-engine-javascript'; -import { getProgramId, getProgramRuleActions, getProgramStageId } from '../helpers'; +import { getTrackedEntityAttributeId, getDataElementId, getProgramId, getProgramRuleActions, getProgramStageId } from '../helpers'; import { getRulesAndVariablesFromProgramIndicators } from '../../../../metaDataMemoryStoreBuilders/programs/getRulesAndVariablesFromIndicators'; const addProgramVariables = (program, programRuleVariables) => { program.programRuleVariables = programRuleVariables.map(programRulesVariable => ({ ...programRulesVariable, programId: getProgramId(programRulesVariable), + dataElementId: getDataElementId(programRulesVariable), + trackedEntityAttributeId: getTrackedEntityAttributeId(programRulesVariable), })); }; diff --git a/src/core_modules/capture-core/components/WidgetProfile/hooks/useProgram.js b/src/core_modules/capture-core/components/WidgetProfile/hooks/useProgram.js index 154d1cc532..caf940d79e 100644 --- a/src/core_modules/capture-core/components/WidgetProfile/hooks/useProgram.js +++ b/src/core_modules/capture-core/components/WidgetProfile/hooks/useProgram.js @@ -35,26 +35,5 @@ export const useProgram = (programId: string) => { ), ); - const program = useMemo(() => { - if (data) { - const adaptedProgram = data.programs; - if (adaptedProgram.programRuleVariables) { - adaptedProgram.programRuleVariables = adaptedProgram.programRuleVariables.map( - ({ trackedEntityAttribute, dataElement, ...variable }) => { - if (dataElement) { - variable.dataElementId = dataElement.id; - } - if (trackedEntityAttribute) { - variable.trackedEntityAttributeId = trackedEntityAttribute.id; - } - return variable; - }, - ); - } - return adaptedProgram; - } - return data; - }, [data]); - - return { error, loading, program: !loading && program }; + return { error, loading, program: !loading && data?.programs }; };