Skip to content

Commit

Permalink
Merge branch 'med-req-update' of github.com:opensrp/fhircore into g6p…
Browse files Browse the repository at this point in the history
…d_release_28Jan
  • Loading branch information
shoaibmushtaq25 committed Jan 28, 2022
2 parents 0b1a422 + cd63586 commit 29048fa
Show file tree
Hide file tree
Showing 15 changed files with 838 additions and 550 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,13 @@ class LibraryEvaluator @Inject constructor() {
helpers,
Bundle(),
// TODO check and handle when data bundle has multiple Patient resources
createBundle(listOfNotNull(patient, *data.entry.map { it.resource }.toTypedArray()))
createBundle(
listOfNotNull(
patient,
*data.entry.map { it.resource }.toTypedArray(),
*repository.search(library.dataRequirementFirstRep).toTypedArray()
)
)
)

val result =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@

package org.smartregister.fhircore.engine.data.local

import ca.uhn.fhir.rest.gclient.TokenClientParam
import com.google.android.fhir.FhirEngine
import com.google.android.fhir.db.ResourceNotFoundException
import com.google.android.fhir.logicalId
import com.google.android.fhir.search.search
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.withContext
import org.hl7.fhir.r4.model.Condition
import org.hl7.fhir.r4.model.DataRequirement
import org.hl7.fhir.r4.model.Enumerations
import org.hl7.fhir.r4.model.Immunization
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse
Expand Down Expand Up @@ -64,6 +68,18 @@ constructor(open val fhirEngine: FhirEngine, open val dispatcherProvider: Dispat
}
}

suspend fun search(dataRequirement: DataRequirement) =
when (dataRequirement.type) {
Enumerations.ResourceType.CONDITION.toCode() ->
fhirEngine.search<Condition> {
dataRequirement.codeFilter.forEach {
filter(TokenClientParam(it.path), { value = of(it.codeFirstRep) })
}
// TODO handle date filter
}
else -> listOf()
}

