Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement event management to close custom resources via the backgrond Worker #3483

Merged
merged 8 commits into from
Sep 12, 2024
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 @@
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,

Check warning on line 264 in android/engine/src/main/java/org/smartregister/fhircore/engine/task/FhirResourceUtil.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/task/FhirResourceUtil.kt#L261-L264

Added lines #L261 - L264 were not covered by tests
)
}
}
}
}
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
Loading