Skip to content

Commit

Permalink
Implement event management to close custom resources via the backgron…
Browse files Browse the repository at this point in the history
…d Worker (#3483)

* Implement event management to close custom resources via the background worker.

Signed-off-by: Lentumunai-Mark <[email protected]>

* "Start writing test fro closing custom resources"

Signed-off-by: Lentumunai-Mark <[email protected]>

* Test closure of resources without a base resource.

Signed-off-by: Lentumunai-Mark <[email protected]>

* Run spotless Apply.

Signed-off-by: Lentumunai-Mark <[email protected]>

* Update android/engine/src/main/java/org/smartregister/fhircore/engine/task/FhirResourceExpireWorker.kt

Co-authored-by: Elly Kitoto <[email protected]>

* Update android/engine/src/main/java/org/smartregister/fhircore/engine/task/FhirResourceUtil.kt

Co-authored-by: Elly Kitoto <[email protected]>

* Replace old fucntion name with new  function name.

Signed-off-by: Lentumunai-Mark <[email protected]>

---------

Signed-off-by: Lentumunai-Mark <[email protected]>
Co-authored-by: Benjamin Mwalimu <[email protected]>
Co-authored-by: Elly Kitoto <[email protected]>
  • Loading branch information
3 people authored Sep 12, 2024
1 parent 4d5248e commit 2601cd0
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 3 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
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 2601cd0

Please sign in to comment.