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 @@ -18,6 +18,7 @@ package org.smartregister.fhircore.engine.task

import com.google.android.fhir.FhirEngine
import com.google.android.fhir.get
import com.google.android.fhir.search.search
import java.util.Date
import javax.inject.Inject
import javax.inject.Singleton
Expand Down Expand Up @@ -125,12 +126,31 @@ constructor(val fhirEngine: FhirEngine, val transformSupportServices: TransformS

suspend fun completeTask(id: String, encounterStatus: EncounterStatus?) {
fhirEngine.run {
create(
val task =
get<Task>(id).apply {
this.status = encounterStatusToTaskStatus(encounterStatus)
this.lastModified = Date()
}
)
create(task)
if (task.status == Task.TaskStatus.COMPLETED) {
val carePlans =
search<CarePlan> { filter(CarePlan.SUBJECT, { value = task.`for`.reference }) }
var carePlanToUpdate: CarePlan? = null
carePlans.forEach { carePlan ->
for ((index, value) in carePlan.activity.withIndex()) {
val outcome = value.outcomeReference.find { x -> x.reference.contains(id) }
if (outcome != null) {
carePlanToUpdate = carePlan.copy()
carePlanToUpdate?.activity?.set(
index,
value.apply { detail.status = CarePlan.CarePlanActivityStatus.COMPLETED }
)
break
}
}
}
carePlanToUpdate?.let { update(it) }
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import org.hl7.fhir.r4.model.Encounter
import org.hl7.fhir.r4.model.Group
import org.hl7.fhir.r4.model.Patient
import org.hl7.fhir.r4.model.PlanDefinition
import org.hl7.fhir.r4.model.Reference
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.StructureMap
Expand Down Expand Up @@ -244,7 +245,7 @@ class FhirCarePlanGeneratorTest : RobolectricTest() {
@Test
fun testCompleteTaskNoEncounter() = runTest {
coEvery { fhirEngine.create(any()) } answers { listOf() }

coEvery { fhirEngine.search<CarePlan>(any()) } answers { listOf() }
coEvery { fhirEngine.get<Task>("12345") } returns Task().apply { id = "12345" }

runBlocking {
Expand All @@ -258,7 +259,7 @@ class FhirCarePlanGeneratorTest : RobolectricTest() {
@Test
fun testCompleteTaskWithEncounter() = runTest {
coEvery { fhirEngine.create(any()) } answers { listOf() }

coEvery { fhirEngine.search<CarePlan>(any()) } answers { listOf() }
coEvery { fhirEngine.get<Task>("12345") } returns Task().apply { id = "12345" }

runBlocking {
Expand All @@ -272,7 +273,7 @@ class FhirCarePlanGeneratorTest : RobolectricTest() {
@Test
fun testCompleteTaskWithEncounter_TaskStatusFromCode() = runTest {
coEvery { fhirEngine.create(any()) } answers { listOf() }

coEvery { fhirEngine.search<CarePlan>(any()) } answers { listOf() }
coEvery { fhirEngine.get<Task>("12345") } returns Task().apply { id = "12345" }

runBlocking {
Expand All @@ -282,4 +283,34 @@ class FhirCarePlanGeneratorTest : RobolectricTest() {
Assert.assertEquals(Task.TaskStatus.INPROGRESS, task.status)
}
}

@Test
fun testCompleteTaskCompleteCarePlan() = runTest {
val carePlan =
CarePlan().apply {
activity =
listOf(
CarePlan.CarePlanActivityComponent().let { activity ->
activity.outcomeReference = listOf(Reference("12345"))
activity
}
)
}
coEvery { fhirEngine.create(any()) } answers { listOf() }
coEvery { fhirEngine.update(any()) } answers {}
coEvery { fhirEngine.get<CarePlan>(any()) } returns carePlan
coEvery { fhirEngine.search<CarePlan>(any()) } returns listOf(carePlan)
coEvery { fhirEngine.get<Task>("12345") } returns Task().apply { id = "12345" }

runBlocking {
fhirCarePlanGenerator.completeTask("12345", Encounter.EncounterStatus.FINISHED)

val updatedCarePlan = fhirEngine.get<CarePlan>("12345")
Assert.assertNotNull(
updatedCarePlan.activity.find { x ->
x.outcomeReference.find { y -> y.reference == "12345" } != null
}
)
}
}
}