Skip to content

Commit

Permalink
Check syncStrategy matches to enable "team" login
Browse files Browse the repository at this point in the history
  • Loading branch information
LZRS committed Sep 29, 2023
1 parent e121c22 commit caffff1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -139,53 +140,69 @@ constructor(
}
},
onFetchPractitioner = { bundleResult, userInfo ->
if (bundleResult.isSuccess) {
val bundle = bundleResult.getOrDefault(FhirR4ModelBundle())
bundle.entry.map { it.resource }.filterIsInstance<PractitionerDetails>()
.forEach {
val existingCredentials = secureSharedPreference.retrieveCredentials()
val existPractitionerDetails = sharedPreferences.read<PractitionerDetails>(
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<PractitionerDetails>()
.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<List<String>>(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<List<String>>(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<List<String>>(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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit caffff1

Please sign in to comment.