Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling sync status by harmonizing SDK and custom sync statuses #3650

Merged
merged 28 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
760e0cd
fix tests
qaziabubakar-vd Nov 20, 2024
080443c
[Enahncement] Monitor custom sync status.
Lentumunai-Mark Dec 10, 2024
94f9daf
Fix failing tests.
Lentumunai-Mark Dec 10, 2024
6673b19
Merge branch 'main' into harmonize-sync-status
Lentumunai-Mark Dec 11, 2024
e642ec0
Merge branch 'main' into harmonize-sync-status
Lentumunai-Mark Dec 13, 2024
4646b49
Harmonize sync status files.
Lentumunai-Mark Dec 13, 2024
d66652c
Chain custom worker to fhir sync worker and clean up sync view.
Lentumunai-Mark Dec 13, 2024
5ac004b
Merge branch 'main' into harmonize-sync-status
Lentumunai-Mark Dec 13, 2024
98dad18
run cutom sync once the last fhir sync job completes.
Lentumunai-Mark Dec 16, 2024
091fd8e
add custom sync state on the register fragment.
Lentumunai-Mark Dec 16, 2024
936094e
Track fetching of cutom resources.
Lentumunai-Mark Jan 10, 2025
bd9b1b8
Merge branch 'main' into harmonize-sync-status
Lentumunai-Mark Jan 10, 2025
26ad951
Remove commented lines of code
Lentumunai-Mark Jan 10, 2025
eb8cf22
Merge branch 'main' into harmonize-sync-status
ellykits Jan 16, 2025
8771811
Merge branch 'main' into harmonize-sync-status
Lentumunai-Mark Jan 23, 2025
38f982e
Resolve spotless check failing.
Lentumunai-Mark Jan 29, 2025
ff775e2
Refactor custom sync
ellykits Jan 29, 2025
576f7d4
Merge branch 'harmonize-sync-status' of github.com:opensrp/fhircore i…
ellykits Jan 29, 2025
6d5d5f8
Refactor sync notification UI
ellykits Jan 31, 2025
d41f321
Merge branch 'main' into harmonize-sync-status
Lentumunai-Mark Jan 31, 2025
0502bc4
Fix failing tests.
Lentumunai-Mark Jan 31, 2025
21f3e2b
Fix failing tests on engine module.
Lentumunai-Mark Jan 31, 2025
e451593
Merge branch 'main' into harmonize-sync-status
ellykits Jan 31, 2025
e1574c3
Fix sync count on apps with no custom resources
ellykits Jan 31, 2025
9f193ea
Merge branch 'main' into harmonize-sync-status
dubdabasoduba Feb 3, 2025
de5b5e6
Fix failing tests
ellykits Feb 3, 2025
bccc351
Merge branch 'harmonize-sync-status' of github.com:opensrp/fhircore i…
ellykits Feb 3, 2025
2ae8519
Merge branch 'main' into harmonize-sync-status
dubdabasoduba Feb 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.android.fhir.db.ResourceNotFoundException
import com.google.android.fhir.get
import com.google.android.fhir.knowledge.KnowledgeManager
import com.google.android.fhir.sync.ParamMap
import com.google.android.fhir.sync.SyncDataParams.LAST_UPDATED_KEY
import com.google.android.fhir.sync.download.ResourceSearchParams
import dagger.hilt.android.qualifiers.ApplicationContext
Expand Down Expand Up @@ -55,7 +56,6 @@
import org.jetbrains.annotations.VisibleForTesting
import org.json.JSONObject
import org.smartregister.fhircore.engine.BuildConfig
import org.smartregister.fhircore.engine.configuration.app.ApplicationConfiguration
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
Expand Down Expand Up @@ -565,7 +565,7 @@
return resultBundle
}

