Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch PractitionerId, PractitionerLocation, PractitionerCareTeam, PractitionerOrganization from Shared Preferences #2903

Merged
merged 20 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2a74bfb
Extract PractitionerId, PractitionerLocation, PractitionerCareTeam, P…
Raynafs Dec 1, 2023
f788202
Fix spacing and code check
Raynafs Dec 1, 2023
f21547f
Merge branch 'main' into fetch-assignment-data-using-rules
SebaMutuku Dec 3, 2023
bb883ac
Change few files
Raynafs Dec 4, 2023
3ccbf85
Merge branch 'fetch-assignment-data-using-rules' of github.com:opensr…
Raynafs Dec 4, 2023
ecce3e2
Fix first failing test
SebaMutuku Dec 4, 2023
f0f914e
Add Practitioner Tests and rule tests in household config
Raynafs Dec 4, 2023
474bde5
Fix failing engine tests
SebaMutuku Dec 4, 2023
94421b9
Add Practitioner Tests and rule tests in household config
Raynafs Dec 4, 2023
eb104ca
Merge branch 'fetch-assignment-data-using-rules' of github.com:opensr…
Raynafs Dec 4, 2023
fed2b95
Fix failing engine test
SebaMutuku Dec 4, 2023
4bb8a94
Merge branch 'fetch-assignment-data-using-rules' of github.com:opensr…
SebaMutuku Dec 4, 2023
45d041b
Fix test view
Raynafs Dec 5, 2023
0954d17
Merge branch 'fetch-assignment-data-using-rules' of github.com:opensr…
Raynafs Dec 5, 2023
62a167f
provide documentation on how to use extractPractitionerInfoFromShared…
Raynafs Dec 5, 2023
6746905
Write tabular representation of enums and their infos
Raynafs Dec 6, 2023
3491f10
Fix test issue
Raynafs Dec 6, 2023
8343254
Merge branch 'fetch-assignment-data-using-rules' of github.com:opensr…
Raynafs Dec 6, 2023
277549e
run spotless
Raynafs Dec 6, 2023
79afa10
Merge branch 'main' into fetch-assignment-data-using-rules
dubdabasoduba Dec 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.smartregister.fhircore.engine.domain.model.ServiceMemberIcon
import org.smartregister.fhircore.engine.domain.model.ServiceStatus
import org.smartregister.fhircore.engine.util.DispatcherProvider
import org.smartregister.fhircore.engine.util.SharedPreferenceKey
import org.smartregister.fhircore.engine.util.extension.SDF_E_MMM_DD_YYYY
import org.smartregister.fhircore.engine.util.extension.extractAge
import org.smartregister.fhircore.engine.util.extension.extractGender
Expand Down Expand Up @@ -301,6 +302,47 @@
return PrettyTime().format(DateTime(inputDateString).toDate())
}

/**
* This function fetches assignment data separately that is; PractitionerId,
* PractitionerCareTeam, PractitionerOrganization and PractitionerLocation, using rules on the
* configs.
*/
fun extractPractitionerInfoFromSharedPrefs(practitionerKey: String): String? {
val key = SharedPreferenceKey.valueOf(practitionerKey)
try {
return when (key) {
SharedPreferenceKey.PRACTITIONER_ID ->
configurationRegistry.sharedPreferencesHelper.read(
SharedPreferenceKey.PRACTITIONER_ID.name,
"",
)
SharedPreferenceKey.CARE_TEAM ->
configurationRegistry.sharedPreferencesHelper.read(
SharedPreferenceKey.CARE_TEAM.name,
"",
)
SharedPreferenceKey.ORGANIZATION ->
configurationRegistry.sharedPreferencesHelper.read(
SharedPreferenceKey.ORGANIZATION.name,
"",
)
SharedPreferenceKey.PRACTITIONER_LOCATION ->
configurationRegistry.sharedPreferencesHelper.read(
SharedPreferenceKey.PRACTITIONER_LOCATION.name,
"",
)
else -> ""

Check warning on line 334 in android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt#L334

Added line #L334 was not covered by tests
}
} catch (exception: Exception) {

Check warning on line 336 in android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt#L336

Added line #L336 was not covered by tests
if (exception is IllegalArgumentException) {
Timber.e("key is not a member of practitioner keys: ", exception)

Check warning on line 338 in android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt#L338

Added line #L338 was not covered by tests
} else {
Timber.e("An exception occurred while fetching your key from sharedPrefs: ", exception)

Check warning on line 340 in android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt#L340

Added line #L340 was not covered by tests
}
}
return ""

Check warning on line 343 in android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/rulesengine/RulesFactory.kt#L343

Added line #L343 was not covered by tests
}

