Skip to content

Commit

Permalink
Fetch PractitionerId, PractitionerLocation, PractitionerCareTeam, Pra…
Browse files Browse the repository at this point in the history
…ctitionerOrganization from Shared Preferences (#2903)

* Extract PractitionerId, PractitionerLocation, PractitionerCareTeam, PractitionerOrganization
* Create new enum class called PractitionerKey
* Create a function called extractSharedPrefValues in RulesFactory

* Fix spacing and code check

* Change few files

* Fix first failing test

* Add Practitioner Tests and rule tests in household config

* Fix failing engine tests

* Add Practitioner Tests and rule tests in household config

* Fix test view

* provide documentation on how to use extractPractitionerInfoFromSharedPrefs() rule

* Write tabular representation of enums and their infos

* Fix test issue

* run spotless

---------

Co-authored-by: Sebastian <[email protected]>
Co-authored-by: SebaMutuku <[email protected]>
Co-authored-by: Benjamin Mwalimu <[email protected]>
  • Loading branch information
4 people authored Dec 7, 2023
1 parent 3cbb77b commit 0782d87
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import org.smartregister.fhircore.engine.domain.model.RuleConfig
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 @@ constructor(
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 -> ""
}
} catch (exception: Exception) {
if (exception is IllegalArgumentException) {
Timber.e("key is not a member of practitioner keys: ", exception)
} else {
Timber.e("An exception occurred while fetching your key from sharedPrefs: ", exception)
}
}
return ""
}

/**
* 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

0 comments on commit 0782d87

Please sign in to comment.