From bc87133aeb194ac60b91fb49da5bdbefecceca03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=E2=89=A1ZRS?= <12814349+LZRS@users.noreply.github.com> Date: Wed, 20 Mar 2024 09:46:30 +0300 Subject: [PATCH] Save remote resources as non local changes(#49) * Save config resources as remote resources To avoid being marked as LocalChange * Exclude resources from practitionerDetails endpoint Not to be marked as local change --- .../engine/data/local/DefaultRepository.kt | 7 ++++ .../ui/appsetting/AppSettingViewModel.kt | 25 +++++++------- .../engine/ui/login/LoginViewModel.kt | 33 +++++++++++-------- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/DefaultRepository.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/DefaultRepository.kt index 498634cd54..9bb9393458 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/DefaultRepository.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/data/local/DefaultRepository.kt @@ -179,6 +179,13 @@ constructor( } } + suspend fun saveRemote(vararg resource: Resource) { + return withContext(dispatcherProvider.io()) { + resource.forEach { it.generateMissingId() } + fhirEngine.createRemote(*resource) + } + } + suspend fun create(addResourceTags: Boolean = true, vararg resource: Resource): List { return withContext(dispatcherProvider.io()) { resource.onEach { diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/appsetting/AppSettingViewModel.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/appsetting/AppSettingViewModel.kt index f1b3376b9f..4eb433aeea 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/appsetting/AppSettingViewModel.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/appsetting/AppSettingViewModel.kt @@ -111,20 +111,21 @@ constructor( } val composition = data.resource as Composition - defaultRepository.save(composition) - - composition.section - .groupBy { it.focus.reference.split("/")[0] } - .entries - .filter { it.key == ResourceType.Binary.name || it.key == ResourceType.Parameters.name } - .forEach { entry: Map.Entry> -> - val ids = entry.value.joinToString(",") { it.focus.extractId() } - val rPath = entry.key + "?${Composition.SP_RES_ID}=$ids" - fhirResourceDataSource.loadData(rPath).entry.forEach { - defaultRepository.save(it.resource) + + val compositionBinaries = + composition.section + .groupBy { it.focus.reference.split("/")[0] } + .entries + .filter { it.key == ResourceType.Binary.name || it.key == ResourceType.Parameters.name } + .flatMap { entry: Map.Entry> -> + val ids = entry.value.joinToString(",") { it.focus.extractId() } + val rPath = entry.key + "?${Composition.SP_RES_ID}=$ids" + fhirResourceDataSource.loadData(rPath).entry.map { it.resource } } - } + .toTypedArray() + val remoteResources = compositionBinaries + arrayOf(composition) + defaultRepository.saveRemote(*remoteResources) loadConfigurations(context) _showProgressBar.postValue(false) } catch (unknownHostException: UnknownHostException) { diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/login/LoginViewModel.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/login/LoginViewModel.kt index 4c8fad99be..165a04dd89 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/login/LoginViewModel.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/ui/login/LoginViewModel.kt @@ -28,6 +28,7 @@ import javax.inject.Inject import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import org.hl7.fhir.r4.model.Bundle +import org.hl7.fhir.r4.model.Resource import org.hl7.fhir.r4.model.ResourceType import org.jetbrains.annotations.TestOnly import org.smartregister.fhircore.engine.auth.AccountAuthenticator @@ -248,20 +249,24 @@ constructor( val locations = practitionerDetails.fhirPractitionerDetails?.locations ?: listOf() val locationHierarchies = practitionerDetails.fhirPractitionerDetails?.locationHierarchyList ?: listOf() - - val careTeamIds = - defaultRepository.create(false, *careTeams.toTypedArray()).map { it.extractLogicalIdUuid() } - sharedPreferences.write(ResourceType.CareTeam.name, careTeamIds) - - val organizationIds = - defaultRepository.create(false, *organizations.toTypedArray()).map { - it.extractLogicalIdUuid() - } - sharedPreferences.write(ResourceType.Organization.name, organizationIds) - - val locationIds = - defaultRepository.create(false, *locations.toTypedArray()).map { it.extractLogicalIdUuid() } - sharedPreferences.write(ResourceType.Location.name, locationIds) + val groups = practitionerDetails.fhirPractitionerDetails?.groups ?: emptyList() + val practitionerRoles = + practitionerDetails.fhirPractitionerDetails?.practitionerRoles ?: emptyList() + val practitioners = practitionerDetails.fhirPractitionerDetails?.practitioners ?: emptyList() + + val remoteResources = + careTeams.toTypedArray() + + organizations.toTypedArray() + + locations.toTypedArray() + + groups.toTypedArray() + + practitionerRoles.toTypedArray() + + practitioners.toTypedArray() + defaultRepository.saveRemote(*remoteResources) + with(sharedPreferences) { + write(ResourceType.CareTeam.name, careTeams.map { it.id.extractLogicalIdUuid() }) + write(ResourceType.Organization.name, organizations.map { it.id.extractLogicalIdUuid() }) + write(ResourceType.Location.name, locations.map { it.id.extractLogicalIdUuid() }) + } sharedPreferences.write( SharedPreferenceKey.PRACTITIONER_LOCATION_HIERARCHIES.name,