-
Notifications
You must be signed in to change notification settings - Fork 137
feat: [ANDROAPP-7605] Add granular sync use-case for programs #4954
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
sync/src/androidMain/kotlin/org/dhis2/mobile/sync/data/SyncProgramRepositoryImpl.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package org.dhis2.mobile.sync.data | ||
|
|
||
| import org.dhis2.mobile.sync.model.ProgramType | ||
| import org.hisp.dhis.android.core.D2 | ||
| import org.hisp.dhis.android.core.common.State | ||
| import org.hisp.dhis.android.core.program.ProgramType as CoreProgramType | ||
|
|
||
| internal class SyncProgramRepositoryImpl( | ||
| private val d2: D2, | ||
| ) : SyncProgramRepository { | ||
| override suspend fun getProgramType(programUid: String): ProgramType = | ||
| when ( | ||
| d2 | ||
| .programModule() | ||
| .programs() | ||
| .uid(programUid) | ||
| .suspendGet() | ||
| ?.programType | ||
| ) { | ||
| CoreProgramType.WITH_REGISTRATION -> ProgramType.Tracker | ||
| CoreProgramType.WITHOUT_REGISTRATION -> ProgramType.Event | ||
| null -> ProgramType.None | ||
| } | ||
|
|
||
| override suspend fun uploadTrackerProgram(programUid: String) { | ||
| d2 | ||
| .trackedEntityModule() | ||
| .trackedEntityInstances() | ||
| .byProgramUids(listOf(programUid)) | ||
| .blockingUpload() | ||
| } | ||
|
|
||
| override suspend fun downloadTrackerProgram(programUid: String) { | ||
| d2 | ||
| .trackedEntityModule() | ||
| .trackedEntityInstanceDownloader() | ||
| .byProgramUid(programUid) | ||
| .blockingDownload() | ||
| } | ||
|
|
||
| override suspend fun uploadEventProgram(programUid: String) { | ||
| d2 | ||
| .eventModule() | ||
| .events() | ||
| .byProgramUid() | ||
| .eq(programUid) | ||
| .blockingUpload() | ||
| } | ||
|
|
||
| override suspend fun downloadEventProgram(programUid: String) { | ||
| d2 | ||
| .eventModule() | ||
| .eventDownloader() | ||
| .byProgramUid(programUid) | ||
| .blockingDownload() | ||
| } | ||
|
|
||
| override suspend fun downloadFileResources(programUid: String) { | ||
| d2 | ||
| .fileResourceModule() | ||
| .fileResourceDownloader() | ||
| .byProgramUid() | ||
| .eq(programUid) | ||
| .blockingDownload() | ||
| } | ||
|
|
||
| override suspend fun allTeisAreSynced(programUid: String) = | ||
| d2 | ||
| .trackedEntityModule() | ||
| .trackedEntityInstances() | ||
| .byProgramUids(listOf(programUid)) | ||
| .byAggregatedSyncState() | ||
| .notIn(State.SYNCED, State.RELATIONSHIP) | ||
| .blockingGet() | ||
| .isEmpty() | ||
|
|
||
| override suspend fun allEventsAreSynced(programUid: String) = | ||
| d2 | ||
| .eventModule() | ||
| .events() | ||
| .byProgramUid() | ||
| .eq(programUid) | ||
| .byAggregatedSyncState() | ||
| .notIn(State.SYNCED) | ||
| .blockingGet() | ||
| .isEmpty() | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
sync/src/commonMain/kotlin/org/dhis2/mobile/sync/data/SyncProgramRepository.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package org.dhis2.mobile.sync.data | ||
|
|
||
| import org.dhis2.mobile.sync.model.ProgramType | ||
|
|
||
| typealias IsSynced = Boolean | ||
|
|
||
| internal interface SyncProgramRepository { | ||
| suspend fun getProgramType(programUid: String): ProgramType | ||
|
|
||
| suspend fun uploadTrackerProgram(programUid: String) | ||
|
|
||
| suspend fun downloadTrackerProgram(programUid: String) | ||
|
|
||
| suspend fun uploadEventProgram(programUid: String) | ||
|
|
||
| suspend fun downloadEventProgram(programUid: String) | ||
|
|
||
| suspend fun downloadFileResources(programUid: String) | ||
|
|
||
| suspend fun allTeisAreSynced(programUid: String): Boolean | ||
|
|
||
| suspend fun allEventsAreSynced(programUid: String): Boolean | ||
| } |
53 changes: 53 additions & 0 deletions
53
sync/src/commonMain/kotlin/org/dhis2/mobile/sync/domain/SyncProgram.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package org.dhis2.mobile.sync.domain | ||
|
|
||
| import org.dhis2.mobile.commons.domain.UseCase | ||
| import org.dhis2.mobile.sync.data.SyncProgramRepository | ||
| import org.dhis2.mobile.sync.model.ProgramType | ||
|
|
||
| internal typealias ProgramUid = String | ||
|
|
||
| internal class SyncProgram( | ||
| private val syncProgramRepository: SyncProgramRepository, | ||
| private val syncStatusController: SyncStatusController, | ||
| ) : UseCase<ProgramUid, Unit> { | ||
| override suspend fun invoke(input: ProgramUid): Result<Unit> = | ||
| try { | ||
| val isSynced = | ||
| when (syncProgramRepository.getProgramType(input)) { | ||
| ProgramType.Event -> syncEvents(input) | ||
| ProgramType.Tracker -> syncTracker(input) | ||
| ProgramType.None -> return Result.failure(Exception("Unknown program")) | ||
| } | ||
|
|
||
| if (isSynced) { | ||
| syncStatusController.updateSingleProgramToSuccess(input) | ||
| Result.success(Unit) | ||
| } else { | ||
| Result.failure(Exception("Status is not synced")) | ||
| } | ||
| } catch (e: Exception) { | ||
| Result.failure(e) | ||
| } | ||
|
|
||
| private suspend fun syncEvents(programUid: ProgramUid): Boolean = | ||
| with(syncProgramRepository) { | ||
| uploadEventProgram(programUid) | ||
| val isSynced = allEventsAreSynced(programUid) | ||
| if (isSynced) { | ||
| downloadEventProgram(programUid) | ||
| downloadFileResources(programUid) | ||
| } | ||
| isSynced | ||
| } | ||
|
|
||
| private suspend fun syncTracker(programUid: ProgramUid): Boolean = | ||
| with(syncProgramRepository) { | ||
| uploadTrackerProgram(programUid) | ||
| val isSynced = allTeisAreSynced(programUid) | ||
| if (isSynced) { | ||
| downloadTrackerProgram(programUid) | ||
| downloadFileResources(programUid) | ||
| } | ||
| isSynced | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
sync/src/commonMain/kotlin/org/dhis2/mobile/sync/model/ProgramType.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package org.dhis2.mobile.sync.model | ||
|
|
||
| sealed interface ProgramType { | ||
| data object Event : ProgramType | ||
|
|
||
| data object Tracker : ProgramType | ||
|
|
||
| data object None : ProgramType | ||
| } |
99 changes: 99 additions & 0 deletions
99
sync/src/commonTest/kotlin/org/dhis2/mobile/sync/domain/SyncProgramTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package org.dhis2.mobile.sync.domain | ||
|
|
||
| import kotlinx.coroutines.runBlocking | ||
| import org.dhis2.mobile.sync.data.SyncProgramRepository | ||
| import org.dhis2.mobile.sync.model.ProgramType | ||
| import org.junit.Test | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.never | ||
| import org.mockito.kotlin.verify | ||
| import org.mockito.kotlin.whenever | ||
| import kotlin.test.assertTrue | ||
|
|
||
| class SyncProgramTest { | ||
| private val repository: SyncProgramRepository = mock() | ||
| private val syncStatusController = SyncStatusController() | ||
| private val useCase = SyncProgram(repository, syncStatusController) | ||
|
|
||
| @Test | ||
| fun `should sync event program successfully when all events are synced`() = | ||
| runBlocking { | ||
| val programUid = "eventProgramUid" | ||
| whenever(repository.getProgramType(programUid)).thenReturn(ProgramType.Event) | ||
| whenever(repository.allEventsAreSynced(programUid)).thenReturn(true) | ||
|
|
||
| val result = useCase(programUid) | ||
|
|
||
| assertTrue(result.isSuccess) | ||
| verify(repository).uploadEventProgram(programUid) | ||
| verify(repository).downloadEventProgram(programUid) | ||
| verify(repository).downloadFileResources(programUid) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should not download event program when events are not synced`() = | ||
| runBlocking { | ||
| val programUid = "eventProgramUid" | ||
| whenever(repository.getProgramType(programUid)).thenReturn(ProgramType.Event) | ||
| whenever(repository.allEventsAreSynced(programUid)).thenReturn(false) | ||
|
|
||
| val result = useCase(programUid) | ||
|
|
||
| assertTrue(result.isFailure) | ||
| verify(repository).uploadEventProgram(programUid) | ||
| verify(repository, never()).downloadEventProgram(programUid) | ||
| verify(repository, never()).downloadFileResources(programUid) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should sync tracker program successfully when all teis are synced`() = | ||
| runBlocking { | ||
| val programUid = "trackerProgramUid" | ||
| whenever(repository.getProgramType(programUid)).thenReturn(ProgramType.Tracker) | ||
| whenever(repository.allTeisAreSynced(programUid)).thenReturn(true) | ||
|
|
||
| val result = useCase(programUid) | ||
|
|
||
| assertTrue(result.isSuccess) | ||
| verify(repository).uploadTrackerProgram(programUid) | ||
| verify(repository).downloadTrackerProgram(programUid) | ||
| verify(repository).downloadFileResources(programUid) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should not download tracker program when teis are not synced`() = | ||
| runBlocking { | ||
| val programUid = "trackerProgramUid" | ||
| whenever(repository.getProgramType(programUid)).thenReturn(ProgramType.Tracker) | ||
| whenever(repository.allTeisAreSynced(programUid)).thenReturn(false) | ||
|
|
||
| val result = useCase(programUid) | ||
|
|
||
| assertTrue(result.isFailure) | ||
| verify(repository).uploadTrackerProgram(programUid) | ||
| verify(repository, never()).downloadTrackerProgram(programUid) | ||
| verify(repository, never()).downloadFileResources(programUid) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should return failure for unknown program type`() = | ||
| runBlocking { | ||
| val programUid = "unknownProgramUid" | ||
| whenever(repository.getProgramType(programUid)).thenReturn(ProgramType.None) | ||
|
|
||
| val result = useCase(programUid) | ||
|
|
||
| assertTrue(result.isFailure) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should return failure when getProgramType throws`() = | ||
| runBlocking { | ||
| val programUid = "programUid" | ||
| whenever(repository.getProgramType(programUid)).thenThrow(RuntimeException("type error")) | ||
|
|
||
| val result = useCase(programUid) | ||
|
|
||
| assertTrue(result.isFailure) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think something in this method could throw a crash? should he handle the error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think so