suspend fun save(resource: Resource) {
return withContext(dispatcherProvider.io()) {
resource.generateMissingId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import org.smartregister.fhircore.engine.ui.base.AlertDialogue.showConfirmAlert
import org.smartregister.fhircore.engine.ui.base.AlertDialogue.showProgressAlert
import org.smartregister.fhircore.engine.ui.base.BaseMultiLanguageActivity
import org.smartregister.fhircore.engine.util.DispatcherProvider
import org.smartregister.fhircore.engine.util.extension.FieldType
import org.smartregister.fhircore.engine.util.extension.find
import org.smartregister.fhircore.engine.util.extension.prepareQuestionsForReadingOrEditing
import org.smartregister.fhircore.engine.util.extension.showToast
Expand Down Expand Up @@ -310,12 +311,6 @@ open class QuestionnaireActivity : BaseMultiLanguageActivity(), View.OnClickList
open fun populateInitialValues(questionnaire: Questionnaire) = Unit

open fun postSaveSuccessful(questionnaireResponse: QuestionnaireResponse) {
setResult(
Activity.RESULT_OK,
Intent().apply {
putExtra(QUESTIONNAIRE_RESPONSE, parser.encodeResourceToString(questionnaireResponse))
}
)
val message = questionnaireViewModel.extractionProgressMessage.value
if (message?.isNotBlank() == true)
AlertDialogue.showInfoAlert(
Expand All @@ -324,10 +319,24 @@ open class QuestionnaireActivity : BaseMultiLanguageActivity(), View.OnClickList
getString(R.string.done),
{
it.dismiss()
finish()
finishActivity(questionnaireResponse)
}
)
else finish()
else finishActivity(questionnaireResponse)
}

fun finishActivity(questionnaireResponse: QuestionnaireResponse) {
val parcelResponse = questionnaireResponse.copy()
questionnaire.find(FieldType.TYPE, Questionnaire.QuestionnaireItemType.ATTACHMENT.name)
.forEach { parcelResponse.find(it.linkId)?.answer?.clear() }
setResult(
Activity.RESULT_OK,
Intent().apply {
putExtra(QUESTIONNAIRE_RESPONSE, parser.encodeResourceToString(parcelResponse))
putExtra(QUESTIONNAIRE_ARG_FORM, questionnaire.logicalId)
}
)
finish()
}

fun deepFlat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,9 @@ fun Questionnaire.cqfLibraryIds() =
}

fun Questionnaire.find(linkId: String): Questionnaire.QuestionnaireItemComponent? {
return item.find(linkId, null)
}

private fun List<Questionnaire.QuestionnaireItemComponent>.find(
linkId: String,
default: Questionnaire.QuestionnaireItemComponent?
): Questionnaire.QuestionnaireItemComponent? {
var result = default
run loop@{
forEach {
if (it.linkId == linkId) {
result = it
return@loop
} else if (it.item.isNotEmpty()) {
result = it.item.find(linkId, result)
}
}
}

return result
val result = mutableListOf<Questionnaire.QuestionnaireItemComponent>()
item.find(FieldType.LINK_ID, linkId, result)
return result.firstOrNull()
}

fun QuestionnaireResponse.find(
Expand All @@ -59,7 +42,7 @@ fun QuestionnaireResponse.find(
return item.find(linkId, null)
}

private fun List<QuestionnaireResponse.QuestionnaireResponseItemComponent>.find(
fun List<QuestionnaireResponse.QuestionnaireResponseItemComponent>.find(
linkId: String,
default: QuestionnaireResponse.QuestionnaireResponseItemComponent?
): QuestionnaireResponse.QuestionnaireResponseItemComponent? {
Expand All @@ -79,3 +62,46 @@ private fun List<QuestionnaireResponse.QuestionnaireResponseItemComponent>.find(

return result
}

enum class FieldType {
DEFINITION,
LINK_ID,
TYPE
}

fun Questionnaire.find(
fieldType: FieldType,
value: String
): List<Questionnaire.QuestionnaireItemComponent> {
val result = mutableListOf<Questionnaire.QuestionnaireItemComponent>()
item.find(fieldType, value, result)
return result
}

fun List<Questionnaire.QuestionnaireItemComponent>.find(
fieldType: FieldType,
value: String,
target: MutableList<Questionnaire.QuestionnaireItemComponent>
) {
forEach {
when (fieldType) {
FieldType.DEFINITION -> {
if (it.definition?.contentEquals(value, true) == true) {
target.add(it)
}
}
FieldType.LINK_ID -> {
if (it.linkId == value) {
target.add(it)
}
}
FieldType.TYPE -> {
if (it.type == Questionnaire.QuestionnaireItemType.valueOf(value)) target.add(it)
}
}

if (it.item.isNotEmpty()) {
it.item.find(fieldType, value, target)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.hl7.fhir.r4.model.CodeableConcept
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Expression
import org.hl7.fhir.r4.model.Extension
import org.hl7.fhir.r4.model.HumanName
import org.hl7.fhir.r4.model.Patient
import org.hl7.fhir.r4.model.PrimitiveType
import org.hl7.fhir.r4.model.Quantity
Expand Down Expand Up @@ -54,6 +55,7 @@ fun Base?.valueToString(): String {
this.repeat.let {
it.period.toPlainString().plus(" ").plus(it.periodUnit.display.capitalize()).plus(" (s)")
}
else if (this is HumanName) "${this.given.firstOrNull().valueToString()} ${this.family}"
else this.toString()
}

Expand Down
10 changes: 10 additions & 0 deletions android/quest/src/main/assets/configurations/app/sync_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
"type": "token",
"expression": "organization"
},
{
"resourceType": "SearchParameter",
"name": "organization",
"code": "subject.organization",
"base": [
"MedicationRequest"
],
"type": "token",
"expression": "organization"
},
{
"resourceType": "SearchParameter",
"name": "organization",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"properties": {
"labelDirection": "UP",
"value": {
"textSize": 45
"textSize": 35
}
}
}
Expand Down Expand Up @@ -130,7 +130,27 @@
"missing": "N/A"
},
"value": {
"textSize": 45
"textSize": 35
}
}
}
]
},
{
"filters": [
{
"resourceType": "MEDICATIONREQUEST",
"key": "reasonCode",
"displayableProperty": "reasonCode",
"valueType": "CODEABLECONCEPT",
"label": "Findings: ",
"properties": {
"valueFormatter": {
"Minor": "Underage",
"missing": "Missing reason for recommendation"
},
"value": {
"textSize": 16
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ data class Filter(
val valuePostfix: String? = null,
val label: String? = null,
val valueType: Enumerations.DataType,
val valueCoding: Code?,
val valueCoding: Code? = null,
val valueString: String? = null,
val dynamicColors: List<DynamicColor>? = null,
val properties: Properties? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fun SimpleDetailsScreen(dataProvider: SimpleDetailsDataProvider) {
}
)

Column(modifier = Modifier.padding(20.dp).testTag(DETAILS_DATA_ROWS)) {
Column(modifier = Modifier.padding(5.dp).testTag(DETAILS_DATA_ROWS)) {
dataItem?.rows?.forEachIndexed { i, r ->
kotlin
.runCatching {
Expand All @@ -105,7 +105,7 @@ fun SimpleDetailsScreen(dataProvider: SimpleDetailsDataProvider) {
if (r.cells.size == 0) {
Divider(
color = colorResource(id = R.color.white_smoke),
modifier = Modifier.padding(15.dp)
modifier = Modifier.padding(2.dp)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.StringType
import org.hl7.fhir.r4.utils.FHIRPathEngine
import org.jetbrains.annotations.VisibleForTesting
import org.smartregister.fhircore.engine.util.extension.extractId
import org.smartregister.fhircore.engine.util.extension.referenceValue
import org.smartregister.fhircore.quest.configuration.view.DetailViewConfiguration
import org.smartregister.fhircore.quest.configuration.view.Filter
Expand Down Expand Up @@ -111,6 +112,7 @@ class SimpleDetailsViewModel @Inject constructor(val patientRepository: PatientR
encounter: Encounter
): MutableMap<Enumerations.ResourceType, List<Resource>> {
return mutableMapOf(
Enumerations.ResourceType.PATIENT to listOf(getPatient(encounter)),
Enumerations.ResourceType.CONDITION to getCondition(encounter, null),
Enumerations.ResourceType.OBSERVATION to getObservation(encounter, null),
Enumerations.ResourceType.MEDICATIONREQUEST to getMedicationRequest(encounter, null)
Expand All @@ -120,8 +122,7 @@ class SimpleDetailsViewModel @Inject constructor(val patientRepository: PatientR
fun Base.getPathValue(path: String) = fhirPathEngine.evaluate(this, path).firstOrNull()

fun doesSatisfyFilter(resource: Resource, filter: Filter): Boolean? {
if (filter.valueCoding == null && filter.valueString == null)
throw IllegalStateException("Filter must have either of one valueCoding or valueString")
if (filter.valueCoding == null && filter.valueString == null) return true

// get property mentioned as filter and match value
// e.g. category: CodeableConcept in Condition
Expand All @@ -144,6 +145,9 @@ class SimpleDetailsViewModel @Inject constructor(val patientRepository: PatientR
}
}

suspend fun getPatient(encounter: Encounter) =
patientRepository.fetchDemographics(encounter.subject.extractId())

suspend fun getCondition(encounter: Encounter, filter: Filter?) =
getSearchResults<Condition>(
encounter.referenceValue(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class CqlContentTest : RobolectricTest() {
coEvery { fhirEngine.load(Library::class.java, fhirModelLibrary.logicalId) } returns
fhirModelLibrary
coEvery { defaultRepository.save(any()) } just runs
coEvery { defaultRepository.search(any()) } returns listOf()

val result = runBlocking {
evaluator.runCqlLibrary(
Expand Down Expand Up @@ -127,6 +128,7 @@ class CqlContentTest : RobolectricTest() {
coEvery { fhirEngine.load(Library::class.java, fhirModelLibrary.logicalId) } returns
fhirModelLibrary
coEvery { defaultRepository.save(any()) } just runs
coEvery { defaultRepository.search(any()) } returns listOf()

val result = runBlocking {
evaluator.runCqlLibrary(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import org.hl7.fhir.r4.model.Encounter
import org.hl7.fhir.r4.model.Enumerations
import org.hl7.fhir.r4.model.MedicationRequest
import org.hl7.fhir.r4.model.Observation
import org.hl7.fhir.r4.model.Patient
import org.hl7.fhir.r4.model.Reference
import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.StringType
import org.junit.Assert
Expand Down Expand Up @@ -106,8 +108,14 @@ class SimpleDetailsViewModelTest : RobolectricTest() {
this.intent = MedicationRequest.MedicationRequestIntent.FILLERORDER
}
)
coEvery { viewModel.getPatient(any()) } returns Patient()

viewModel.getDataMap(Encounter().apply { id = "123" })
viewModel.getDataMap(
Encounter().apply {
id = "123"
subject = Reference().apply { reference = "Encounter/123" }
}
)

coVerify { viewModel.getCondition(any(), any()) }
coVerify { viewModel.getObservation(any(), any()) }
Expand Down
Loading

0 comments on commit 29048fa

Please sign in to comment.