diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SharedPreferencesHelper.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SharedPreferencesHelper.kt index 7be62552a45..7d9035e53a4 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SharedPreferencesHelper.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SharedPreferencesHelper.kt @@ -27,7 +27,6 @@ import javax.inject.Singleton import kotlinx.serialization.SerializationException import org.smartregister.fhircore.engine.util.extension.decodeJson import org.smartregister.fhircore.engine.util.extension.encodeJson -import org.smartregister.model.practitioner.PractitionerDetails import timber.log.Timber @Singleton @@ -96,11 +95,7 @@ constructor(@ApplicationContext val context: Context, val gson: Gson) { with(prefs.edit()) { putString( key, - (if (encodeWithGson) gson.toJson(value) else value.encodeJson()).also { - if (value is PractitionerDetails) { - Timber.e(it) - } - }, + (if (encodeWithGson) gson.toJson(value) else value.encodeJson()), ) commit() } diff --git a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/login/LoginViewModel.kt b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/login/LoginViewModel.kt index 62f92b2b973..50aca93f2d8 100644 --- a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/login/LoginViewModel.kt +++ b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/login/LoginViewModel.kt @@ -51,6 +51,7 @@ import org.smartregister.fhircore.engine.util.SecureSharedPreference import org.smartregister.fhircore.engine.util.SharedPreferenceKey import org.smartregister.fhircore.engine.util.SharedPreferencesHelper import org.smartregister.fhircore.engine.util.clearPasswordInMemory +import org.smartregister.fhircore.engine.util.extension.encodeResourceToString import org.smartregister.fhircore.engine.util.extension.extractLogicalIdUuid import org.smartregister.fhircore.engine.util.extension.getActivity import org.smartregister.fhircore.engine.util.extension.isDeviceOnline @@ -139,53 +140,69 @@ constructor( } }, onFetchPractitioner = { bundleResult, userInfo -> - if (bundleResult.isSuccess) { - val bundle = bundleResult.getOrDefault(FhirR4ModelBundle()) - bundle.entry.map { it.resource }.filterIsInstance() - .forEach { - val existingCredentials = secureSharedPreference.retrieveCredentials() - val existPractitionerDetails = sharedPreferences.read( - key = SharedPreferenceKey.PRACTITIONER_DETAILS.name, - decodeWithGson = true - ) - - if (existingCredentials == null || existPractitionerDetails == null) { - savePractitionerDetails(it, userInfo) { - _showProgressBar.postValue(false) - updateNavigateHome(true) - } - return@forEach + if (bundleResult.isFailure){ + _showProgressBar.postValue(false) + Timber.e(bundleResult.exceptionOrNull()) + Timber.e(bundleResult.getOrNull().valueToString()) + _loginErrorState.postValue(LoginErrorState.ERROR_FETCHING_USER) + return@fetchToken + } + + val bundle = bundleResult.getOrDefault(FhirR4ModelBundle()) + bundle.entry.map { it.resource }.filterIsInstance() + .forEach { + val existingCredentials = secureSharedPreference.retrieveCredentials() + val practitionerExists = sharedPreferences.read(SharedPreferenceKey.PRACTITIONER_ID.name, null) + .isNullOrBlank() + .not() + + if (existingCredentials == null || !practitionerExists) { + savePractitionerDetails(it, userInfo) { + _showProgressBar.postValue(false) + updateNavigateHome(true) } + return@forEach + } + + val appSyncStrategies = applicationConfiguration.syncStrategy - val appSyncStrategies = applicationConfiguration.syncStrategy - var syncStrategyMatching = false - appSyncStrategies.forEach { syncStrategy -> - if (syncStrategy.equals("Location", ignoreCase = true)) { // may be use enums, in future - syncStrategyMatching = - existPractitionerDetails.fhirPractitionerDetails.locations.any { location -> - it.fhirPractitionerDetails.locations.contains(location) - } + val syncStrategy = appSyncStrategies.single() + val syncStrategyMatching = when(syncStrategy.lowercase()){ + "location" -> { + val currentLocations = sharedPreferences.read>(key = ResourceType.Location.name) ?: emptyList() + val userLocations = it.fhirPractitionerDetails.locations.map { location -> location.id.extractLogicalIdUuid() } + currentLocations.all { location -> + userLocations.contains(location) + } + } + "careteam" -> { + val currentCareTeams = sharedPreferences.read>(key = ResourceType.CareTeam.name) ?: emptyList() + val userCareTeams = it.fhirPractitionerDetails.careTeams.map { careTeam -> careTeam.id.extractLogicalIdUuid() } + currentCareTeams.all { careTeam -> + userCareTeams.contains(careTeam) } } - - if (syncStrategyMatching){ - savePractitionerDetails(it, userInfo) { - _showProgressBar.postValue(false) - updateNavigateHome(true) + "organization" -> { + val currentOrganizations = sharedPreferences.read>(key = ResourceType.Organization.name) ?: emptyList() + val userOrganizations = it.fhirPractitionerDetails.organizations.map { org -> org.id.extractLogicalIdUuid() } + currentOrganizations.all { org -> + userOrganizations.contains(org) } - }else{ - _showProgressBar.postValue(false) - // TODO: Post login error for second user when not matching sync strategy - _loginErrorState.postValue(LoginErrorState.ERROR_MATCHING_SYNC_STRATEGY) } + else -> throw NotImplementedError() + } + if (syncStrategyMatching){ + savePractitionerDetails(it, userInfo) { + _showProgressBar.postValue(false) + updateNavigateHome(true) + } + }else{ + _showProgressBar.postValue(false) + _loginErrorState.postValue(LoginErrorState.ERROR_MATCHING_SYNC_STRATEGY) } - } else { - _showProgressBar.postValue(false) - Timber.e(bundleResult.exceptionOrNull()) - Timber.e(bundleResult.getOrNull().valueToString()) - _loginErrorState.postValue(LoginErrorState.ERROR_FETCHING_USER) - } + + } }, ) } else { @@ -339,11 +356,6 @@ constructor( val locationHierarchies = practitionerDetails.fhirPractitionerDetails?.locationHierarchyList ?: listOf() - // TODO: Get if another user logged in previously - // TODO: Get applicationConfiguration from configurationRegistry - // Todo: Check practitionerDetails matching with current user considering - // applicationConfiguration.syncStrategy - // Todo: If not matching throw error, else continue to save val careTeamIds = withContext(dispatcherProvider.io()) { defaultRepository.createRemote(false, *careTeams.toTypedArray()).run {