Skip to content

Commit

Permalink
Merge branch 'main' into enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
pld authored Nov 19, 2024
2 parents 281cc2b + e83bd59 commit d555208
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 233 deletions.
5 changes: 3 additions & 2 deletions android/buildSrc/src/main/kotlin/BuildConfigs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ object BuildConfigs {
const val compileSdk = 34
const val targetSdk = 34
const val versionCode = 11
const val versionName = "2.0.0"
const val versionName = "2.0.1"
const val applicationId = "org.smartregister.opensrp"
const val jvmToolchain = 17
const val kotlinCompilerExtensionVersion = "1.5.8"
const val jacocoVersion ="0.8.11"
const val ktLintVersion = "0.49.0"
const val enableUnitTestCoverage = true
const val enableAndroidTestCoverage = false
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ constructor(
fhirEngine.update(resource)
}

suspend fun applyDbTransaction(block: suspend () -> Unit) {
fhirEngine.withTransaction { block.invoke() }
}

suspend fun loadManagingEntity(group: Group) =
group.managingEntity?.let { reference ->
fhirEngine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import java.util.Base64
import javax.inject.Inject
import javax.inject.Singleton
import javax.net.ssl.SSLHandshakeException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.smartregister.fhircore.engine.configuration.app.ConfigService
import org.smartregister.fhircore.engine.data.remote.auth.OAuthService
Expand Down Expand Up @@ -215,8 +217,11 @@ constructor(
addAccountExplicitly(newAccount, oAuthResponse.refreshToken, null)
setAuthToken(newAccount, AUTH_TOKEN_TYPE, oAuthResponse.accessToken)
}

// Save credentials
secureSharedPreference.saveCredentials(username, password)
CoroutineScope(dispatcherProvider.io()).launch {
secureSharedPreference.saveCredentials(username, password)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.RequestBody.Companion.toRequestBody
import org.apache.commons.lang3.StringUtils
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,73 +195,93 @@ constructor(
context = context,
)

saveExtractedResources(
bundle = bundle,
questionnaire = questionnaire,
questionnaireConfig = questionnaireConfig,
questionnaireResponse = currentQuestionnaireResponse,
context = context,
defaultRepository.applyDbTransaction {
performSave(
bundle,
questionnaire,
questionnaireConfig,
currentQuestionnaireResponse,
context,
actionParameters,
)
}

val idTypes =
bundle.entry?.map { IdType(it.resource.resourceType.name, it.resource.logicalId) }
?: emptyList()

onSuccessfulSubmission(
idTypes,
currentQuestionnaireResponse,
)
}
}

updateResourcesLastUpdatedProperty(actionParameters)
private suspend fun performSave(
bundle: Bundle,
questionnaire: Questionnaire,
questionnaireConfig: QuestionnaireConfig,
currentQuestionnaireResponse: QuestionnaireResponse,
context: Context,
actionParameters: List<ActionParameter>,
) {
saveExtractedResources(
bundle = bundle,
questionnaire = questionnaire,
questionnaireConfig = questionnaireConfig,
questionnaireResponse = currentQuestionnaireResponse,
context = context,
)

// Important to load subject resource to retrieve ID (as reference) correctly
val subjectIdType: IdType? =
if (currentQuestionnaireResponse.subject.reference.isNullOrEmpty()) {
null
} else {
IdType(currentQuestionnaireResponse.subject.reference)
}
updateResourcesLastUpdatedProperty(actionParameters)

if (subjectIdType != null) {
val subject =
loadResource(
ResourceType.valueOf(subjectIdType.resourceType),
subjectIdType.idPart,
)
// Important to load subject resource to retrieve ID (as reference) correctly
val subjectIdType: IdType? =
if (currentQuestionnaireResponse.subject.reference.isNullOrEmpty()) {
null
} else {
IdType(currentQuestionnaireResponse.subject.reference)
}

if (subject != null && !questionnaireConfig.isReadOnly()) {
val newBundle = bundle.copyBundle(currentQuestionnaireResponse)
if (subjectIdType != null) {
val subject =
loadResource(
ResourceType.valueOf(subjectIdType.resourceType),
subjectIdType.idPart,
)

val extractedResources = newBundle.entry.map { it.resource }
validateWithFhirValidator(*extractedResources.toTypedArray())
if (subject != null && !questionnaireConfig.isReadOnly()) {
val newBundle = bundle.copyBundle(currentQuestionnaireResponse)

generateCarePlan(
subject = subject,
bundle = newBundle,
questionnaireConfig = questionnaireConfig,
)
val extractedResources = newBundle.entry.map { it.resource }
validateWithFhirValidator(*extractedResources.toTypedArray())

withContext(dispatcherProvider.io()) {
executeCql(
subject = subject,
bundle = newBundle,
questionnaire = questionnaire,
questionnaireConfig = questionnaireConfig,
)
}
generateCarePlan(
subject = subject,
bundle = newBundle,
questionnaireConfig = questionnaireConfig,
)

fhirCarePlanGenerator.conditionallyUpdateResourceStatus(
questionnaireConfig = questionnaireConfig,
withContext(dispatcherProvider.io()) {
executeCql(
subject = subject,
bundle = newBundle,
questionnaire = questionnaire,
questionnaireConfig = questionnaireConfig,
)
}
}

softDeleteResources(questionnaireConfig)

retireUsedQuestionnaireUniqueId(questionnaireConfig, currentQuestionnaireResponse)
fhirCarePlanGenerator.conditionallyUpdateResourceStatus(
questionnaireConfig = questionnaireConfig,
subject = subject,
bundle = newBundle,
)
}
}

val idTypes =
bundle.entry?.map { IdType(it.resource.resourceType.name, it.resource.logicalId) }
?: emptyList()
softDeleteResources(questionnaireConfig)

onSuccessfulSubmission(
idTypes,
currentQuestionnaireResponse,
)
}
retireUsedQuestionnaireUniqueId(questionnaireConfig, currentQuestionnaireResponse)
}

fun validateWithFhirValidator(vararg resource: Resource) {
Expand Down Expand Up @@ -642,27 +662,25 @@ constructor(
): Bundle =
kotlin
.runCatching {
withContext(dispatcherProvider.default()) {
if (extractByStructureMap) {
ResourceMapper.extract(
questionnaire = questionnaire,
questionnaireResponse = questionnaireResponse,
structureMapExtractionContext =
StructureMapExtractionContext(
transformSupportServices = transformSupportServices,
structureMapProvider = { structureMapUrl: String?, _: IWorkerContext ->
structureMapUrl?.substringAfterLast("/")?.let { structureMapId ->
defaultRepository.loadResourceFromCache<StructureMap>(structureMapId)
}
},
),
)
} else {
ResourceMapper.extract(
questionnaire = questionnaire,
questionnaireResponse = questionnaireResponse,
)
}
if (extractByStructureMap) {
ResourceMapper.extract(
questionnaire = questionnaire,
questionnaireResponse = questionnaireResponse,
structureMapExtractionContext =
StructureMapExtractionContext(
transformSupportServices = transformSupportServices,
structureMapProvider = { structureMapUrl: String?, _: IWorkerContext ->
structureMapUrl?.substringAfterLast("/")?.let { structureMapId ->
defaultRepository.loadResourceFromCache<StructureMap>(structureMapId)
}
},
),
)
} else {
ResourceMapper.extract(
questionnaire = questionnaire,
questionnaireResponse = questionnaireResponse,
)
}
}
.onFailure { exception ->
Expand Down
Loading

0 comments on commit d555208

Please sign in to comment.