Skip to content

Commit

Permalink
Updating the login flow to save multiple practitioner details (#2623)
Browse files Browse the repository at this point in the history
* Update login to allow saving of multiple Practitioner details.
* Update the status code set when the FHIR conditions are closed by the events management
* Update the login pieces to download configs every time the user is on the username/password screen
* Update the login view model to save the practitioner details for supervisor and CHWs
* Remove the Snackbar display every time sync fails
* Bump up version name
* Refactor configs to request via Batch POST

---------

Co-authored-by: Martin Ndegwa <mndegwa@ona.io>
Co-authored-by: Martin Ndegwa <ndegwamartin@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 28, 2023
1 parent e51a10d commit 138a40d
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
import okhttp3.RequestBody.Companion.toRequestBody
import org.hl7.fhir.r4.model.Base
import org.hl7.fhir.r4.model.Binary
import org.hl7.fhir.r4.model.Bundle
Expand All @@ -44,12 +45,14 @@ import org.json.JSONObject
import org.smartregister.fhircore.engine.BuildConfig
import org.smartregister.fhircore.engine.configuration.app.ConfigService
import org.smartregister.fhircore.engine.data.remote.fhir.resource.FhirResourceDataSource
import org.smartregister.fhircore.engine.di.NetworkModule
import org.smartregister.fhircore.engine.util.DispatcherProvider
import org.smartregister.fhircore.engine.util.SharedPreferenceKey
import org.smartregister.fhircore.engine.util.SharedPreferencesHelper
import org.smartregister.fhircore.engine.util.extension.camelCase
import org.smartregister.fhircore.engine.util.extension.decodeJson
import org.smartregister.fhircore.engine.util.extension.decodeResourceFromString
import org.smartregister.fhircore.engine.util.extension.encodeResourceToString
import org.smartregister.fhircore.engine.util.extension.extractId
import org.smartregister.fhircore.engine.util.extension.extractLogicalIdUuid
import org.smartregister.fhircore.engine.util.extension.fileExtension
Expand Down Expand Up @@ -377,12 +380,10 @@ constructor(
val chunkedResourceIdList =
resourceGroup.value.chunked(MANIFEST_PROCESSOR_BATCH_SIZE)
chunkedResourceIdList.forEach {
val resourceIds =
it.joinToString(",") { sectionComponent -> sectionComponent.focus.extractId() }

fhirResourceDataSource.getResource(
"${resourceGroup.key}?${Composition.SP_RES_ID}=$resourceIds&_count=$HAPI_FHIR_DEFAULT_COUNT"
)
processCompositionManifestResources(
resourceGroup.key,
it.map { sectionComponent -> sectionComponent.focus.extractId() }
)
.entry
.forEach { bundleEntryComponent ->
when (bundleEntryComponent.resource) {
Expand Down Expand Up @@ -415,14 +416,13 @@ constructor(
}
}
} else {

val chunkedResourceIdList = resourceGroup.value.chunked(MANIFEST_PROCESSOR_BATCH_SIZE)

chunkedResourceIdList.forEach {
val resourceIds =
it.joinToString(",") { sectionComponent -> sectionComponent.focus.extractId() }
processCompositionManifestResources(
searchPath =
"${resourceGroup.key}?${Composition.SP_RES_ID}=$resourceIds&_count=$HAPI_FHIR_DEFAULT_COUNT"
resourceGroup.key,
it.map { sectionComponent -> sectionComponent.focus.extractId() }
)
}
}
Expand All @@ -431,6 +431,50 @@ constructor(
}
}

private suspend fun processCompositionManifestResources(
resourceType: String,
resourceIdList: List<String>
): Bundle {

val resultBundle =
fhirResourceDataSource.post(
"",
generateRequestBundle(resourceType, resourceIdList)
.encodeResourceToString()
.toRequestBody(NetworkModule.JSON_MEDIA_TYPE)
)
resultBundle.entry?.forEach { bundleEntryComponent ->
when (bundleEntryComponent.resource) {
is Bundle -> {
val bundle = bundleEntryComponent.resource as Bundle
bundle.entry.forEach { entryComponent ->
when (entryComponent.resource) {
is Bundle -> {

val bundle = entryComponent.resource as Bundle
addOrUpdate(bundle)
bundle.entry.forEach { innerEntryComponent ->
saveListEntryResource(innerEntryComponent)
}
}
else -> saveListEntryResource(entryComponent)
}
}
}
else -> {
if (bundleEntryComponent.resource != null) {
addOrUpdate(bundleEntryComponent.resource)
Timber.d(
"Fetched and processed resources ${bundleEntryComponent.resource.resourceType}/${bundleEntryComponent.resource.id}"
)
}
}
}
}

return resultBundle
}

private suspend fun processCompositionManifestResources(
gatewayModeHeaderValue: String? = null,
searchPath: String
Expand Down Expand Up @@ -481,6 +525,7 @@ constructor(
* resource, or create it if not found.
*/
suspend fun <R : Resource> addOrUpdate(resource: R) {
if (resource == null) return
withContext(dispatcherProvider.io()) {
resource.updateLastUpdated()
try {
Expand Down Expand Up @@ -517,6 +562,27 @@ constructor(
isNonProxy_ = nonProxy
}

private fun generateRequestBundle(resourceType: String, idList: List<String>): Bundle {
val bundleEntryComponents = mutableListOf<BundleEntryComponent>()

idList.forEach {
bundleEntryComponents.add(
BundleEntryComponent().apply {
request =
Bundle.BundleEntryRequestComponent().apply {
url = "$resourceType/$it"
method = Bundle.HTTPVerb.GET
}
}
)
}

return Bundle().apply {
type = Bundle.BundleType.BATCH
entry = bundleEntryComponents
}
}

companion object {
const val BASE_CONFIG_PATH = "configs/%s"
const val COMPOSITION_CONFIG_PATH = "configs/%s/composition_config.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,9 +754,9 @@ constructor(
)

companion object {
const val SNOMED_SYSTEM = "http://www.snomed.org/"
const val PATIENT_CONDITION_RESOLVED_CODE = "370996005"
const val PATIENT_CONDITION_RESOLVED_DISPLAY = "resolved"
const val SNOMED_SYSTEM = "http://hl7.org/fhir/R4B/valueset-condition-clinical.html"
const val PATIENT_CONDITION_RESOLVED_CODE = "resolved"
const val PATIENT_CONDITION_RESOLVED_DISPLAY = "Resolved"
const val PNC_CONDITION_TO_CLOSE_RESOURCE_ID = "pncConditionToClose"
const val SICK_CHILD_CONDITION_TO_CLOSE_RESOURCE_ID = "sickChildConditionToClose"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.smartregister.fhircore.engine.data.remote.fhir.resource

import javax.inject.Inject
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.hl7.fhir.r4.model.Bundle
import org.hl7.fhir.r4.model.OperationOutcome
Expand All @@ -29,6 +30,10 @@ class FhirResourceDataSource @Inject constructor(private val resourceService: Fh
suspend fun getResource(path: String): Bundle {
return resourceService.getResource(path)
}
suspend fun post(path: String, requestBody: RequestBody): Bundle {
return resourceService.post(path, requestBody)
}

suspend fun getResourceWithGatewayModeHeader(
gatewayModeHeaderValue: String,
path: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.PATCH
import retrofit2.http.POST
import retrofit2.http.PUT
import retrofit2.http.Path
import retrofit2.http.QueryMap
Expand Down Expand Up @@ -69,4 +70,5 @@ interface FhirResourceService {
@Path("resourceType") resourceType: String,
@QueryMap(encoded = false) searchParameters: Map<String, String>
): Bundle
@POST suspend fun post(@Url url: String, @Body body: RequestBody): Bundle
}
2 changes: 1 addition & 1 deletion android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data-capture = "1.0.0-preview11-SNAPSHOT"
desugar-jdk-libs = "1.1.5"
easy-rules-jexl = "4.1.0"
espresso-core = "3.5.0"
fhir-common-utils = "0.0.7-SNAPSHOT"
fhir-common-utils = "0.0.10-SNAPSHOT"
fhir-engine = "0.1.0-beta03-preview9.5-SNAPSHOT"
foundation = "1.3.1"
fragment-ktx = "1.5.5"
Expand Down
2 changes: 1 addition & 1 deletion android/quest/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ android {
minSdk = 26
targetSdk = 33
versionCode = 3
versionName = "0.2.4"
versionName = "0.2.5"
multiDexEnabled = true

buildConfigField("boolean", "SKIP_AUTH_CHECK", "false")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import java.nio.charset.Charset
import javax.inject.Inject
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.RequestBody.Companion.toRequestBody
import okio.ByteString.Companion.decodeBase64
import org.hl7.fhir.r4.model.Binary
import org.hl7.fhir.r4.model.Bundle
import org.hl7.fhir.r4.model.Composition
import org.hl7.fhir.r4.model.ResourceType
import org.smartregister.fhircore.engine.BuildConfig
Expand All @@ -40,11 +42,13 @@ import org.smartregister.fhircore.engine.configuration.profile.ProfileConfigurat
import org.smartregister.fhircore.engine.configuration.register.RegisterConfiguration
import org.smartregister.fhircore.engine.data.local.DefaultRepository
import org.smartregister.fhircore.engine.data.remote.fhir.resource.FhirResourceDataSource
import org.smartregister.fhircore.engine.di.NetworkModule
import org.smartregister.fhircore.engine.domain.model.FhirResourceConfig
import org.smartregister.fhircore.engine.domain.model.ResourceConfig
import org.smartregister.fhircore.engine.util.DispatcherProvider
import org.smartregister.fhircore.engine.util.SharedPreferenceKey
import org.smartregister.fhircore.engine.util.SharedPreferencesHelper
import org.smartregister.fhircore.engine.util.extension.encodeResourceToString
import org.smartregister.fhircore.engine.util.extension.extractId
import org.smartregister.fhircore.engine.util.extension.getActivity
import org.smartregister.fhircore.engine.util.extension.launchActivityWithNoBackStackHistory
Expand Down Expand Up @@ -120,36 +124,45 @@ constructor(
val chunkedResourceIdList =
entry.value.chunked(ConfigurationRegistry.MANIFEST_PROCESSOR_BATCH_SIZE)
chunkedResourceIdList.forEach { parentIt ->
val ids = parentIt.joinToString(",") { it.focus.extractId() }
val resourceUrlPath =
"${entry.key}?${Composition.SP_RES_ID}=$ids&_count=${ConfigurationRegistry.HAPI_FHIR_DEFAULT_COUNT}"
Timber.d("Fetching config: $resourceUrlPath")

fhirResourceDataSource.getResource(resourceUrlPath).entry.forEach {
bundleEntryComponent ->
defaultRepository.createRemote(false, bundleEntryComponent.resource)

if (bundleEntryComponent.resource is Binary) {
val binary = bundleEntryComponent.resource as Binary
binary
.data
.decodeToString()
.decodeBase64()
?.string(Charset.defaultCharset())
?.let {
val config =
it.tryDecodeJson<RegisterConfiguration>()
?: it.tryDecodeJson<ProfileConfiguration>()

when (config) {
is RegisterConfiguration ->
config.fhirResource.dependentResourceTypes(patientRelatedResourceTypes)
is ProfileConfiguration ->
config.fhirResource.dependentResourceTypes(patientRelatedResourceTypes)
}
Timber.d("Fetching config resource ${entry.key}: with ids $parentIt")
fhirResourceDataSource.post(
"",
generateRequestBundle(entry.key, parentIt.map { it.focus.extractId() })
.encodeResourceToString()
.toRequestBody(NetworkModule.JSON_MEDIA_TYPE)
)
.entry
.forEach { bundleEntryComponent ->
if (bundleEntryComponent.resource != null) {

defaultRepository.createRemote(false, bundleEntryComponent.resource)

if (bundleEntryComponent.resource is Binary) {
val binary = bundleEntryComponent.resource as Binary
binary
.data
.decodeToString()
.decodeBase64()
?.string(Charset.defaultCharset())
?.let {
val config =
it.tryDecodeJson<RegisterConfiguration>()
?: it.tryDecodeJson<ProfileConfiguration>()

when (config) {
is RegisterConfiguration ->
config.fhirResource.dependentResourceTypes(
patientRelatedResourceTypes
)
is ProfileConfiguration ->
config.fhirResource.dependentResourceTypes(
patientRelatedResourceTypes
)
}
}
}
}
}
}
}
}

Expand Down Expand Up @@ -218,4 +231,25 @@ constructor(

fun hasDebugSuffix(): Boolean =
appId.value?.endsWith(DEBUG_SUFFIX, ignoreCase = true) == true && BuildConfig.DEBUG

private fun generateRequestBundle(resourceType: String, idList: List<String>): Bundle {
val bundleEntryComponents = mutableListOf<Bundle.BundleEntryComponent>()

idList.forEach {
bundleEntryComponents.add(
Bundle.BundleEntryComponent().apply {
request =
Bundle.BundleEntryRequestComponent().apply {
url = "$resourceType/$it"
method = Bundle.HTTPVerb.GET
}
}
)
}

return Bundle().apply {
type = Bundle.BundleType.BATCH
entry = bundleEntryComponents
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ open class LoginActivity : BaseMultiLanguageActivity() {
if ((loginActivity.deviceOnline() && loginActivity.isRefreshTokenActive()) ||
!loginActivity.deviceOnline()
) {

navigateToPinLogin(launchSetup = false)
}
}

navigateToHome.observe(loginActivity) { launchHomeScreen ->
if (launchHomeScreen) {
if (!hasActivePin) downloadNowWorkflowConfigs()
downloadNowWorkflowConfigs()
if (isPinEnabled && !hasActivePin) navigateToPinLogin(launchSetup = true)
else loginActivity.navigateToHome()
}
Expand Down
Loading

0 comments on commit 138a40d

Please sign in to comment.