diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt index 450916a755..8d53761fcb 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt @@ -37,7 +37,6 @@ import javax.inject.Singleton import kotlinx.coroutines.withContext import kotlinx.serialization.json.Json import okhttp3.RequestBody.Companion.toRequestBody -import org.hl7.fhir.r4.model.Reference import org.hl7.fhir.r4.model.Base import org.hl7.fhir.r4.model.Binary import org.hl7.fhir.r4.model.Bundle @@ -47,6 +46,7 @@ import org.hl7.fhir.r4.model.ImplementationGuide import org.hl7.fhir.r4.model.ListResource import org.hl7.fhir.r4.model.MetadataResource import org.hl7.fhir.r4.model.Parameters +import org.hl7.fhir.r4.model.Reference import org.hl7.fhir.r4.model.Resource import org.hl7.fhir.r4.model.ResourceType import org.hl7.fhir.r4.model.SearchParameter @@ -336,15 +336,30 @@ constructor( } } else { composition.retrieveCompositionSections().forEach { sectionComponent -> - val referenceResourceType = sectionComponent.focus.reference.substringBefore(TYPE_REFERENCE_DELIMITER) - if (sectionComponent.hasFocus() && sectionComponent.focus.hasReferenceElement() && sectionComponent.focus.hasIdentifier()) { + val referenceResourceType = + sectionComponent.focus.reference.substringBefore(TYPE_REFERENCE_DELIMITER) + if ( + sectionComponent.hasFocus() && + sectionComponent.focus.hasReferenceElement() && + sectionComponent.focus.hasIdentifier() + ) { val configIdentifier = sectionComponent.focus.identifier.value - addBinaryToConfigsJsonMap(referenceResourceType, configIdentifier, sectionComponent.focus, configsLoadedCallback) + addBinaryToConfigsJsonMap( + referenceResourceType, + configIdentifier, + sectionComponent.focus, + configsLoadedCallback, + ) } - if (sectionComponent.hasEntry() && sectionComponent.entry.isNotEmpty()){ - sectionComponent.entry.forEach{ entryReference -> + if (sectionComponent.hasEntry() && sectionComponent.entry.isNotEmpty()) { + sectionComponent.entry.forEach { entryReference -> val configIdentifier = entryReference.identifier.value - addBinaryToConfigsJsonMap(referenceResourceType, configIdentifier, entryReference, configsLoadedCallback) + addBinaryToConfigsJsonMap( + referenceResourceType, + configIdentifier, + entryReference, + configsLoadedCallback, + ) } } } @@ -356,7 +371,7 @@ constructor( referenceResourceType: String, configIdentifier: String, entryReference: Reference, - configsLoadedCallback: (Boolean) -> Unit + configsLoadedCallback: (Boolean) -> Unit, ) { if (isAppConfig(referenceResourceType) && !isIconConfig(configIdentifier)) { val extractedId = entryReference.identifier @@ -428,26 +443,27 @@ constructor( val compositionResource = fetchRemoteCompositionByAppId(parsedAppId) compositionResource?.let { composition -> val compositionSections = composition.retrieveCompositionSections() - val sectionComponentMap = mutableMapOf>() - compositionSections.forEach { sectionComponent -> - if ( sectionComponent.hasFocus() && sectionComponent.focus.hasReferenceElement()){ - val key = sectionComponent.focus.reference.substringBefore( - delimiter = TYPE_REFERENCE_DELIMITER, - missingDelimiterValue = "", - ) - sectionComponentMap.getOrPut(key){ mutableListOf() }.apply { add(sectionComponent) } - } - if ( sectionComponent.hasEntry() && sectionComponent.entry.isNotEmpty()){ - sectionComponent.entry.forEach { - val key = it.reference.substringBefore( - delimiter = TYPE_REFERENCE_DELIMITER, + val sectionComponentMap = mutableMapOf>() + compositionSections.forEach { sectionComponent -> + if (sectionComponent.hasFocus() && sectionComponent.focus.hasReferenceElement()) { + val key = + sectionComponent.focus.reference.substringBefore( + delimiter = TYPE_REFERENCE_DELIMITER, missingDelimiterValue = "", ) - sectionComponentMap.getOrPut(key){ mutableListOf() }.apply { add(sectionComponent) } - } + sectionComponentMap.getOrPut(key) { mutableListOf() }.apply { add(sectionComponent) } + } + if (sectionComponent.hasEntry() && sectionComponent.entry.isNotEmpty()) { + sectionComponent.entry.forEach { + val key = + it.reference.substringBefore( + delimiter = TYPE_REFERENCE_DELIMITER, + missingDelimiterValue = "", + ) + sectionComponentMap.getOrPut(key) { mutableListOf() }.apply { add(sectionComponent) } } - } + } processCompositionSectionComponent(sectionComponentMap) @@ -459,8 +475,11 @@ constructor( } } - private suspend fun processCompositionSectionComponent(sectionComponentMap: Map>){ - sectionComponentMap.filter { entry -> entry.key in FILTER_RESOURCE_LIST } + private suspend fun processCompositionSectionComponent( + sectionComponentMap: Map>, + ) { + sectionComponentMap + .filter { entry -> entry.key in FILTER_RESOURCE_LIST } .forEach { entry: Map.Entry> -> if (entry.key == ResourceType.List.name) { processCompositionListResources(entry) @@ -470,17 +489,15 @@ constructor( chunkedResourceIdList.forEach { sectionComponents -> Timber.d( "Fetching config resource ${entry.key}: with ids ${ - sectionComponents.joinToString( - ",", - ) - }", + sectionComponents.joinToString( + ",", + ) + }", ) fetchResources( resourceType = entry.key, resourceIdList = - sectionComponents.map { sectionComponent -> - sectionComponent.focus.extractId() - }, + sectionComponents.map { sectionComponent -> sectionComponent.focus.extractId() }, ) } } diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistryTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistryTest.kt index 29b230402c..723d142875 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistryTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistryTest.kt @@ -1085,10 +1085,11 @@ class ConfigurationRegistryTest : RobolectricTest() { @Test fun testFetchNonWorkflowConfigResourcesWithNoFocusOrEntry() = runTest { val appId = "app-id" - val composition = Composition().apply { - identifier = Identifier().apply { value = appId } - section = listOf(SectionComponent()) // Neither focus nor entry - } + val composition = + Composition().apply { + identifier = Identifier().apply { value = appId } + section = listOf(SectionComponent()) // Neither focus nor entry + } configRegistry.fetchNonWorkflowConfigResources() @@ -1098,9 +1099,7 @@ class ConfigurationRegistryTest : RobolectricTest() { @Test fun testPopulateConfigurationsMapWithNeitherFocusNorEntry() = runTest { - val composition = Composition().apply { - section = listOf(SectionComponent()) - } + val composition = Composition().apply { section = listOf(SectionComponent()) } configRegistry.populateConfigurationsMap(context, composition, false, "app-id") {} @@ -1110,23 +1109,24 @@ class ConfigurationRegistryTest : RobolectricTest() { @Test fun testFetchNonWorkflowConfigResourcesWithAllFocus() = runTest { val appId = "app-id" - val composition = Composition().apply { - identifier = Identifier().apply { value = appId } - section = listOf( - SectionComponent().apply { - focus = Reference().apply { - identifier = Identifier().apply { value = "focus-1" } - reference = "ResourceType/1" - } - } - ) - } + val composition = + Composition().apply { + identifier = Identifier().apply { value = appId } + section = + listOf( + SectionComponent().apply { + focus = + Reference().apply { + identifier = Identifier().apply { value = "focus-1" } + reference = "ResourceType/1" + } + }, + ) + } - coEvery { fhirResourceDataSource.getResource(any()) } returns Bundle().apply { - addEntry().resource = composition - } + coEvery { fhirResourceDataSource.getResource(any()) } returns + Bundle().apply { addEntry().resource = composition } configRegistry.fetchNonWorkflowConfigResources() - } }