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
Next Next commit
Exclude configured fields from form prepopulation
Signed-off-by: Elly Kitoto <junkmailstoelly@gmail.com>
  • Loading branch information
ellykits committed Aug 23, 2024
commit 7ecdafb61fc34dabf59117b6d76f41f3ab73114c
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,5 @@ enum class LinkIdType : Parcelable {
READ_ONLY,
BARCODE,
LOCATION,
IDENTIFIER,
PREPOPULATION_EXCLUSION,
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import org.smartregister.fhircore.engine.BuildConfig
import org.smartregister.fhircore.engine.configuration.ConfigType
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
import org.smartregister.fhircore.engine.configuration.GroupResourceConfig
import org.smartregister.fhircore.engine.configuration.LinkIdType
import org.smartregister.fhircore.engine.configuration.QuestionnaireConfig
import org.smartregister.fhircore.engine.configuration.app.ApplicationConfiguration
import org.smartregister.fhircore.engine.configuration.app.CodingSystemUsage
Expand Down Expand Up @@ -1098,6 +1099,15 @@ constructor(
null
}

// 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) }
ellykits marked this conversation as resolved.
Show resolved Hide resolved
}
return Pair(questionnaireResponse, launchContextResources)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ class QuestionnaireViewModelTest : RobolectricTest() {
@ExperimentalCoroutinesApi
fun setUp() {
hiltRule.inject()

// Write practitioner and organization to shared preferences
sharedPreferencesHelper.write(
SharedPreferenceKey.PRACTITIONER_ID.name,
Expand Down Expand Up @@ -1837,6 +1836,92 @@ class QuestionnaireViewModelTest : RobolectricTest() {
Assert.assertTrue(initialValueDate.isToday)
}

@Test
fun testThatPopulateQuestionnaireSetInitialDefaultValueButExcludesFieldFromResponse() =
runTest(timeout = 90.seconds) {
val thisQuestionnaireConfig =
questionnaireConfig.copy(
resourceType = ResourceType.Patient,
resourceIdentifier = patient.logicalId,
type = QuestionnaireType.EDIT.name,
linkIds =
listOf(
LinkIdConfig("dateToday", LinkIdType.PREPOPULATION_EXCLUSION),
),
)
val questionnaireViewModelInstance =
QuestionnaireViewModel(
defaultRepository = defaultRepository,
dispatcherProvider = defaultRepository.dispatcherProvider,
fhirCarePlanGenerator = fhirCarePlanGenerator,
resourceDataRulesExecutor = resourceDataRulesExecutor,
transformSupportServices = mockk(),
sharedPreferencesHelper = sharedPreferencesHelper,
fhirOperator = fhirOperator,
fhirValidatorProvider = fhirValidatorProvider,
fhirPathDataExtractor = fhirPathDataExtractor,
configurationRegistry = configurationRegistry,
)
val questionnaireWithDefaultDate =
Questionnaire().apply {
id = thisQuestionnaireConfig.id
addItem(
Questionnaire.QuestionnaireItemComponent().apply {
linkId = "dateToday"
type = Questionnaire.QuestionnaireItemType.DATE
addExtension(
Extension(
"http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-initialExpression",
Expression().apply {
language = "text/fhirpath"
expression = "today()"
},
),
)
},
)
}

val questionnaireResponse =
QuestionnaireResponse().apply {
addItem(
QuestionnaireResponse.QuestionnaireResponseItemComponent().apply {
linkId = "dateToday"
addAnswer(
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent().apply {
value = DateType(Date())
},
)
},
)
setQuestionnaire(
thisQuestionnaireConfig.id.asReference(ResourceType.Questionnaire).reference,
)
}

coEvery {
fhirEngine.get(
thisQuestionnaireConfig.resourceType!!,
thisQuestionnaireConfig.resourceIdentifier!!,
)
} returns patient

coEvery { fhirEngine.search<QuestionnaireResponse>(any<Search>()) } returns
listOf(
SearchResult(questionnaireResponse, included = null, revIncluded = null),
)

val (result, _) =
questionnaireViewModelInstance.populateQuestionnaire(
questionnaire = questionnaireWithDefaultDate,
questionnaireConfig = thisQuestionnaireConfig,
actionParameters = emptyList(),
)

Assert.assertNotNull(result?.item)
Assert.assertTrue(result!!.item.isEmpty())
}

@Test
fun testThatPopulateQuestionnaireReturnsQuestionnaireResponseWithUnAnsweredRemoved() = runTest {
val questionnaireViewModelInstance =
Expand Down