/**
* This function is responsible for formatting a date for whatever expectedFormat we need. It
* takes an [inputDate] string along with the [inputDateFormat] so it can convert it to the Date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ enum class SharedPreferenceKey {
PRACTITIONER_ID,
PRACTITIONER_DETAILS,
PRACTITIONER_LOCATION_HIERARCHIES,
PRACTITIONER_LOCATION,
REMOTE_SYNC_RESOURCES,
LOGIN_CREDENTIAL_KEY,
LOGIN_PIN_KEY,
LOGIN_PIN_SALT,
LAST_OFFSET,
USER_INFO,
CARE_TEAM,
ORGANIZATION,
}
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,11 @@ class RulesFactoryTest : RobolectricTest() {
fun evaluateToBooleanReturnsCorrectValueWhenMatchAllIsTrue() {
val fhirPathExpression = "Patient.active"
val patients =
mutableListOf(Patient().setActive(true), Patient().setActive(true), Patient().setActive(true))
mutableListOf(
Patient().setActive(true),
Patient().setActive(true),
Patient().setActive(true),
)

Assert.assertTrue(rulesEngineService.evaluateToBoolean(patients, fhirPathExpression, true))

Expand All @@ -728,7 +732,11 @@ class RulesFactoryTest : RobolectricTest() {
fun evaluateToBooleanReturnsCorrectValueWhenMatchAllIsFalse() {
val fhirPathExpression = "Patient.active"
val patients =
mutableListOf(Patient().setActive(true), Patient().setActive(true), Patient().setActive(true))
mutableListOf(
Patient().setActive(true),
Patient().setActive(true),
Patient().setActive(true),
)

Assert.assertTrue(rulesEngineService.evaluateToBoolean(patients, fhirPathExpression, false))

Expand Down Expand Up @@ -847,7 +855,87 @@ class RulesFactoryTest : RobolectricTest() {
)
Assert.assertEquals(
period.years.toString() + "y",
rulesEngineService.extractAge(Patient().setBirthDate(LocalDate.parse("2005-01-01").toDate())),
rulesEngineService.extractAge(
Patient()
.setBirthDate(
LocalDate.parse("2005-01-01").toDate(),
),
),
)
}

@Test
fun testExtractSharedPrefValuesReturnsPractitionerId() {
val sharedPreferenceKey = "PRACTITIONER_ID"
val expectedValue = "1234"
every {
configurationRegistry.sharedPreferencesHelper.read(
sharedPreferenceKey,
"",
)
} returns expectedValue
val result = rulesEngineService.extractPractitionerInfoFromSharedPrefs(sharedPreferenceKey)

verify { configurationRegistry.sharedPreferencesHelper.read(sharedPreferenceKey, "") }
Assert.assertEquals(expectedValue, result)
}

@Test
fun testExtractSharedPrefValuesReturnsCareTeam() {
val sharedPreferenceKey = "CARE_TEAM"
val expectedValue = "1234"
every {
configurationRegistry.sharedPreferencesHelper.read(
sharedPreferenceKey,
"",
)
} returns expectedValue
val result = rulesEngineService.extractPractitionerInfoFromSharedPrefs(sharedPreferenceKey)

verify { configurationRegistry.sharedPreferencesHelper.read(sharedPreferenceKey, "") }
Assert.assertEquals(expectedValue, result)
}

@Test
fun testExtractSharedPrefValuesReturnsOrganization() {
val sharedPreferenceKey = "ORGANIZATION"
val expectedValue = "1234"
every {
configurationRegistry.sharedPreferencesHelper.read(
sharedPreferenceKey,
"",
)
} returns expectedValue
val result = rulesEngineService.extractPractitionerInfoFromSharedPrefs(sharedPreferenceKey)

verify { configurationRegistry.sharedPreferencesHelper.read(sharedPreferenceKey, "") }
Assert.assertEquals(expectedValue, result)
}

@Test
fun testExtractSharedPrefValuesReturnsPractitionerLocation() {
val sharedPreferenceKey = "PRACTITIONER_LOCATION"
val expectedValue = "1234"
every {
configurationRegistry.sharedPreferencesHelper.read(
sharedPreferenceKey,
"",
)
} returns expectedValue
val result = rulesEngineService.extractPractitionerInfoFromSharedPrefs(sharedPreferenceKey)

verify { configurationRegistry.sharedPreferencesHelper.read(sharedPreferenceKey, "") }
Assert.assertEquals(expectedValue, result)
}

@Test
fun testExtractSharedPrefValuesThrowsAnExceptionWhenKeyIsInvalid() {
val sharedPreferenceKey = "INVALID_KEY"
Assert.assertThrows(
"key is not a member of practitioner keys: ",
IllegalArgumentException::class.java,
) {
rulesEngineService.extractPractitionerInfoFromSharedPrefs(sharedPreferenceKey)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,34 @@
"actions": [
"data.put('statuses', service.joinToString([data.get('getChildStatus'), data.get('getSickChildStatus'), data.get('getPregnancyStatus'), data.get('getFPStatus'), data.get('getPNCStatus'), data.get('getMentalHeathStatus'), data.get('getHIVStatus'),data.get('getCMNTDStatus'), data.get('getTBStatus'), data.get('removalReasonPatient'), data.get('getFamilyHeadStatus'), ...]))"
]
},
{
"name": "practitionerId",
"condition": "true",
"actions": [
"data.put('practitionerId',service.extractPractitionerInfoFromSharedPrefs('PRACTITIONER_ID'))"
]
},
{
"name": "careTeam",
"condition": "true",
"actions": [
"data.put('careTeam',service.extractPractitionerInfoFromSharedPrefs('CARE_TEAM'))"
]
},
{
"name": "organization",
"condition": "true",
"actions": [
"data.put('organization',service.extractPractitionerInfoFromSharedPrefs('ORGANIZATION'))"
]
},
{
"name": "practitionerLocation",
"condition": "true",
"actions": [
"data.put('practitionerLocation',service.extractPractitionerInfoFromSharedPrefs('PRACTITIONER_LOCATION'))"
]
}
],
"views": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class QuestionnaireActivityTest : RobolectricTest() {

@Test
fun testThatActivityFinishesWhenQuestionnaireIsNull() {
val toast = mockk<Toast>()
val toast = mockk<Toast>(relaxed = true)
every { toast.show() } just runs
mockkStatic(Toast::class)
every { Toast.makeText(any(), any<String>(), Toast.LENGTH_LONG) } returns toast
Expand Down
Loading