diff --git a/shared/src/commonMain/kotlin/io/redlink/more/more_app_mutliplatform/services/network/RegistrationService.kt b/shared/src/commonMain/kotlin/io/redlink/more/more_app_mutliplatform/services/network/RegistrationService.kt index d68b40ee..6a420418 100644 --- a/shared/src/commonMain/kotlin/io/redlink/more/more_app_mutliplatform/services/network/RegistrationService.kt +++ b/shared/src/commonMain/kotlin/io/redlink/more/more_app_mutliplatform/services/network/RegistrationService.kt @@ -10,6 +10,7 @@ */ package io.redlink.more.more_app_mutliplatform.services.network +import io.github.aakira.napier.Napier import io.redlink.more.app.android.services.network.errors.NetworkServiceError import io.redlink.more.more_app_mutliplatform.Shared import io.redlink.more.more_app_mutliplatform.database.DatabaseManager @@ -26,7 +27,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.launch -class RegistrationService ( +class RegistrationService( private val shared: Shared ) { var study: Study? = null @@ -39,10 +40,19 @@ class RegistrationService ( fun getEndpointRepository(): EndpointRepository = shared.endpointRepository - fun sendRegistrationToken(token: String, manualEndpoint: String? = null, onSuccess: (Study) -> Unit, onError: ((NetworkServiceError?) -> Unit), onFinish: () -> Unit) { + fun sendRegistrationToken( + token: String, + manualEndpoint: String? = null, + onSuccess: (Study) -> Unit, + onError: ((NetworkServiceError?) -> Unit), + onFinish: () -> Unit + ) { if (token.isNotEmpty()) { Scope.launch { - val (result, networkError) = shared.networkService.validateRegistrationToken( token.uppercase(), manualEndpoint) + val (result, networkError) = shared.networkService.validateRegistrationToken( + token.uppercase(), + manualEndpoint + ) result?.let { endpoint = manualEndpoint study = it @@ -57,14 +67,20 @@ class RegistrationService ( } } - fun acceptConsent(consentInfoMd5: String, uniqueDeviceId: String, onSuccess: (Boolean) -> Unit, onError: ((NetworkServiceError?) -> Unit), onFinish: () -> Unit) { + fun acceptConsent( + consentInfoMd5: String, + uniqueDeviceId: String, + onSuccess: (Boolean) -> Unit, + onError: ((NetworkServiceError?) -> Unit), + onFinish: () -> Unit + ) { Scope.launch { shared.credentialRepository.remove() shared.endpointRepository.removeEndpoint() DatabaseManager.deleteAll() } study?.let { study -> - participationToken?.let {token -> + participationToken?.let { token -> val studyConsent = StudyConsent( consent = true, observations = study.observations.map { @@ -73,14 +89,25 @@ class RegistrationService ( consentInfoMD5 = consentInfoMd5, deviceId = "${getPlatform().productName}#$uniqueDeviceId" ) - sendConsent(token, studyConsent, study, endpoint, onSuccess, onError, onFinish) + sendConsent(token, studyConsent, endpoint, onSuccess, onError, onFinish) } } } - private fun sendConsent(token: String, studyConsent: StudyConsent, study: Study, endpoint: String? = null, onSuccess: (Boolean) -> Unit, onError: ((NetworkServiceError?) -> Unit), onFinish: () -> Unit) { + private fun sendConsent( + token: String, + studyConsent: StudyConsent, + endpoint: String? = null, + onSuccess: (Boolean) -> Unit, + onError: ((NetworkServiceError?) -> Unit), + onFinish: () -> Unit + ) { scope.launch { - val (config, networkError) = shared.networkService.sendConsent(token, studyConsent, endpoint) + val (config, networkError) = shared.networkService.sendConsent( + token, + studyConsent, + endpoint + ) if (config != null) { config.endpoint?.let { shared.endpointRepository.storeEndpoint(it) @@ -88,10 +115,22 @@ class RegistrationService ( val credentialModel = CredentialModel(config.credentials.apiId, config.credentials.apiKey) if (shared.credentialRepository.store(credentialModel) && shared.credentialRepository.hasCredentials()) { - StudyRepository().storeStudy(study) - shared.observationFactory.addNeededObservationTypes(study.observations.map { it.observationType }.toSet()) - shared.resetFirstStartUp() - onSuccess(shared.credentialRepository.hasCredentials()) + + val (study, error) = shared.networkService.getStudyConfig() + if (error != null) { + Napier.e { error.message } + onError(NetworkServiceError(null, "Could not get study: ${error.message}")) + } else { + study?.let { study -> + StudyRepository().storeStudy(study) + shared.observationFactory + .addNeededObservationTypes(study.observations.map { it.observationType }.toSet()) + shared.resetFirstStartUp() + onSuccess(shared.credentialRepository.hasCredentials()) + } ?: kotlin.run { + onError(NetworkServiceError(null, "Could not get study")) + } + } } else { onError(NetworkServiceError(null, "Could not store credentials")) }