diff --git a/dhis2-mobile-program-rules/src/main/java/org/dhis2/mobileProgramRules/RuleEngineExtensions.kt b/dhis2-mobile-program-rules/src/main/java/org/dhis2/mobileProgramRules/RuleEngineExtensions.kt index 3668c00194..0289d3ca7a 100644 --- a/dhis2-mobile-program-rules/src/main/java/org/dhis2/mobileProgramRules/RuleEngineExtensions.kt +++ b/dhis2-mobile-program-rules/src/main/java/org/dhis2/mobileProgramRules/RuleEngineExtensions.kt @@ -177,10 +177,18 @@ fun ProgramRuleAction.toRuleEngineObject(): RuleAction { RuleAction( data = data() ?: "", type = ProgramRuleActionType.ASSIGN.name, - values = mutableMapOf( - Pair("field", field), - ).also { map -> - contentToDisplay?.let { map["content"] = it } + values = if (field.isNotEmpty()) { + mutableMapOf( + Pair("field", field), + ).also { map -> + contentToDisplay?.let { map["content"] = it } + } + } else { + contentToDisplay?.let { + mutableMapOf( + Pair("content", it), + ) + } ?: emptyMap() }, ) } diff --git a/dhis2-mobile-program-rules/src/test/java/org/dhis2/mobileProgramRules/RuleEngineExtensionsTest.kt b/dhis2-mobile-program-rules/src/test/java/org/dhis2/mobileProgramRules/RuleEngineExtensionsTest.kt index 2cc3e6e057..87be207381 100644 --- a/dhis2-mobile-program-rules/src/test/java/org/dhis2/mobileProgramRules/RuleEngineExtensionsTest.kt +++ b/dhis2-mobile-program-rules/src/test/java/org/dhis2/mobileProgramRules/RuleEngineExtensionsTest.kt @@ -9,6 +9,7 @@ import org.hisp.dhis.android.core.event.Event import org.hisp.dhis.android.core.option.Option import org.hisp.dhis.android.core.option.OptionCollectionRepository import org.hisp.dhis.android.core.program.ProgramRuleAction +import org.hisp.dhis.android.core.program.ProgramRuleActionType import org.hisp.dhis.android.core.program.ProgramRuleVariable import org.hisp.dhis.android.core.program.ProgramRuleVariableCollectionRepository import org.hisp.dhis.android.core.program.ProgramRuleVariableSourceType @@ -368,6 +369,19 @@ class RuleEngineExtensionsTest { assertTrue(ruleEngineAction.type == "unsupported") } + @Test + fun `Should parse program rule action to rule action with calculated value`() { + val ruleAction = ProgramRuleAction.builder() + .uid("uid") + .data("") + .content("calculated value") + .programRuleActionType(ProgramRuleActionType.ASSIGN) + .build() + val ruleEngineAction = ruleAction.toRuleEngineObject() + assertTrue(!ruleEngineAction.values.containsKey("field")) + assertTrue(ruleEngineAction.values["content"] == "calculated value") + } + @Test fun `should parse ProgramRuleVariable to RuleVariable`() { val programRuleVariable = ProgramRuleVariable.builder()