From 9749d148c891dc1e4584b5be5c11ecbf441e73a0 Mon Sep 17 00:00:00 2001 From: Martin Ndegwa Date: Tue, 20 Feb 2024 18:53:05 +0300 Subject: [PATCH] =?UTF-8?q?Fixes=20config=20resources=20flagged=20for=20up?= =?UTF-8?q?sync=20=F0=9F=90=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configuration/ConfigurationRegistry.kt | 25 ++++++++----------- .../ConfigurationRegistryTest.kt | 14 ++++++----- 2 files changed, 18 insertions(+), 21 deletions(-) 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 390b8a6aa9..2d9c8daffb 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 @@ -24,7 +24,6 @@ import com.google.android.fhir.FhirEngine import com.google.android.fhir.db.ResourceNotFoundException import com.google.android.fhir.get import com.google.android.fhir.knowledge.KnowledgeManager -import com.google.android.fhir.logicalId import dagger.hilt.android.qualifiers.ApplicationContext import java.io.File import java.io.FileNotFoundException @@ -76,7 +75,6 @@ import org.smartregister.fhircore.engine.util.extension.referenceValue import org.smartregister.fhircore.engine.util.extension.retrieveCompositionSections import org.smartregister.fhircore.engine.util.extension.searchCompositionByIdentifier import org.smartregister.fhircore.engine.util.extension.tryDecodeJson -import org.smartregister.fhircore.engine.util.extension.updateFrom import org.smartregister.fhircore.engine.util.extension.updateLastUpdated import org.smartregister.fhircore.engine.util.helper.LocalizationHelper import retrofit2.HttpException @@ -552,22 +550,17 @@ constructor( } /** - * Using this [FhirEngine] and [DispatcherProvider], update this stored resources with the passed - * resource, or create it if not found. + * Update this stored resources with the passed resource, or create it if not found. If the + * resource is a Metadata Resource save it in the Knowledge Manager + * + * Note */ suspend fun addOrUpdate(resource: R) { withContext(dispatcherProvider.io()) { - resource.updateLastUpdated() try { - fhirEngine.get(resource.resourceType, resource.logicalId).run { - fhirEngine.update(updateFrom(resource)) - } - } catch (resourceNotFoundException: ResourceNotFoundException) { - try { - createRemote(resource) - } catch (sqlException: SQLException) { - Timber.e(sqlException) - } + createOrUpdateRemote(resource) + } catch (sqlException: SQLException) { + Timber.e(sqlException) } /** @@ -610,9 +603,11 @@ constructor( * Using this [FhirEngine] and [DispatcherProvider], for all passed resources, make sure they all * have IDs or generate if they don't, then pass them to create. * + * Note: The backing db API for fhirEngine.create(..,isLocalOnly) performs an UPSERT + * * @param resources vararg of resources */ - suspend fun createRemote(vararg resources: Resource) { + suspend fun createOrUpdateRemote(vararg resources: Resource) { return withContext(dispatcherProvider.io()) { resources.onEach { it.updateLastUpdated() 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 30b2fb6b64..87add326da 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 @@ -44,7 +44,6 @@ import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent import org.hl7.fhir.r4.model.Composition import org.hl7.fhir.r4.model.Composition.SectionComponent import org.hl7.fhir.r4.model.Enumerations -import org.hl7.fhir.r4.model.Group import org.hl7.fhir.r4.model.Identifier import org.hl7.fhir.r4.model.ListResource import org.hl7.fhir.r4.model.Reference @@ -282,7 +281,6 @@ class ConfigurationRegistryTest : RobolectricTest() { val requestPathArgumentSlot = mutableListOf() - coVerify(exactly = 1) { fhirEngine.get(any(), any()) } coVerify(exactly = 1) { fhirEngine.create(capture(requestPathArgumentSlot)) } Assert.assertEquals("composition-id-1", requestPathArgumentSlot.first().id) Assert.assertEquals(ResourceType.Composition, requestPathArgumentSlot.first().resourceType) @@ -350,8 +348,6 @@ class ConfigurationRegistryTest : RobolectricTest() { coEvery { fhirResourceDataSource.getResource("$focusReference?_id=$focusReference") } returns bundle - coEvery { fhirEngine.update(any()) } returns Unit - coEvery { fhirEngine.get(ResourceType.List, testListId) } returns listResource coEvery { fhirResourceDataSource.getResource("$resourceKey?_id=$resourceId&_count=200") } returns bundle @@ -364,7 +360,13 @@ class ConfigurationRegistryTest : RobolectricTest() { configRegistry.setNonProxy(true) configRegistry.fetchNonWorkflowConfigResources() - coVerify { fhirEngine.get(ResourceType.List, testListId) } + val createdResourceArgumentSlot = mutableListOf() + + coVerify { configRegistry.createOrUpdateRemote(capture(createdResourceArgumentSlot)) } + Assert.assertEquals( + "test-list-id", + createdResourceArgumentSlot.filterIsInstance().first().id + ) coVerify { fhirResourceDataSource.getResource("$resourceKey?_id=$resourceId&_count=200") } coEvery { fhirResourceDataSource.getResource("$focusReference?_id=$focusReference") } } @@ -416,7 +418,7 @@ class ConfigurationRegistryTest : RobolectricTest() { coEvery { fhirEngine.create(patient, isLocalOnly = true) } returns listOf(patient.id) runTest { - configRegistry.createRemote(patient) + configRegistry.createOrUpdateRemote(patient) coVerify { fhirEngine.create(patient, isLocalOnly = true) } } }