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

Support interpolation for conditionalFhirPathExpression to filter resources #3538

Open
wants to merge 1 commit into
base: fix-loading-nested-list-data
Choose a base branch
from
Open
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 @@ -20,6 +20,7 @@ import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.runtime.snapshots.SnapshotStateMap
import com.google.android.fhir.datacapture.extensions.logicalId
import javax.inject.Inject
import org.hl7.fhir.r4.model.Resource
import org.jeasy.rules.api.Facts
import org.smartregister.fhircore.engine.configuration.view.ListProperties
Expand All @@ -29,7 +30,7 @@ import org.smartregister.fhircore.engine.domain.model.ResourceData
import org.smartregister.fhircore.engine.domain.model.RuleConfig
import org.smartregister.fhircore.engine.domain.model.ViewType
import org.smartregister.fhircore.engine.util.extension.extractLogicalIdUuid
import javax.inject.Inject
import org.smartregister.fhircore.engine.util.extension.interpolate

/**
* This class is used to fire rules used to extract and manipulate data from FHIR resources.
Expand Down Expand Up @@ -73,7 +74,7 @@ class ResourceDataRulesExecutor @Inject constructor(val rulesFactory: RulesFacto
listResourceDataStateMap: SnapshotStateMap<String, SnapshotStateList<ResourceData>>,
) {
listProperties.resources.forEach { listResource ->
filteredListResources(relatedResourcesMap, listResource)
filteredListResources(relatedResourcesMap, listResource, computedValuesMap)
.mapToResourceData(
listResourceConfig = listResource,
relatedResourcesMap = relatedResourcesMap,
Expand Down Expand Up @@ -133,11 +134,14 @@ class ResourceDataRulesExecutor @Inject constructor(val rulesFactory: RulesFacto
relatedResourcesMap = relatedResourcesMap,
)
}

val interpolatedConditionalFhirPathExpression =
relatedListResourceConfig.conditionalFhirPathExpression?.interpolate(computedValuesMap)

rulesFactory.rulesEngineService
.filterResources(
resources = retrievedRelatedResources,
conditionalFhirPathExpression =
relatedListResourceConfig.conditionalFhirPathExpression,
conditionalFhirPathExpression = interpolatedConditionalFhirPathExpression,
)
.also { filteredResources ->
// Add to queue for processing
Expand Down Expand Up @@ -198,14 +202,17 @@ class ResourceDataRulesExecutor @Inject constructor(val rulesFactory: RulesFacto
private fun filteredListResources(
relatedResourceMap: Map<String, List<Resource>>,
listResource: ListResourceConfig,
computedValuesMap: Map<String, Any>,
): List<Resource> {
val relatedResourceKey = listResource.relatedResourceId ?: listResource.resourceType.name
val interpolatedConditionalFhirPathExpression =
listResource.conditionalFhirPathExpression?.interpolate(computedValuesMap)

// Filter by condition derived from fhirPathExpression otherwise return original or empty list
val resources =
rulesFactory.rulesEngineService.filterResources(
resources = relatedResourceMap[relatedResourceKey],
conditionalFhirPathExpression = listResource.conditionalFhirPathExpression,
conditionalFhirPathExpression = interpolatedConditionalFhirPathExpression,
)

// Sort resources if valid sort configuration is provided
Expand Down