Skip to content

Commit

Permalink
Fixes config resources flagged for upsync 🐛
Browse files Browse the repository at this point in the history
  • Loading branch information
ndegwamartin committed Feb 20, 2024
1 parent 39b2ed9 commit 9749d14
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 <R : Resource> 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)
}

/**
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -282,7 +281,6 @@ class ConfigurationRegistryTest : RobolectricTest() {

val requestPathArgumentSlot = mutableListOf<Resource>()

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)
Expand Down Expand Up @@ -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
Expand All @@ -364,7 +360,13 @@ class ConfigurationRegistryTest : RobolectricTest() {
configRegistry.setNonProxy(true)
configRegistry.fetchNonWorkflowConfigResources()

coVerify { fhirEngine.get(ResourceType.List, testListId) }
val createdResourceArgumentSlot = mutableListOf<Resource>()

coVerify { configRegistry.createOrUpdateRemote(capture(createdResourceArgumentSlot)) }
Assert.assertEquals(
"test-list-id",
createdResourceArgumentSlot.filterIsInstance<ListResource>().first().id
)
coVerify { fhirResourceDataSource.getResource("$resourceKey?_id=$resourceId&_count=200") }
coEvery { fhirResourceDataSource.getResource("$focusReference?_id=$focusReference") }
}
Expand Down Expand Up @@ -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) }
}
}
Expand Down

0 comments on commit 9749d14

Please sign in to comment.