From e88724e832ef01ac81a69ed44a45cd862a6394c0 Mon Sep 17 00:00:00 2001 From: Rkareko <47570855+Rkareko@users.noreply.github.com> Date: Fri, 24 May 2024 11:29:31 +0300 Subject: [PATCH] Fetch Practioner Location Id using Rules on the Configs from the SharedPreferences (#3281) * Load practitioner location Id from the shared prefs via rules engine service * Add unit test for writing practitioner location id to shared preferences * Run spotlessApply * Update unit test * Run spotless Apply --- .../engine/rulesengine/RulesFactory.kt | 5 +++++ .../engine/util/SharedPreferenceKey.kt | 1 + .../engine/rulesengine/RulesFactoryTest.kt | 18 ++++++++++++++++- .../fhircore/quest/ui/login/LoginViewModel.kt | 6 +++++- .../quest/ui/login/LoginViewModelTest.kt | 20 +++++++++++++++++++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt index 61f2b853f1..68664ffc4d 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt @@ -351,6 +351,11 @@ constructor( SharedPreferenceKey.PRACTITIONER_LOCATION.name, "", ) + SharedPreferenceKey.PRACTITIONER_LOCATION_ID -> + configurationRegistry.sharedPreferencesHelper.read( + SharedPreferenceKey.PRACTITIONER_LOCATION_ID.name, + "", + ) else -> "" } } catch (exception: Exception) { diff --git a/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SharedPreferenceKey.kt b/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SharedPreferenceKey.kt index ab38d4268e..e9277ec5b8 100644 --- a/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SharedPreferenceKey.kt +++ b/android/engine/src/main/java/org/smartregister/fhircore/engine/util/SharedPreferenceKey.kt @@ -24,6 +24,7 @@ enum class SharedPreferenceKey { PRACTITIONER_DETAILS, PRACTITIONER_LOCATION_HIERARCHIES, PRACTITIONER_LOCATION, + PRACTITIONER_LOCATION_ID, REMOTE_SYNC_RESOURCES, LOGIN_CREDENTIAL_KEY, LOGIN_PIN_KEY, diff --git a/android/engine/src/test/java/org/smartregister/fhircore/engine/rulesengine/RulesFactoryTest.kt b/android/engine/src/test/java/org/smartregister/fhircore/engine/rulesengine/RulesFactoryTest.kt index 66f93f9966..cbbe276f3f 100644 --- a/android/engine/src/test/java/org/smartregister/fhircore/engine/rulesengine/RulesFactoryTest.kt +++ b/android/engine/src/test/java/org/smartregister/fhircore/engine/rulesengine/RulesFactoryTest.kt @@ -977,7 +977,23 @@ class RulesFactoryTest : RobolectricTest() { @Test fun testExtractSharedPrefValuesReturnsPractitionerLocation() { val sharedPreferenceKey = "PRACTITIONER_LOCATION" - val expectedValue = "1234" + val expectedValue = "Demo Facility" + every { + configurationRegistry.sharedPreferencesHelper.read( + sharedPreferenceKey, + "", + ) + } returns expectedValue + val result = rulesEngineService.extractPractitionerInfoFromSharedPrefs(sharedPreferenceKey) + + verify { configurationRegistry.sharedPreferencesHelper.read(sharedPreferenceKey, "") } + Assert.assertEquals(expectedValue, result) + } + + @Test + fun testExtractSharedPrefValuesReturnsPractitionerLocationId() { + val sharedPreferenceKey = "PRACTITIONER_LOCATION_ID" + val expectedValue = "ABCD1234" every { configurationRegistry.sharedPreferencesHelper.read( sharedPreferenceKey, 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 dc72fe99be..4fda6dd085 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 @@ -415,7 +415,7 @@ constructor( ) } - private fun writePractitionerDetailsToShredPref( + fun writePractitionerDetailsToShredPref( careTeam: List, organization: List, location: List, @@ -452,6 +452,10 @@ constructor( key = SharedPreferenceKey.ORGANIZATION.name, value = organization.joinToString(separator = ""), ) + sharedPreferences.write( + key = SharedPreferenceKey.PRACTITIONER_LOCATION_ID.name, + value = locations.joinToString(separator = ""), + ) } fun downloadNowWorkflowConfigs(isInitialLogin: Boolean = true) { diff --git a/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/login/LoginViewModelTest.kt b/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/login/LoginViewModelTest.kt index 9c61a4ff8a..5f0b9d57f1 100644 --- a/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/login/LoginViewModelTest.kt +++ b/android/quest/src/test/java/org/smartregister/fhircore/quest/ui/login/LoginViewModelTest.kt @@ -33,6 +33,7 @@ import io.mockk.verify import java.net.SocketTimeoutException import java.net.UnknownHostException import javax.inject.Inject +import kotlin.test.assertEquals import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.runBlocking import okhttp3.internal.http.RealResponseBody @@ -638,6 +639,25 @@ internal class LoginViewModelTest : RobolectricTest() { verify { workManager.enqueue(any()) } } + @Test + fun testWritePractitionerDetailsToShredPrefSavesPractitionerLocationId() { + val locationId = "ABCD123" + loginViewModel.writePractitionerDetailsToShredPref( + careTeams = listOf(""), + careTeam = listOf(""), + organization = listOf(""), + organizations = listOf(""), + location = listOf(""), + locations = listOf(locationId), + fhirPractitionerDetails = PractitionerDetails(), + locationHierarchies = listOf(LocationHierarchy()), + ) + assertEquals( + locationId, + sharedPreferencesHelper.read(SharedPreferenceKey.PRACTITIONER_LOCATION_ID.name), + ) + } + private fun updateCredentials() { loginViewModel.run { onUsernameUpdated(thisUsername)