diff --git a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/register/RegisterViewModel.kt b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/register/RegisterViewModel.kt index 9491ac2f61..d07d28d85f 100644 --- a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/register/RegisterViewModel.kt +++ b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/register/RegisterViewModel.kt @@ -52,6 +52,7 @@ import org.smartregister.fhircore.engine.domain.model.Code import org.smartregister.fhircore.engine.domain.model.DataQuery import org.smartregister.fhircore.engine.domain.model.FhirResourceConfig import org.smartregister.fhircore.engine.domain.model.FilterCriterionConfig +import org.smartregister.fhircore.engine.domain.model.NestedSearchConfig import org.smartregister.fhircore.engine.domain.model.ResourceConfig import org.smartregister.fhircore.engine.domain.model.ResourceData import org.smartregister.fhircore.engine.domain.model.SnackBarMessageConfig @@ -244,15 +245,19 @@ constructor( baseResource.copy( dataQueries = newBaseResourceDataQueries ?: baseResource.dataQueries, nestedSearchResources = - baseResourceRegisterFilterField?.nestedSearchResources?.map { nestedSearchConfig -> - nestedSearchConfig.copy( - dataQueries = - createQueriesForRegisterFilter( - dataQueries = nestedSearchConfig.dataQueries, - qrItemMap = qrItemMap, - ), + getValidatedNestedSearchResources( + baseResourceRegisterFilterField?.nestedSearchResources, + qrItemMap, ) - } ?: baseResource.nestedSearchResources, + ?.map { nestedSearchConfig -> + nestedSearchConfig.copy( + dataQueries = + createQueriesForRegisterFilter( + dataQueries = nestedSearchConfig.dataQueries, + qrItemMap = qrItemMap, + ), + ) + } ?: baseResource.nestedSearchResources, ), relatedResources = newRelatedResources, ) @@ -264,6 +269,19 @@ constructor( Timber.i("New ResourceConfig for register data filter: ${fhirResourceConfig.encodeJson()}") } + private fun getValidatedNestedSearchResources( + nestedSearchResources: List?, + qrItemMap: Map, + ) = + nestedSearchResources?.filter { nestedSearchConfig -> + nestedSearchConfig.dataQueries?.any { dataQuery -> + dataQuery.filterCriteria.any { filterCriterionConfig -> + filterCriterionConfig.dataFilterLinkId.isNullOrEmpty() || + qrItemMap[filterCriterionConfig.dataFilterLinkId]?.answer?.isNotEmpty() == true + } + } ?: false + } + private fun createFilterRelatedResources( registerDataFilterFieldsMap: Map?, relatedResources: List,