From 591ed116a550d5eb5c56e4d93f49acf3463c1424 Mon Sep 17 00:00:00 2001 From: Enrico Date: Mon, 13 May 2024 15:54:17 +0200 Subject: [PATCH] fix: Get environment from expected function --- src/commonMain/kotlin/org/hisp/dhis/rules/Environment.kt | 3 +++ .../kotlin/org/hisp/dhis/rules/engine/DefaultRuleEngine.kt | 7 ++++--- .../org/hisp/dhis/rules/models/TriggerEnvironment.kt | 7 ++++++- .../kotlin/org/hisp/dhis/rules/ProgramRuleVariableTest.kt | 2 +- src/jsMain/kotlin/org/hisp/dhis/rules/Environment.js.kt | 5 +++++ src/jvmMain/kotlin/org/hisp/dhis/rules/Environment.jvm.kt | 5 +++++ .../kotlin/org/hisp/dhis/rules/Environment.native.kt | 5 +++++ 7 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/commonMain/kotlin/org/hisp/dhis/rules/Environment.kt create mode 100644 src/jsMain/kotlin/org/hisp/dhis/rules/Environment.js.kt create mode 100644 src/jvmMain/kotlin/org/hisp/dhis/rules/Environment.jvm.kt create mode 100644 src/nativeMain/kotlin/org/hisp/dhis/rules/Environment.native.kt diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/Environment.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/Environment.kt new file mode 100644 index 00000000..a24063ad --- /dev/null +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/Environment.kt @@ -0,0 +1,3 @@ +package org.hisp.dhis.rules + +expect fun getEnvironment(): String \ No newline at end of file diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/DefaultRuleEngine.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/DefaultRuleEngine.kt index 6941f296..72996965 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/engine/DefaultRuleEngine.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/engine/DefaultRuleEngine.kt @@ -8,6 +8,7 @@ import org.hisp.dhis.lib.expression.spi.ValueType import org.hisp.dhis.rules.api.DataItem import org.hisp.dhis.rules.api.RuleEngine import org.hisp.dhis.rules.api.RuleEngineContext +import org.hisp.dhis.rules.getEnvironment import org.hisp.dhis.rules.models.* internal class DefaultRuleEngine: RuleEngine { @@ -15,7 +16,7 @@ internal class DefaultRuleEngine: RuleEngine { val valueMap = RuleVariableValueMapBuilder.target(target) .ruleVariables(executionContext.ruleVariables) .ruleEnrollment(ruleEnrollment) - .triggerEnvironment(TriggerEnvironment.SERVER) + .triggerEnvironment(TriggerEnvironment.valueOf(getEnvironment())) .ruleEvents(ruleEvents) .constantValueMap(executionContext.constantsValues) .build() @@ -28,7 +29,7 @@ internal class DefaultRuleEngine: RuleEngine { override fun evaluate(target: RuleEnrollment, ruleEvents: List, executionContext: RuleEngineContext): List { val valueMap = RuleVariableValueMapBuilder.target(target) .ruleVariables(executionContext.ruleVariables) - .triggerEnvironment(TriggerEnvironment.SERVER) + .triggerEnvironment(TriggerEnvironment.valueOf(getEnvironment())) .ruleEvents(ruleEvents) .constantValueMap(executionContext.constantsValues) .build() @@ -42,7 +43,7 @@ internal class DefaultRuleEngine: RuleEngine { val valueMap = RuleVariableValueMapBuilder.target() .ruleVariables(executionContext.ruleVariables) .ruleEnrollment(enrollmentTarget) - .triggerEnvironment(TriggerEnvironment.SERVER) + .triggerEnvironment(TriggerEnvironment.valueOf(getEnvironment())) .ruleEvents(eventsTarget) .constantValueMap(executionContext.constantsValues) .multipleBuild() diff --git a/src/commonMain/kotlin/org/hisp/dhis/rules/models/TriggerEnvironment.kt b/src/commonMain/kotlin/org/hisp/dhis/rules/models/TriggerEnvironment.kt index 0ef00de1..256ccb8c 100644 --- a/src/commonMain/kotlin/org/hisp/dhis/rules/models/TriggerEnvironment.kt +++ b/src/commonMain/kotlin/org/hisp/dhis/rules/models/TriggerEnvironment.kt @@ -1,4 +1,7 @@ package org.hisp.dhis.rules.models + +import kotlin.js.JsExport + /* * Copyright (c) 2004-2018, University of Oslo * All rights reserved. @@ -30,8 +33,10 @@ package org.hisp.dhis.rules.models /** * @author Zubair Asghar. */ +@JsExport enum class TriggerEnvironment(val clientName: String) { ANDROIDCLIENT("AndroidClient"), - SERVER("Server") + SERVER("Server"), + WEBCLIENT("WebClient") } diff --git a/src/commonTest/kotlin/org/hisp/dhis/rules/ProgramRuleVariableTest.kt b/src/commonTest/kotlin/org/hisp/dhis/rules/ProgramRuleVariableTest.kt index 90417cda..9663a1b1 100644 --- a/src/commonTest/kotlin/org/hisp/dhis/rules/ProgramRuleVariableTest.kt +++ b/src/commonTest/kotlin/org/hisp/dhis/rules/ProgramRuleVariableTest.kt @@ -89,7 +89,7 @@ class ProgramRuleVariableTest { fun testEnvironmentProgramVariableIsAssigned() { val rule = getRule("V{environment}") val ruleEffects = callEnrollmentRuleEngine(rule) - assertProgramRuleVariableAssignment(ruleEffects, rule, "Server") + assertProgramRuleVariableAssignment(ruleEffects, rule, TriggerEnvironment.valueOf(getEnvironment()).clientName) } @Test diff --git a/src/jsMain/kotlin/org/hisp/dhis/rules/Environment.js.kt b/src/jsMain/kotlin/org/hisp/dhis/rules/Environment.js.kt new file mode 100644 index 00000000..1b67f33e --- /dev/null +++ b/src/jsMain/kotlin/org/hisp/dhis/rules/Environment.js.kt @@ -0,0 +1,5 @@ +package org.hisp.dhis.rules + +actual fun getEnvironment(): String { + return "WEBCLIENT" +} \ No newline at end of file diff --git a/src/jvmMain/kotlin/org/hisp/dhis/rules/Environment.jvm.kt b/src/jvmMain/kotlin/org/hisp/dhis/rules/Environment.jvm.kt new file mode 100644 index 00000000..92d416b6 --- /dev/null +++ b/src/jvmMain/kotlin/org/hisp/dhis/rules/Environment.jvm.kt @@ -0,0 +1,5 @@ +package org.hisp.dhis.rules + +actual fun getEnvironment(): String { + return "SERVER" +} \ No newline at end of file diff --git a/src/nativeMain/kotlin/org/hisp/dhis/rules/Environment.native.kt b/src/nativeMain/kotlin/org/hisp/dhis/rules/Environment.native.kt new file mode 100644 index 00000000..dd0763ff --- /dev/null +++ b/src/nativeMain/kotlin/org/hisp/dhis/rules/Environment.native.kt @@ -0,0 +1,5 @@ +package org.hisp.dhis.rules + +actual fun getEnvironment(): String { + return "ANDROIDCLIENT" +} \ No newline at end of file