suspend fun fetchResources(
private suspend fun fetchResources(
gatewayModeHeaderValue: String? = null,
url: String,
) {
Expand All @@ -581,11 +581,8 @@
Timber.e("Error occurred while retrieving resource via URL $url", throwable)
}
.getOrThrow()

val nextPageUrl = resultBundle.getLink(PAGINATION_NEXT)?.url

processResultBundleEntries(resultBundle.entry)

if (!nextPageUrl.isNullOrEmpty()) {
fetchResources(
gatewayModeHeaderValue = gatewayModeHeaderValue,
Expand All @@ -594,7 +591,7 @@
}
}

private suspend fun processResultBundleEntries(
suspend fun processResultBundleEntries(
resultBundleEntries: List<Bundle.BundleEntryComponent>,
) {
resultBundleEntries.forEach { bundleEntryComponent ->
Expand Down Expand Up @@ -784,10 +781,8 @@
}
}

suspend fun loadResourceSearchParams():
Pair<Map<String, Map<String, String>>, ResourceSearchParams> {
suspend fun loadResourceSearchParams(): Pair<Map<String, ParamMap>, ResourceSearchParams> {

Check warning on line 784 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt#L784

Added line #L784 was not covered by tests
val syncConfig = retrieveResourceConfiguration<Parameters>(ConfigType.Sync)
val appConfig = retrieveConfiguration<ApplicationConfiguration>(ConfigType.Application)
val customResourceSearchParams = mutableMapOf<String, MutableMap<String, String>>()
val fhirResourceSearchParams = mutableMapOf<ResourceType, MutableMap<String, String>>()
val organizationResourceTag =
Expand Down Expand Up @@ -847,9 +842,32 @@
}
}
}

// If there are custom resources to be synced return 2 otherwise 1
sharedPreferencesHelper.write(
SharedPreferenceKey.TOTAL_SYNC_COUNT.name,

Check warning on line 848 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt#L847-L848

Added lines #L847 - L848 were not covered by tests
if (customResourceSearchParams.isEmpty()) "1" else "2",
)
return Pair(customResourceSearchParams, fhirResourceSearchParams)
}

/**
* This function returns either '1' or '2' depending on whether there are custom resources (not
* included in ResourceType enum) in the sync configuration. The custom resources are configured
* in the sync configuration JSON file as valid FHIR SearchParameter of type 'special'. If there
* are custom resources to be synced with the data, the application will first download the custom
* resources then the rest of the app data.
*/
fun retrieveTotalSyncCount(): Int {
val totalSyncCount = sharedPreferencesHelper.read(SharedPreferenceKey.TOTAL_SYNC_COUNT.name, "")

Check warning on line 862 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt#L862

Added line #L862 was not covered by tests
return if (totalSyncCount.isNullOrBlank()) {
retrieveResourceConfiguration<Parameters>(ConfigType.Sync)
.parameter
.map { it.resource as SearchParameter }

Check warning on line 866 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt#L864-L866

Added lines #L864 - L866 were not covered by tests
.count { it.hasType() && it.type == Enumerations.SearchParamType.SPECIAL }
} else totalSyncCount.toInt()

Check warning on line 868 in android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/configuration/ConfigurationRegistry.kt#L868

Added line #L868 was not covered by tests
}

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 @@ -41,11 +41,14 @@
import com.ibm.icu.util.Calendar
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import java.time.OffsetDateTime
import kotlinx.coroutines.runBlocking
import org.smartregister.fhircore.engine.R
import org.smartregister.fhircore.engine.configuration.app.ConfigService
import org.smartregister.fhircore.engine.util.NotificationConstants
import org.smartregister.fhircore.engine.util.SharedPreferenceKey
import retrofit2.HttpException
import timber.log.Timber

@HiltWorker
class AppSyncWorker
Expand All @@ -57,6 +60,7 @@
private val openSrpFhirEngine: FhirEngine,
private val appTimeStampContext: AppTimeStampContext,
private val configService: ConfigService,
private val customResourceSyncService: CustomResourceSyncService,
) : FhirSyncWorker(appContext, workerParams), OnSyncListener {
private val notificationManager =
appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
Expand All @@ -74,11 +78,39 @@
)

