Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.smartregister.fhircore.engine.util.DateUtils.isToday
import org.smartregister.fhircore.engine.util.DateUtils.today

const val GUARDIAN_VISIT_CODE = "guardian-visit"
const val CLINICAL_VISIT_ORDER_CODE_REGEX_FORMAT = "clinic-visit-task-order-\\d*\\.?\\d*\$"

fun Task.hasPastEnd() =
this.hasExecutionPeriod() &&
Expand All @@ -36,14 +37,18 @@ fun Task.hasStarted() =

fun Task.TaskStatus.toCoding() = Coding(this.system, this.toCode(), this.display)

fun Task.clinicVisitOrder(systemTag: String) =
fun Task.clinicVisitOrder(systemTag: String): Double? =
this.meta
.tag
.asSequence()
.filter { it.system.equals(systemTag, true) }
.filterNot { it.code.isNullOrBlank() }
.map { it.code.replace("_", "-").substringAfterLast("-").trim() }
.map { it.toDouble() }
.filter { !it.code.isNullOrBlank() }
.map { it.code.replace("_", "-") }
.filter {
it.matches(Regex(CLINICAL_VISIT_ORDER_CODE_REGEX_FORMAT, option = RegexOption.IGNORE_CASE))
}
.map { it.substringAfterLast("-").trim() }
.map { it.toDoubleOrNull() }
.lastOrNull()

fun Task.isGuardianVisit(systemTag: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import org.smartregister.fhircore.engine.robolectric.RobolectricTest
import org.smartregister.fhircore.engine.rule.CoroutineTestRule
import org.smartregister.fhircore.engine.util.DefaultDispatcherProvider
import org.smartregister.fhircore.engine.util.extension.asReference
import org.smartregister.fhircore.engine.util.extension.clinicVisitOrder
import org.smartregister.fhircore.engine.util.extension.referenceValue

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -244,6 +245,11 @@ class HivRegisterDaoTest : RobolectricTest() {
}
assertNotNull(data)
val hivProfileData = data as ProfileData.HivProfileData
val order = hivProfileData.tasks.none { it.clinicVisitOrder("") != null }
Assert.assertEquals(hivProfileData.tasks.isEmpty(), false)
val sorted = hivProfileData.tasks.sortedWith(compareBy { it.description }).isEmpty()
Assert.assertFalse(sorted)
Assert.assertEquals(order, true)
assertEquals("50y", hivProfileData.age)
assertEquals("Dist 1 City 1", hivProfileData.address)
assertEquals("John Doe", hivProfileData.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,60 @@ class TaskExtensionTest {
Assert.assertFalse(testTask2.isGuardianVisit(systemTag))
}

@Test
fun `task clinicVisitOrder`() {
val systemTag = "https://d-tree.org"
Assert.assertEquals(3.0, testTask1.clinicVisitOrder(systemTag))
Assert.assertEquals(testTask2.clinicVisitOrder(systemTag), 1.0)
val task = testTask1.copy().apply { meta.tag.first().code = systemTag }
Assert.assertNull(task.clinicVisitOrder(systemTag))
}

@Test
fun `task clinicVisitOrder null when clinic-visit-order code invalid`() {
val systemTag = "https://d-tree.org"
val task =
Task().apply {
meta.addTag(
Coding().apply {
system = systemTag
code = "clinic-visit-task-order-NAN"
}
)
}
Assert.assertNull(task.clinicVisitOrder(systemTag))
}

@Test
fun `task clinicVisitOrder when clinic-visit-order code is double`() {
val systemTag = "https://d-tree.org"
val task =
Task().apply {
meta.addTag(
Coding().apply {
system = systemTag
code = "clinic-visit-task-order-34.2"
}
)
}
Assert.assertEquals(34.2, task.clinicVisitOrder(systemTag))
}

@Test
fun `task clinicVisitOrder with when clinic-visit-order code mixed - and _`() {
val systemTag = "https://d-tree.org"
val task =
Task().apply {
meta.addTag(
Coding().apply {
system = systemTag
code = "CLINIC_VISIT-TASK-ORDER_48.1"
}
)
}
Assert.assertEquals(48.1, task.clinicVisitOrder(systemTag))
}

@Test
fun `task isNotCompleted`() {
Assert.assertTrue(testTask1.isNotCompleted())
Expand Down