Coverage Summary for Class: BudgetSyncPlugin (com.georgeci.moneysurfer.data.sync.plugin)
| Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
| BudgetSyncPlugin |
100%
(3/3)
|
83.3%
(5/6)
|
100%
(27/27)
|
99.2%
(255/257)
|
| BudgetSyncPlugin$applyDoc$1 |
|
| BudgetSyncPlugin$push$2 |
100%
(1/1)
|
50%
(2/4)
|
100%
(3/3)
|
100%
(43/43)
|
| Total |
100%
(4/4)
|
70%
(7/10)
|
100%
(30/30)
|
99.3%
(298/300)
|
package com.georgeci.moneysurfer.data.sync.plugin
import com.georgeci.moneysurfer.data.db.dao.BudgetDao
import com.georgeci.moneysurfer.data.remote.BudgetDoc
import com.georgeci.moneysurfer.data.sync.WorkspaceDocumentWriter
import com.georgeci.moneysurfer.data.sync.toDoc
import com.georgeci.moneysurfer.data.sync.toEntity
import com.georgeci.moneysurfer.domain.AppInfo
import com.georgeci.moneysurfer.domain.sync.SyncEntityTypes
import com.georgeci.moneysurfer.sync.api.SyncCollection
import com.georgeci.moneysurfer.sync.plugin.EntityApplyResult
import com.georgeci.moneysurfer.sync.plugin.RemoteDocument
import com.georgeci.moneysurfer.sync.plugin.SyncEntityPlugin
import com.georgeci.moneysurfer.sync.repository.ConflictMetadata
import com.georgeci.moneysurfer.sync.repository.ConflictResolver
import com.georgeci.moneysurfer.sync.repository.PendingMutation
import org.koin.core.annotation.Single
import kotlin.time.Instant
@Single(binds = [SyncEntityPlugin::class])
class BudgetSyncPlugin(
private val writer: WorkspaceDocumentWriter,
private val appInfo: AppInfo,
private val conflictResolver: ConflictResolver,
private val budgetDao: BudgetDao,
) : SyncEntityPlugin {
override val entityType: String = SyncEntityTypes.BUDGET
override val firestoreCollectionName: String = SyncCollection.BUDGETS
override val pullPriority: Int = SyncPullPriorities.BUDGETS
override suspend fun push(mutation: PendingMutation) = writer.pushToCollection(
mutation = mutation,
collectionName = SyncCollection.BUDGETS,
clientVersionCode = appInfo.versionCode,
strategy = BudgetDoc.serializer(),
) {
budgetDao.getById(mutation.entityId)
?.toDoc()
?.copy(clientVersionCode = appInfo.versionCode)
}
override suspend fun applyDoc(doc: RemoteDocument, scopeKey: String): EntityApplyResult {
val dto = doc.decodeOrNull(BudgetDoc.serializer()) ?: return SKIPPED_APPLY_RESULT
if (dto.deletedAt != null) {
budgetDao.delete(doc.id)
return EntityApplyResult(applied = true, wasConflict = false)
}
val local = budgetDao.getById(doc.id)
val resolution = conflictResolver.resolve(
local = local,
remote = dto.toEntity(id = doc.id, workspaceId = scopeKey),
metadata = ConflictMetadata(
entityType = SyncEntityTypes.BUDGET,
entityId = doc.id,
localUpdatedAt = local?.updatedAt?.let(Instant::fromEpochMilliseconds),
remoteUpdatedAt = Instant.fromEpochMilliseconds(dto.updatedAt),
),
)
return applyResolution(resolution) { budgetDao.upsertAll(listOf(it)) }
}
}