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
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.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 @@
null
}

// Exclude the configured fields from QR
val prepopulationExclusionLinkIdsMap =
questionnaireConfig.linkIds

Check warning on line 1104 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#L1103-L1104

Added lines #L1103 - L1104 were not covered by tests
?.asSequence()
?.filter { it.type == LinkIdType.PREPOPULATION_EXCLUSION }
?.associateBy { it.linkId } ?: emptyMap()
questionnaireResponse?.apply {
item = item.filterNot { prepopulationExclusionLinkIdsMap.containsKey(it.linkId) }

Check warning on line 1109 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#L1109

Added line #L1109 was not covered by tests
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
Original file line number Diff line number Diff line change
Expand Up @@ -592,4 +592,20 @@ The QR code widget supports adding an arbitrary number of QR codes, implemented
]
}
```
The extension's implementation can be found [here](https://github.com/opensrp/fhircore/blob/main/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/sdc/qrCode/EditTextQrCodeViewHolderFactory.kt)
The extension's implementation can be found [here](https://github.com/opensrp/fhircore/blob/main/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/sdc/qrCode/EditTextQrCodeViewHolderFactory.kt)

## Excluding questionnaire fields from prepopulation

Use the `linkIds` property to provide linkIds for the Questionnaire fields that should not be pre-field with data during editing or when opening the questionnaire in a read only format.
The `LinkIdType` required for the exclusion to work is `PREPOPULATION_EXCLUSION`

Example:

```json
"linkIds": [
{
"linkId": "ad29c7bd-8041-427f-8e63-b066afe5b438-009",
"type": "PREPOPULATION_EXCLUSION"
}
]
```
Loading