override suspend fun doWork(): Result {
saveSyncStartTimestamp()
setForeground(getForegroundInfo())
return super.doWork()
kotlin
.runCatching {
saveSyncStartTimestamp()
setForeground(getForegroundInfo())
customResourceSyncService.runCustomResourceSync()

Check warning on line 85 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt#L82-L85

Added lines #L82 - L85 were not covered by tests
}
.onSuccess {
return super.doWork()

Check warning on line 88 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt#L88

Added line #L88 was not covered by tests
}
.onFailure { exception ->
when (exception) {

Check warning on line 91 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt#L91

Added line #L91 was not covered by tests
is HttpException -> {
val response = exception.response()

Check warning on line 93 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt#L93

Added line #L93 was not covered by tests
if (response != null && (400..503).contains(response.code())) {
Timber.e("HTTP exception ${response.code()} -> ${response.errorBody()}")

Check warning on line 95 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt#L95

Added line #L95 was not covered by tests
}
}
else -> Timber.e(exception)

Check warning on line 98 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt#L98

Added line #L98 was not covered by tests
}
syncListenerManager.emitSyncStatus(
SyncState(
counter = SYNC_COUNTER_1,
currentSyncJobStatus = CurrentSyncJobStatus.Failed(OffsetDateTime.now()),

Check warning on line 103 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt#L100-L103

Added lines #L100 - L103 were not covered by tests
),
)
return result()

Check warning on line 106 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt#L106

Added line #L106 was not covered by tests
}
return Result.success()

Check warning on line 108 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt#L108

Added line #L108 was not covered by tests
}

private fun result(): Result =
if (inputData.getInt(MAX_RETRIES, 3) > runAttemptCount) Result.retry() else Result.failure()

