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) {
|
if (remote.length > 0) {
|
||||||
const local = await getAllMeasurementsForSync()
|
const local = await getAllMeasurementsForSync()
|
||||||
|
const merged = mergeMeasurements(local, remote)
|
||||||
|
|
||||||
const localMap = new Map(local.map(m => [m.id, m]))
|
const localMap = new Map(local.map(m => [m.id, m]))
|
||||||
|
|
||||||
for (const r of remote) {
|
for (const m of merged) {
|
||||||
const localRec = localMap.get(r.id)
|
if (m.deletedAt) {
|
||||||
const isDeleted = !!(r as any).deleted
|
await removeMeasurement(m.id)
|
||||||
|
const localRec = localMap.get(m.id)
|
||||||
if (isDeleted) {
|
if (localRec && !localRec.deletedAt) {
|
||||||
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')
|
|
||||||
pulled++
|
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
|
if (isNew || isUpdated) {
|
||||||
await upsertMeasurement(record)
|
await upsertMeasurement(m)
|
||||||
await updateSyncStatus(r.id, 'synced')
|
await updateSyncStatus(m.id, 'synced')
|
||||||
pulled++
|
pulled++
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue