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

Exclude questionnaire items from population #3460

Merged
merged 9 commits into from
Sep 19, 2024
Prev Previous commit
Next Next commit
Handle exclusion of nested QuestionnaireResponse items
Signed-off-by: Elly Kitoto <junkmailstoelly@gmail.com>
  • Loading branch information
ellykits committed Sep 4, 2024
commit 4997fb7909ff9b916ac689e79c43ab5099d46a1b
Original file line number Diff line number Diff line change
Expand Up @@ -1120,22 +1120,48 @@
}

// Exclude the configured fields from QR
val prepopulationExclusionLinkIdsMap =
questionnaireConfig.linkIds
?.asSequence()
?.filter { it.type == LinkIdType.PREPOPULATION_EXCLUSION }
?.associateBy { it.linkId } ?: emptyMap()
questionnaireResponse?.apply {
item = item.filterNot { prepopulationExclusionLinkIdsMap.containsKey(it.linkId) }
if (questionnaireResponse != null) {
val exclusionLinkIdsMap: Map<String, Boolean> =
questionnaireConfig.linkIds

Check warning on line 1125 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt#L1124-L1125

Added lines #L1124 - L1125 were not covered by tests
?.asSequence()
?.filter { it.type == LinkIdType.PREPOPULATION_EXCLUSION }
?.associateBy { it.linkId }
?.mapValues { it.value.type == LinkIdType.PREPOPULATION_EXCLUSION } ?: emptyMap()

questionnaireResponse.item =
excludePrepopulationFields(questionnaireResponse.item.toMutableList(), exclusionLinkIdsMap)

Check warning on line 1132 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt#L1131-L1132

Added lines #L1131 - L1132 were not covered by tests
}
return Pair(questionnaireResponse, launchContextResources)
}

fun excludePrepopulationFields(
items: MutableList<QuestionnaireResponseItemComponent>,
exclusionMap: Map<String, Boolean>,
): MutableList<QuestionnaireResponseItemComponent> {
val stack = LinkedList<MutableList<QuestionnaireResponseItemComponent>>()
stack.push(items)

Check warning on line 1142 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt#L1141-L1142

Added lines #L1141 - L1142 were not covered by tests
while (stack.isNotEmpty()) {
val currentItems = stack.pop()
val iterator = currentItems.iterator()

Check warning on line 1145 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt#L1144-L1145

Added lines #L1144 - L1145 were not covered by tests
while (iterator.hasNext()) {
val item = iterator.next()

Check warning on line 1147 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt#L1147

Added line #L1147 was not covered by tests
if (exclusionMap.containsKey(item.linkId)) {
iterator.remove()

Check warning on line 1149 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt#L1149

Added line #L1149 was not covered by tests
} else if (item.item.isNotEmpty()) {
stack.push(item.item)

Check warning on line 1151 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt#L1151

Added line #L1151 was not covered by tests
}
}
}
return items

Check warning on line 1155 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt#L1155

Added line #L1155 was not covered by tests
}

private fun List<QuestionnaireResponseItemComponent>.removeUnAnsweredItems():
List<QuestionnaireResponseItemComponent> {
return this.filter { it.hasAnswer() || it.item.isNotEmpty() }
return this.asSequence()

Check warning on line 1160 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt#L1160

Added line #L1160 was not covered by tests
.filter { it.hasAnswer() || it.item.isNotEmpty() }
.onEach { it.item = it.item.removeUnAnsweredItems() }
.filter { it.hasAnswer() || it.item.isNotEmpty() }
.toList()

Check warning on line 1164 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt#L1164

Added line #L1164 was not covered by tests
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2032,4 +2032,44 @@ class QuestionnaireViewModelTest : RobolectricTest() {
Assert.assertNotNull(result.first)
Assert.assertTrue(result.first!!.find("linkid-1") == null)
}

@Test
fun testExcludeNestedItemFromQuestionnairePrepopulation() {
val item1 = QuestionnaireResponse.QuestionnaireResponseItemComponent().apply { linkId = "1" }
val item2 = QuestionnaireResponse.QuestionnaireResponseItemComponent().apply { linkId = "2" }
val item3 =
QuestionnaireResponse.QuestionnaireResponseItemComponent().apply {
linkId = "3"
item =
mutableListOf(
QuestionnaireResponse.QuestionnaireResponseItemComponent().apply { linkId = "3.1" },
QuestionnaireResponse.QuestionnaireResponseItemComponent().apply {
linkId = "3.2"
item =
mutableListOf(
QuestionnaireResponse.QuestionnaireResponseItemComponent().apply {
linkId = "3.2.1"
},
QuestionnaireResponse.QuestionnaireResponseItemComponent().apply {
linkId = "3.2.2"
},
)
},
)
}

val items = mutableListOf(item1, item2, item3)
val exclusionMap = mapOf("2" to true, "3.1" to true, "3.2.2" to true)
val filteredItems = questionnaireViewModel.excludePrepopulationFields(items, exclusionMap)
Assert.assertEquals(2, filteredItems.size)
Assert.assertEquals("1", filteredItems.first().linkId)
val itemThree = filteredItems.last()
Assert.assertEquals("3", itemThree.linkId)
Assert.assertEquals(1, itemThree.item.size)
val itemThreePointTwo = itemThree.item.first()
Assert.assertEquals("3.2", itemThreePointTwo.linkId)
Assert.assertEquals(1, itemThreePointTwo.item.size)
val itemThreePointTwoOne = itemThreePointTwo.item.first()
Assert.assertEquals("3.2.1", itemThreePointTwoOne.linkId)
}
}
Loading