From b58fc3ec04afdb99cf5a165481504c3588b0b20b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=E2=89=A1ZRS?= <12814349+LZRS@users.noreply.github.com> Date: Thu, 9 May 2024 15:50:56 +0300 Subject: [PATCH] Apply patch changes from https://github.com/opensrp/fhircore/pull/3218 --- .../util/extension/ResourceExtension.kt | 25 +++++++++++++++++++ .../engine/robolectric/RobolectricTest.kt | 4 +-- .../util/extension/ResourceExtensionTest.kt | 13 ++++++++++ .../questionnaire/QuestionnaireViewModel.kt | 5 ++++ .../QuestionnaireViewModelTest.kt | 4 +-- 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/ResourceExtension.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/ResourceExtension.kt index f6f55fc3c5..6ddcb2752e 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/ResourceExtension.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/ResourceExtension.kt @@ -224,6 +224,31 @@ fun List.prepareQuestionsForReadingOrE } } +/** + * Set all questions that are not of type [Questionnaire.QuestionnaireItemType.GROUP] to readOnly if + * [readOnlyLinkIds] item are there while editing the form. This also generates the correct FHIRPath + * population expression for each question when mapped to the corresponding [QuestionnaireResponse] + */ +fun List.prepareQuestionsForEditing( + path: String = "QuestionnaireResponse.item", + readOnlyLinkIds: List? = emptyList(), +) { + forEach { item -> + if (item.type != Questionnaire.QuestionnaireItemType.GROUP) { + item.readOnly = readOnlyLinkIds?.contains(item.linkId) == true + item.item.prepareQuestionsForEditing( + "$path.where(linkId = '${item.linkId}').answer.item", + readOnlyLinkIds, + ) + } else { + item.item.prepareQuestionsForEditing( + "$path.where(linkId = '${item.linkId}').item", + readOnlyLinkIds, + ) + } + } +} + /** Delete resources in [QuestionnaireResponse.contained] from the database */ suspend fun QuestionnaireResponse.deleteRelatedResources(defaultRepository: DefaultRepository) { contained.forEach { defaultRepository.delete(it) } diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/robolectric/RobolectricTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/robolectric/RobolectricTest.kt index 366488e29a..a18b5ec614 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/robolectric/RobolectricTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/robolectric/RobolectricTest.kt @@ -45,8 +45,8 @@ abstract class RobolectricTest { val latch = CountDownLatch(1) val observer: Observer = object : Observer { - override fun onChanged(o: T) { - data[0] = o + override fun onChanged(value: T) { + data[0] = value latch.countDown() liveData.removeObserver(this) } diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/util/extension/ResourceExtensionTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/util/extension/ResourceExtensionTest.kt index 3f5e0683c9..d5a1032d2f 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/util/extension/ResourceExtensionTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/util/extension/ResourceExtensionTest.kt @@ -895,4 +895,17 @@ class ResourceExtensionTest : RobolectricTest() { patient.appendOrganizationInfo(listOf("Organization/12345")) Assert.assertEquals("Organization/12345", patient.managingOrganization.reference) } + + @Test + fun `prepareQuestionsForEditing should set readOnly correctly when readOnlyLinkIds passed`() { + val questionnaire = Questionnaire() + questionnaire.item.add(Questionnaire.QuestionnaireItemComponent().apply { linkId = "1" }) + questionnaire.item.add(Questionnaire.QuestionnaireItemComponent().apply { linkId = "2" }) + questionnaire.item.add(Questionnaire.QuestionnaireItemComponent().apply { linkId = "3" }) + questionnaire.item.prepareQuestionsForEditing("", readOnlyLinkIds = listOf("1", "3")) + + Assert.assertTrue(questionnaire.item[0].readOnly) + Assert.assertFalse(questionnaire.item[1].readOnly) + Assert.assertTrue(questionnaire.item[2].readOnly) + } } diff --git a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt index 49feab9c40..5abd9385c5 100644 --- a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt +++ b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt @@ -91,6 +91,7 @@ import org.smartregister.fhircore.engine.util.extension.find import org.smartregister.fhircore.engine.util.extension.generateMissingId import org.smartregister.fhircore.engine.util.extension.isIn import org.smartregister.fhircore.engine.util.extension.prePopulateInitialValues +import org.smartregister.fhircore.engine.util.extension.prepareQuestionsForEditing import org.smartregister.fhircore.engine.util.extension.prepareQuestionsForReadingOrEditing import org.smartregister.fhircore.engine.util.extension.showToast import org.smartregister.fhircore.engine.util.extension.updateLastUpdated @@ -168,6 +169,10 @@ constructor( ) } + if (questionnaireConfig.isEditable()) { + item.prepareQuestionsForEditing(readOnlyLinkIds = questionnaireConfig.readOnlyLinkIds) + } + // Pre-populate questionnaire items with configured values allActionParameters ?.filter { (it.paramType == ActionParameterType.PREPOPULATE && it.value.isNotEmpty()) } diff --git a/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModelTest.kt b/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModelTest.kt index 8018185529..41a1dc9e85 100644 --- a/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModelTest.kt +++ b/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModelTest.kt @@ -1311,7 +1311,7 @@ class QuestionnaireViewModelTest : RobolectricTest() { questionnaireConfig.copy( resourceType = ResourceType.Patient, resourceIdentifier = patient.logicalId, - type = QuestionnaireType.EDIT.name + type = QuestionnaireType.EDIT.name, ) val questionnaireWithInitialValue = @@ -1394,7 +1394,7 @@ class QuestionnaireViewModelTest : RobolectricTest() { questionnaireViewModel.populateQuestionnaire( questionnaireWithInitialValue, questionnaireConfig1, - emptyList() + emptyList(), ) Assert.assertNotNull(result.first) Assert.assertTrue(result.first!!.find("linkid-1") == null)