refactor: syncAll pull-fase gebruikt mergeMeasurements als canonieke merge (issue #3)
This commit is contained in:
parent
edbf69d6d9
commit
e3960a389e
1 changed files with 19 additions and 36 deletions
|
|
@ -112,48 +112,31 @@ export async function syncAll(): Promise<SyncResult> {
|
|||
|
||||
if (remote.length > 0) {
|
||||
const local = await getAllMeasurementsForSync()
|
||||
const merged = mergeMeasurements(local, remote)
|
||||
|
||||
const localMap = new Map(local.map(m => [m.id, m]))
|
||||
|
||||
for (const r of remote) {
|
||||
const localRec = localMap.get(r.id)
|
||||
const isDeleted = !!(r as any).deleted
|
||||
|
||||
if (isDeleted) {
|
||||
if (localRec) {
|
||||
if (
|
||||
localRec.syncStatus === 'pending' ||
|
||||
localRec.syncStatus === 'pending-delete'
|
||||
) {
|
||||
continue
|
||||
}
|
||||
await removeMeasurement(r.id)
|
||||
deleted++
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
const localIsPending =
|
||||
localRec?.syncStatus === 'pending' ||
|
||||
localRec?.syncStatus === 'pending-delete'
|
||||
|
||||
if (localIsPending) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (localRec) {
|
||||
if (new Date(r.updatedAt) > new Date(localRec.updatedAt)) {
|
||||
const record = { ...r, deletedAt: (r as any).deletedAt || undefined } as Measurement
|
||||
await upsertMeasurement(record)
|
||||
await updateSyncStatus(r.id, 'synced')
|
||||
for (const m of merged) {
|
||||
if (m.deletedAt) {
|
||||
await removeMeasurement(m.id)
|
||||
const localRec = localMap.get(m.id)
|
||||
if (localRec && !localRec.deletedAt) {
|
||||
pulled++
|
||||
}
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
const localRec = localMap.get(m.id)
|
||||
const isNew = !localRec
|
||||
const isUpdated =
|
||||
localRec &&
|
||||
new Date(m.updatedAt).getTime() !==
|
||||
new Date(localRec.updatedAt).getTime()
|
||||
|
||||
const record = { ...r, deletedAt: (r as any).deletedAt || undefined } as Measurement
|
||||
await upsertMeasurement(record)
|
||||
await updateSyncStatus(r.id, 'synced')
|
||||
pulled++
|
||||
if (isNew || isUpdated) {
|
||||
await upsertMeasurement(m)
|
||||
await updateSyncStatus(m.id, 'synced')
|
||||
pulled++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue