Skip to content

Commit

Permalink
Apply patch changes from #3218
Browse files Browse the repository at this point in the history
  • Loading branch information
LZRS committed May 9, 2024
1 parent 79c5acb commit b58fc3e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,31 @@ fun List<Questionnaire.QuestionnaireItemComponent>.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<Questionnaire.QuestionnaireItemComponent>.prepareQuestionsForEditing(
path: String = "QuestionnaireResponse.item",
readOnlyLinkIds: List<String>? = emptyList(),

Check warning on line 234 in android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/ResourceExtension.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/ResourceExtension.kt#L232-L234

Added lines #L232 - L234 were not covered by tests
) {
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,

Check warning on line 246 in android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/ResourceExtension.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/util/extension/ResourceExtension.kt#L244-L246

Added lines #L244 - L246 were not covered by tests
)
}
}
}

/** Delete resources in [QuestionnaireResponse.contained] from the database */
suspend fun QuestionnaireResponse.deleteRelatedResources(defaultRepository: DefaultRepository) {
contained.forEach { defaultRepository.delete(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ abstract class RobolectricTest {
val latch = CountDownLatch(1)
val observer: Observer<T> =
object : Observer<T> {
override fun onChanged(o: T) {
data[0] = o
override fun onChanged(value: T) {
data[0] = value
latch.countDown()
liveData.removeObserver(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -168,6 +169,10 @@ constructor(
)
}

if (questionnaireConfig.isEditable()) {
item.prepareQuestionsForEditing(readOnlyLinkIds = questionnaireConfig.readOnlyLinkIds)

Check warning on line 173 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#L173

Added line #L173 was not covered by tests
}

// Pre-populate questionnaire items with configured values
allActionParameters
?.filter { (it.paramType == ActionParameterType.PREPOPULATE && it.value.isNotEmpty()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b58fc3e

Please sign in to comment.