Skip to content

Commit

Permalink
Merge branch 'main' into fix-register-filters
Browse files Browse the repository at this point in the history
  • Loading branch information
dubdabasoduba authored Sep 12, 2024
2 parents 960176c + 2601cd0 commit 06d81a9
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ constructor(
*/
suspend fun updateResourcesRecursively(
resourceConfig: ResourceConfig,
subject: Resource,
subject: Resource? = null,
eventWorkflow: EventWorkflow,
) {
withContext(dispatcherProvider.io()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ constructor(

override suspend fun doWork(): Result {
return withContext(dispatcherProvider.io()) {
fhirResourceUtil.expireOverdueTasks()
fhirResourceUtil.closeResourcesRelatedToCompletedServiceRequests()
fhirResourceUtil.run {
expireOverdueTasks()
closeResourcesRelatedToCompletedServiceRequests()
closeFhirResources()
}
Result.success()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,22 @@ constructor(
closeRelatedResources(serviceRequest)
}
}

suspend fun closeFhirResources() {
val appRegistry =
configurationRegistry.retrieveConfiguration<ApplicationConfiguration>(
ConfigType.Application,
)

appRegistry.eventWorkflows
.filter { it.eventType == EventType.RESOURCE_CLOSURE }
.forEach { eventWorkFlow ->
eventWorkFlow.eventResources.forEach { eventResource ->
defaultRepository.updateResourcesRecursively(
resourceConfig = eventResource,
eventWorkflow = eventWorkFlow,
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Condition
import org.hl7.fhir.r4.model.ContactPoint
import org.hl7.fhir.r4.model.DateTimeType
import org.hl7.fhir.r4.model.Encounter
import org.hl7.fhir.r4.model.Enumerations
import org.hl7.fhir.r4.model.Group
import org.hl7.fhir.r4.model.HumanName
import org.hl7.fhir.r4.model.Location
import org.hl7.fhir.r4.model.Organization
import org.hl7.fhir.r4.model.Patient
import org.hl7.fhir.r4.model.Period
import org.hl7.fhir.r4.model.Procedure
import org.hl7.fhir.r4.model.Reference
import org.hl7.fhir.r4.model.RelatedPerson
Expand All @@ -80,6 +82,7 @@ import org.smartregister.fhircore.engine.app.fakes.Faker
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
import org.smartregister.fhircore.engine.configuration.UniqueIdAssignmentConfig
import org.smartregister.fhircore.engine.configuration.app.ConfigService
import org.smartregister.fhircore.engine.configuration.event.EventTriggerCondition
import org.smartregister.fhircore.engine.configuration.event.EventWorkflow
import org.smartregister.fhircore.engine.configuration.event.UpdateWorkflowValueConfig
import org.smartregister.fhircore.engine.configuration.profile.ManagingEntityConfig
Expand Down Expand Up @@ -887,6 +890,90 @@ class DefaultRepositoryTest : RobolectricTest() {
coVerify(exactly = 0) { fhirEngine.update(any()) }
}

@Test
fun testNonCareplanRelatedResourcesUpdatedCorrectlyAndWithNoSpecifiedBaseResource() = runTest {
val encounter =
Encounter().apply {
id = "test-Encounter"
period = Period().apply { start = Date().plusDays(-2) }
status = Encounter.EncounterStatus.INPROGRESS
type =
listOf(
CodeableConcept().apply {
coding =
listOf(
Coding().apply {
system = "http://smartregister.org/"
code = "SVISIT"
display = "Service Point Visit"
},
)
text = "Service Point Visit"
},
)
}
val eventWorkflow =
EventWorkflow(
triggerConditions =
listOf(
EventTriggerCondition(
eventResourceId = "encounterToBeClosed",
matchAll = false,
conditionalFhirPathExpressions =
listOf(
"true",
),
),
),
eventResources =
listOf(
ResourceConfig(
id = "encounterToBeClosed",
resource = ResourceType.Encounter,
configRules = listOf(),
dataQueries =
listOf(
DataQuery(
paramName = "reason-code",
filterCriteria =
listOf(
FilterCriterionConfig.TokenFilterCriterionConfig(
dataType = Enumerations.DataType.CODEABLECONCEPT,
value = Code(system = "http://smartregister.org/", code = "SVISIT"),
),
),
),
),
),
),
updateValues =
listOf(
UpdateWorkflowValueConfig(
jsonPathExpression = "Encounter.status",
value = JsonPrimitive("finished"),
resourceType = ResourceType.Encounter,
),
),
resourceFilterExpressions =
listOf(
ResourceFilterExpression(
conditionalFhirPathExpressions = listOf("Encounter.period.end < now()"),
matchAll = true,
),
),
)
fhirEngine.create(encounter)
defaultRepository.updateResourcesRecursively(
resourceConfig = eventWorkflow.eventResources[0],
eventWorkflow = eventWorkflow,
)
val resourceSlot = slot<Encounter>()
val captured = resourceSlot.captured
coVerify { fhirEngine.update(capture(resourceSlot)) }
Assert.assertEquals("test-Encounter", captured.id)
Assert.assertEquals(Encounter.EncounterStatus.FINISHED, captured.status)
}

@Test
fun testUpdateResourcesRecursivelyClosesResource() = runTest {
val patient =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import org.smartregister.fhircore.engine.domain.model.Code
import org.smartregister.fhircore.engine.domain.model.DataQuery
import org.smartregister.fhircore.engine.domain.model.FhirResourceConfig
import org.smartregister.fhircore.engine.domain.model.FilterCriterionConfig
import org.smartregister.fhircore.engine.domain.model.NestedSearchConfig
import org.smartregister.fhircore.engine.domain.model.ResourceConfig
import org.smartregister.fhircore.engine.domain.model.ResourceData
import org.smartregister.fhircore.engine.domain.model.SnackBarMessageConfig
Expand Down Expand Up @@ -244,15 +245,19 @@ constructor(
baseResource.copy(
dataQueries = newBaseResourceDataQueries ?: baseResource.dataQueries,
nestedSearchResources =
baseResourceRegisterFilterField?.nestedSearchResources?.map { nestedSearchConfig ->
nestedSearchConfig.copy(
dataQueries =
createQueriesForRegisterFilter(
dataQueries = nestedSearchConfig.dataQueries,
qrItemMap = qrItemMap,
),
getValidatedNestedSearchResources(
baseResourceRegisterFilterField?.nestedSearchResources,
qrItemMap,
)
} ?: baseResource.nestedSearchResources,
?.map { nestedSearchConfig ->
nestedSearchConfig.copy(
dataQueries =
createQueriesForRegisterFilter(
dataQueries = nestedSearchConfig.dataQueries,
qrItemMap = qrItemMap,
),
)
} ?: baseResource.nestedSearchResources,
),
relatedResources = newRelatedResources,
)
Expand All @@ -264,6 +269,19 @@ constructor(
Timber.i("New ResourceConfig for register data filter: ${fhirResourceConfig.encodeJson()}")
}

private fun getValidatedNestedSearchResources(
nestedSearchResources: List<NestedSearchConfig>?,
qrItemMap: Map<String, QuestionnaireResponse.QuestionnaireResponseItemComponent>,
) =
nestedSearchResources?.filter { nestedSearchConfig ->
nestedSearchConfig.dataQueries?.any { dataQuery ->
dataQuery.filterCriteria.any { filterCriterionConfig ->
filterCriterionConfig.dataFilterLinkId.isNullOrEmpty() ||
qrItemMap[filterCriterionConfig.dataFilterLinkId]?.answer?.isNotEmpty() == true
}
} ?: false
}

private fun createFilterRelatedResources(
registerDataFilterFieldsMap: Map<String, RegisterFilterField>?,
relatedResources: List<ResourceConfig>,
Expand Down
48 changes: 48 additions & 0 deletions android/quest/src/test/assets/configs/app/application_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,54 @@
"QuestionnaireResponse"
]
},
"eventWorkflows": [
{
"eventType": "RESOURCE_CLOSURE",
"triggerConditions": [
{
"eventResourceId": "encounterToBeClosed",
"matchAll": false,
"conditionalFhirPathExpressions": [
"true"
]
}
],
"eventResources": [
{
"id": "encounterToBeClosed",
"resource": "Encounter",
"configRules": [],
"dataQueries": [
{
"paramName": "reason-code",
"filterCriteria": [
{
"dataType": "CODEABLECONCEPT",
"value": {
"system": "http://smartregister.org/",
"code": "SVISIT"
}
}
]
}
]
}
],
"updateValues": [
{
"jsonPathExpression": "Encounter.status",
"value": "finished",
"resourceType": "Encounter"
}
],
"resourceFilterExpression": {
"conditionalFhirPathExpressions": [
"Encounter.period.end < now()"
],
"matchAll": true
}
}
],
"logGpsLocation": [
"QUESTIONNAIRE"
]
Expand Down

0 comments on commit 06d81a9

Please sign in to comment.