Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch task states from from Task Resource #68

Merged
merged 4 commits into from
Jun 17, 2024
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 @@ -31,6 +31,7 @@ import javax.inject.Provider
import javax.inject.Singleton
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import org.hl7.fhir.r4.model.CarePlan
import org.hl7.fhir.r4.model.CodeType
import org.hl7.fhir.r4.model.CodeableConcept
import org.hl7.fhir.r4.model.Coding
Expand All @@ -42,6 +43,7 @@ import org.hl7.fhir.r4.model.Reference
import org.hl7.fhir.r4.model.RelatedPerson
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.Task
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
import org.smartregister.fhircore.engine.data.domain.Guardian
import org.smartregister.fhircore.engine.data.domain.PregnancyStatus
Expand Down Expand Up @@ -74,6 +76,7 @@ import org.smartregister.fhircore.engine.util.extension.getResourcesByIds
import org.smartregister.fhircore.engine.util.extension.givenName
import org.smartregister.fhircore.engine.util.extension.loadResource
import org.smartregister.fhircore.engine.util.extension.shouldShowOnProfile
import org.smartregister.fhircore.engine.util.extension.taskStatusToCarePlanActivityStatus
import org.smartregister.fhircore.engine.util.extension.toAgeDisplay
import org.smartregister.fhircore.engine.util.extension.yearsPassed
import timber.log.Timber
Expand Down Expand Up @@ -243,12 +246,7 @@ constructor(
showIdentifierInProfile = true,
currentCarePlan = carePlan,
healthStatus = patient.extractHealthStatusFromMeta(patientTypeMetaTagCodingSystem),
tasks =
carePlan
?.activity
?.filter { it.shouldShowOnProfile() }
?.sortedWith(compareBy(nullsLast()) { it?.detail?.code?.text?.toBigIntegerOrNull() })
?: listOf(),
tasks = fetchCarePlanActivities(carePlan),
conditions = defaultRepository.activePatientConditions(patient.logicalId),
otherPatients = patient.otherChildren(),
guardians = patient.guardians(),
Expand Down Expand Up @@ -490,6 +488,37 @@ constructor(
.filterNot { it.healthStatus == HealthStatus.DEFAULT }
}

private suspend fun fetchCarePlanActivities(
carePlan: CarePlan?
): List<CarePlan.CarePlanActivityComponent> {
if (carePlan == null) return emptyList()
val activityOnList = mutableMapOf<String, CarePlan.CarePlanActivityComponent>()
val tasksToFetch = mutableListOf<String>()
for (planActivity in carePlan.activity) {
if (!planActivity.shouldShowOnProfile()) {
continue
}
val taskId = planActivity.outcomeReference.firstOrNull()?.extractId()
if (taskId != null) {
tasksToFetch.add(taskId)
activityOnList[taskId] = planActivity
}
}
if (tasksToFetch.isNotEmpty()) {
val tasks = fhirEngine.getResourcesByIds<Task>(tasksToFetch)
tasks.forEach { task ->
val planActivity: CarePlan.CarePlanActivityComponent? = activityOnList[task.logicalId]
if (planActivity != null) {
planActivity.detail?.status = task.taskStatusToCarePlanActivityStatus()
activityOnList[task.logicalId] = planActivity
}
}
}
return activityOnList.values.sortedWith(
compareBy(nullsLast()) { it.detail?.code?.text?.toBigIntegerOrNull() },
)
}

object ResourceValue {
const val BLANK = ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.hl7.fhir.r4.utils.StructureMapUtilities
import org.smartregister.fhircore.engine.data.local.DefaultRepository
import org.smartregister.fhircore.engine.util.extension.encodeResourceToString
import org.smartregister.fhircore.engine.util.extension.getCarePlanId
import org.smartregister.fhircore.engine.util.extension.taskStatusToCarePlanActivityStatus
import org.smartregister.fhircore.engine.util.helper.TransformSupportServices
import timber.log.Timber

Expand Down Expand Up @@ -148,7 +149,7 @@ constructor(val fhirEngine: FhirEngine, val transformSupportServices: TransformS
for ((index, value) in carePlan.activity.withIndex()) {
val outcome = value.outcomeReference.find { x -> x.reference.contains(id) }
if (outcome != null) {
value.detail.status = taskStatusToCarePlanActivityStatus(task.status)
value.detail.status = task.taskStatusToCarePlanActivityStatus()
carePlan.activity?.set(index, value)
break
}
Expand Down Expand Up @@ -176,18 +177,4 @@ constructor(val fhirEngine: FhirEngine, val transformSupportServices: TransformS
else -> Task.TaskStatus.COMPLETED
}
}

private fun taskStatusToCarePlanActivityStatus(
status: Task.TaskStatus,
): CarePlan.CarePlanActivityStatus {
return when (status) {
Task.TaskStatus.FAILED -> CarePlan.CarePlanActivityStatus.STOPPED
Task.TaskStatus.CANCELLED -> CarePlan.CarePlanActivityStatus.CANCELLED
Task.TaskStatus.COMPLETED,
Task.TaskStatus.ONHOLD,
Task.TaskStatus.INPROGRESS,
Task.TaskStatus.ENTEREDINERROR, -> CarePlan.CarePlanActivityStatus.fromCode(status.toCode())
else -> CarePlan.CarePlanActivityStatus.NULL
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.smartregister.fhircore.engine.util.extension

import org.hl7.fhir.r4.model.CarePlan
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Task
import org.smartregister.fhircore.engine.util.DateUtils
Expand Down Expand Up @@ -71,3 +72,16 @@ fun Task.getCarePlanId(): String? {
?.code
?.substringAfterLast(delimiter = '/', missingDelimiterValue = "")
}

fun Task.taskStatusToCarePlanActivityStatus(): CarePlan.CarePlanActivityStatus {
return when (status) {
Task.TaskStatus.FAILED -> CarePlan.CarePlanActivityStatus.STOPPED
Task.TaskStatus.CANCELLED -> CarePlan.CarePlanActivityStatus.CANCELLED
Task.TaskStatus.READY -> CarePlan.CarePlanActivityStatus.NOTSTARTED
Task.TaskStatus.COMPLETED,
Task.TaskStatus.ONHOLD,
Task.TaskStatus.INPROGRESS,
Task.TaskStatus.ENTEREDINERROR, -> CarePlan.CarePlanActivityStatus.fromCode(status.toCode())
else -> CarePlan.CarePlanActivityStatus.NULL
}
}