Skip to content

Commit

Permalink
fix: Use eventDate as fallback for event_id variable
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Jan 24, 2025
1 parent 4c00a48 commit 7d9d1c9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ internal class RuleVariableValueMapBuilder {
RuleValueType.TEXT,
ruleEvent.event,
listOf(ruleEvent.event),
currentDate.toString(),
ruleEvent.eventDate
.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.toString(),
)
val status = ruleEvent.status.toString()
valueMap[RuleEngineUtils.ENV_VAR_EVENT_STATUS] =
Expand Down Expand Up @@ -210,7 +213,7 @@ internal class RuleVariableValueMapBuilder {
RuleValueType.TEXT,
ruleEnrollment.enrollment,
listOf(ruleEnrollment.enrollment),
currentDate.toString(),
ruleEnrollment.enrollmentDate.toString(),
)
valueMap[RuleEngineUtils.ENV_VAR_ENROLLMENT_COUNT] =
RuleVariableValue(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.hisp.dhis.rules.models

import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import org.hisp.dhis.rules.engine.RuleVariableValue
import org.hisp.dhis.rules.utils.getLastUpdateDate

class RuleVariableNewestEvent(
override val name: String,
Expand All @@ -25,7 +26,10 @@ class RuleVariableNewestEvent(
fieldType,
optionValue,
ruleDataValues.map { it.value },
getLastUpdateDate(ruleDataValues),
value.eventDate
.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.toString(),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.hisp.dhis.rules.models

import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import org.hisp.dhis.rules.engine.RuleVariableValue
import org.hisp.dhis.rules.utils.getLastUpdateDate

class RuleVariableNewestStageEvent(
override val name: String,
Expand All @@ -27,7 +28,10 @@ class RuleVariableNewestStageEvent(
fieldType,
optionValue,
stageRuleDataValues.map { it.value },
getLastUpdateDate(stageRuleDataValues),
value.eventDate
.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.toString(),
)
}
}
Expand Down
33 changes: 0 additions & 33 deletions src/commonMain/kotlin/org/hisp/dhis/rules/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,6 @@ import kotlinx.datetime.*
import org.hisp.dhis.rules.models.RuleDataValueHistory
import org.hisp.dhis.rules.models.RuleEvent

fun getLastUpdateDateForPrevious(
ruleDataValues: List<RuleDataValueHistory>,
ruleEvent: RuleEvent,
): String {
val dates: MutableList<Instant> = ArrayList()
for (date in ruleDataValues) {
val d = date.eventDate
if (d < ruleEvent.eventDate ||
(ruleEvent.eventDate == d && ruleEvent.createdDate > date.createdDate)
) {
dates.add(d)
}
}
return dates
.max()
.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.toString()
}

fun getLastUpdateDate(ruleDataValues: List<RuleDataValueHistory>): String {
val dates: MutableList<Instant> = ArrayList()
for (date in ruleDataValues) {
val d = date.eventDate
dates.add(d)
}
return dates
.max()
.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.toString()
}

fun unwrapVariableName(variable: String): String {
if (variable.startsWith("#{") && variable.endsWith("}")) {
return variable.substring(2, variable.length - 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3121,6 +3121,52 @@ class RuleEngineFunctionTest {
assertEquals(dayAfterTomorrow.toString(), ruleEffects[0].data)
}

@Test
fun evaluateLastEventDateForEventIdVariable() {
val dayBeforeYesterday =
LocalDate.Companion
.currentDate()
.minus(
2,
DateTimeUnit.DAY,
).atStartOfDayIn(TimeZone.currentSystemDefault())
val ruleAction =
RuleAction(
"d2:lastEventDate(V{event_id})",
"DISPLAYTEXT",
mapOf(Pair("content", "test_action_content"), Pair("location", "feedback")),
)

val rule = Rule("true", listOf(ruleAction), "test_rule", "")
val ruleEngineContext = RuleEngineTestUtils.getRuleEngineContext(rule, listOf())
val ruleEvent1 =
RuleEvent(
"test_event1",
"test_program_stage1",
"",
RuleEventStatus.ACTIVE,
dayBeforeYesterday,
dayBeforeYesterday,
LocalDate.currentDate(),
null,
"",
null,
listOf(
RuleDataValue(
"test_data_element_one",
"value1",
),
),
)
val ruleEffects = RuleEngine.getInstance().evaluate(ruleEvent1, null, listOf(), ruleEngineContext)
assertEquals(1, ruleEffects.size)
assertEquals(ruleAction, ruleEffects[0].ruleAction)
val expectedDate = dayBeforeYesterday.toLocalDateTime(TimeZone.currentSystemDefault())
.date
.toString()
assertEquals(expectedDate, ruleEffects[0].data)
}

companion object {
private const val DATE_PATTERN = "yyyy-MM-dd"
private const val USE_CODE_FOR_OPTION_SET = true
Expand Down

0 comments on commit 7d9d1c9

Please sign in to comment.