Skip to content

Commit

Permalink
Merge branch 'ak/workflow-md' of github.com:aditya-07/android-fhir in…
Browse files Browse the repository at this point in the history
…to ak/workflow-md
  • Loading branch information
aditya-07 committed Nov 25, 2024
2 parents 07b7b8b + 99013c8 commit 509eb8c
Showing 1 changed file with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.google.android.fhir.workflow.activity

import androidx.annotation.WorkerThread
import ca.uhn.fhir.model.api.IQueryParameterType
import ca.uhn.fhir.rest.param.ReferenceParam
import com.google.android.fhir.workflow.activity.phase.Phase
import com.google.android.fhir.workflow.activity.phase.Phase.PhaseName
import com.google.android.fhir.workflow.activity.phase.Phase.PhaseName.ORDER
Expand All @@ -34,6 +36,11 @@ import com.google.android.fhir.workflow.activity.resource.request.CPGCommunicati
import com.google.android.fhir.workflow.activity.resource.request.CPGMedicationRequest
import com.google.android.fhir.workflow.activity.resource.request.CPGRequestResource
import com.google.android.fhir.workflow.activity.resource.request.Intent
import org.hl7.fhir.r4.model.Bundle
import org.hl7.fhir.r4.model.CommunicationRequest
import org.hl7.fhir.r4.model.MedicationRequest
import org.hl7.fhir.r4.model.ServiceRequest
import org.hl7.fhir.r4.model.Task
import org.opencds.cqf.fhir.api.Repository

/**
Expand Down Expand Up @@ -303,5 +310,107 @@ private constructor(
resource: CPGOrderMedicationEvent<*>,
): ActivityFlow<CPGMedicationRequest, CPGOrderMedicationEvent<*>> =
ActivityFlow(repository, null, resource)

/**
* This returns a list of flows and because of type erasure, its not possible to do a
* filterInstance with specific CPG resource types. Instead, use list.filter and check the cpg
* type of the [ActivityFlow.requestResource] instead.
*/
fun of(
repository: Repository,
patientId: String,
): List<ActivityFlow<CPGRequestResource<*>, CPGEventResource<*>>> {
val tasks =
repository
.search(
Bundle::class.java,
Task::class.java,
mutableMapOf<String, MutableList<IQueryParameterType>>(
Task.SUBJECT.paramName to mutableListOf(ReferenceParam("Patient/$patientId")),
),
null,
)
.entry
.map { it.resource }
val medicationRequests =
repository
.search(
Bundle::class.java,
MedicationRequest::class.java,
mutableMapOf<String, MutableList<IQueryParameterType>>(
MedicationRequest.SUBJECT.paramName to
mutableListOf(ReferenceParam("Patient/$patientId")),
),
null,
)
.entry
.map { it.resource }
val communicationRequests =
repository
.search(
Bundle::class.java,
CommunicationRequest::class.java,
mutableMapOf<String, MutableList<IQueryParameterType>>(
CommunicationRequest.SUBJECT.paramName to
mutableListOf(ReferenceParam("Patient/$patientId")),
),
null,
)
.entry
.map { it.resource }

val serviceRequests =
repository
.search(
Bundle::class.java,
ServiceRequest::class.java,
mutableMapOf<String, MutableList<IQueryParameterType>>(
ServiceRequest.SUBJECT.paramName to
mutableListOf(ReferenceParam("Patient/$patientId")),
),
null,
)
.entry
.map { it.resource }

val cache: MutableMap<String, CPGRequestResource<*>> =
sequenceOf(tasks, communicationRequests, serviceRequests, medicationRequests)
.flatten()
.map { CPGRequestResource.of(it) }
.associateByTo(LinkedHashMap()) { "${it.resourceType}/${it.logicalId}" }
fun addBasedOn(
request: RequestChain<CPGRequestResource<*>>,
): RequestChain<CPGRequestResource<*>>? {
request.request.getBasedOn()?.let { reference ->
cache[reference.reference]?.let {
cache.remove(reference.reference)
request.basedOn = RequestChain(it, addBasedOn(RequestChain(it, null)))
request.basedOn
}
}
return null
}
val requestChain =
cache.values
.filter {
it.getIntent() == Intent.ORDER ||
it.getIntent() == Intent.PLAN ||
it.getIntent() == Intent.PROPOSAL
}
.sortedByDescending { it.getIntent().code }
.mapNotNull {
if (cache.containsKey("${it.resourceType}/${it.logicalId}")) {
RequestChain(it, addBasedOn(RequestChain(it, null)))
} else {
null
}
}
return requestChain.map { ActivityFlow(repository, it.request) }
}
}
}

internal data class RequestChain<R : CPGRequestResource<*>>(
val request: CPGRequestResource<*>,
var basedOn: RequestChain<R>?,
)

0 comments on commit 509eb8c

Please sign in to comment.