private fun saveSyncStartTimestamp() {
syncListenerManager.sharedPreferencesHelper.write(
SharedPreferenceKey.SYNC_START_TIMESTAMP.name,
Expand Down Expand Up @@ -131,8 +163,8 @@
private fun getSyncProgress(completed: Int, total: Int) =
completed * 100 / if (total > 0) total else 1

override fun onSync(syncJobStatus: CurrentSyncJobStatus) {
when (syncJobStatus) {
override fun onSync(syncState: SyncState) {
when (val syncJobStatus = syncState.currentSyncJobStatus) {

Check warning on line 167 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/AppSyncWorker.kt#L167

Added line #L167 was not covered by tests
is CurrentSyncJobStatus.Running -> {
if (syncJobStatus.inProgressSyncJob is SyncJobStatus.InProgress) {
val inProgressSyncJob = syncJobStatus.inProgressSyncJob as SyncJobStatus.InProgress
Expand Down Expand Up @@ -175,4 +207,8 @@
buildNotification(progress = progress, isSyncUpload = isSyncUpload, isInitial = false)
notificationManager.notify(NotificationConstants.NotificationId.DATA_SYNC, notification)
}

companion object {
const val MAX_RETRIES = "max_retires"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright 2021-2024 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.smartregister.fhircore.engine.sync

import com.google.android.fhir.sync.CurrentSyncJobStatus
import com.google.android.fhir.sync.SyncJobStatus
import com.google.android.fhir.sync.SyncOperation
import com.google.android.fhir.sync.concatParams
import java.time.OffsetDateTime
import javax.inject.Inject
import javax.inject.Singleton
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry.Companion.PAGINATION_NEXT
import org.smartregister.fhircore.engine.data.remote.fhir.resource.FhirResourceDataSource
import org.smartregister.fhircore.engine.util.DispatcherProvider
import timber.log.Timber

@Singleton

Check warning on line 32 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L32

Added line #L32 was not covered by tests
class CustomResourceSyncService
@Inject
constructor(
val configurationRegistry: ConfigurationRegistry,
val dispatcherProvider: DispatcherProvider,
val fhirResourceDataSource: FhirResourceDataSource,
val syncListenerManager: SyncListenerManager,

Check warning on line 39 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L35-L39

Added lines #L35 - L39 were not covered by tests
) {
suspend fun runCustomResourceSync() {
val (resourceSearchParams, _) = configurationRegistry.loadResourceSearchParams()

Check warning on line 42 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L42

Added line #L42 was not covered by tests
if (resourceSearchParams.isEmpty()) return

val resourceUrls =
resourceSearchParams
.asIterable()

Check warning on line 47 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L45-L47

Added lines #L45 - L47 were not covered by tests
.filter { it.value.isNotEmpty() }
.map { "${it.key}?${it.value.concatParams()}" }

Check warning on line 49 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L49

Added line #L49 was not covered by tests

val summaryCount = fetchSummaryCount(resourceUrls).values.sumOf { it ?: 0 }

resourceUrls.forEach { url ->
fetchCustomResources(
gatewayModeHeaderValue = ConfigurationRegistry.FHIR_GATEWAY_MODE_HEADER_VALUE,
url = url,
totalCounts = summaryCount,

Check warning on line 57 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L53-L57

Added lines #L53 - L57 were not covered by tests
)
}
}

private suspend fun fetchCustomResources(
gatewayModeHeaderValue: String? = null,

Check warning on line 63 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L62-L63

Added lines #L62 - L63 were not covered by tests
url: String,
totalCounts: Int = 0,
completedRecords: Int = 0,

Check warning on line 66 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L65-L66

Added lines #L65 - L66 were not covered by tests
) {
runCatching {
Timber.d("Setting state: Running")
syncListenerManager.emitSyncStatus(
SyncState(
counter = SYNC_COUNTER_1,

Check warning on line 72 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L68-L72

Added lines #L68 - L72 were not covered by tests
currentSyncJobStatus =
CurrentSyncJobStatus.Running(
SyncJobStatus.InProgress(
syncOperation = SyncOperation.DOWNLOAD,
total = totalCounts,
completed = completedRecords,

Check warning on line 78 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L74-L78

Added lines #L74 - L78 were not covered by tests
),
),
),
)
Timber.d("Fetching page with URL: $url")

Check warning on line 83 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L83

Added line #L83 was not covered by tests
if (gatewayModeHeaderValue.isNullOrEmpty()) {
fhirResourceDataSource.getResource(url)

Check warning on line 85 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L85

Added line #L85 was not covered by tests
} else {
fhirResourceDataSource.getResourceWithGatewayModeHeader(gatewayModeHeaderValue, url)

Check warning on line 87 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L87

Added line #L87 was not covered by tests
}
}
.onFailure { throwable ->
Timber.e("Error occurred while retrieving resource via URL $url", throwable)
syncListenerManager.emitSyncStatus(
SyncState(
counter = SYNC_COUNTER_1,
currentSyncJobStatus = CurrentSyncJobStatus.Failed(OffsetDateTime.now()),

Check warning on line 95 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L91-L95

Added lines #L91 - L95 were not covered by tests
),
)
return

Check warning on line 98 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L98

Added line #L98 was not covered by tests
}
.onSuccess { resultBundle ->
configurationRegistry.processResultBundleEntries(resultBundle.entry)
val newCompletedRecords = completedRecords + resultBundle.entry.size
syncListenerManager.emitSyncStatus(
SyncState(
counter = SYNC_COUNTER_1,

Check warning on line 105 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L101-L105

Added lines #L101 - L105 were not covered by tests
currentSyncJobStatus =
CurrentSyncJobStatus.Running(
SyncJobStatus.InProgress(
syncOperation = SyncOperation.DOWNLOAD,
total = totalCounts,
completed = newCompletedRecords,

Check warning on line 111 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L107-L111

Added lines #L107 - L111 were not covered by tests
),
),
),
)

val nextPageUrl = resultBundle.getLink(PAGINATION_NEXT)?.url

if (!nextPageUrl.isNullOrEmpty()) {
fetchCustomResources(
gatewayModeHeaderValue = gatewayModeHeaderValue,
url = nextPageUrl,
totalCounts = totalCounts,
completedRecords = newCompletedRecords,

Check warning on line 124 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L120-L124

Added lines #L120 - L124 were not covered by tests
)
} else {
Timber.d("Fetch complete. Emitting SyncStatus.Succeeded.")
syncListenerManager.emitSyncStatus(
SyncState(
counter = SYNC_COUNTER_1,
currentSyncJobStatus = CurrentSyncJobStatus.Succeeded(OffsetDateTime.now()),

Check warning on line 131 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L127-L131

Added lines #L127 - L131 were not covered by tests
),
)
}
}
}

/** Fetch summary counts for the provided [resourceUrls] */
private suspend fun fetchSummaryCount(resourceUrls: List<String>): Map<String, Int?> =
resourceUrls
.associate { url ->
val summaryUrl = "$url&summary=count"
val total: Int? =
runCatching { fhirResourceDataSource.getResource(summaryUrl) }

Check warning on line 144 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L140-L144

Added lines #L140 - L144 were not covered by tests
.onFailure { Timber.e(it, "Failed to fetch summary for $summaryUrl") }
.getOrNull()
?.total
summaryUrl to total

Check warning on line 148 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L148

Added line #L148 was not covered by tests
}
.also { summaries -> Timber.i("Summary fetch results: $summaries") }

Check warning on line 150 in android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/sync/CustomResourceSyncService.kt#L150

Added line #L150 was not covered by tests
}
Loading
Loading