Added tests, shift 'delete'. Fixed overnight shifts, sync, error handling.
This commit is contained in:
parent
9d0fa1f0af
commit
f9c4facad6
21 changed files with 2522 additions and 40 deletions
|
|
@ -40,6 +40,9 @@ export async function syncPull() {
|
|||
}
|
||||
if (data.volunteer_shifts?.length) {
|
||||
await db.volunteer_shifts.bulkPut(data.volunteer_shifts)
|
||||
const deleted = data.volunteer_shifts.filter(vs => vs.deleted_at)
|
||||
.map(vs => [vs.volunteer_id, vs.shift_id])
|
||||
if (deleted.length) await db.volunteer_shifts.bulkDelete(deleted)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
|
@ -65,27 +68,30 @@ export function startSSE(onEvent) {
|
|||
|
||||
sseSource = new EventSource(`/api/sync/stream?token=${encodeURIComponent(session.token)}`)
|
||||
|
||||
sseSource.onmessage = (e) => {
|
||||
sseSource.onmessage = async (e) => {
|
||||
try {
|
||||
const payload = JSON.parse(e.data)
|
||||
if (payload.event === 'checkin') {
|
||||
// Apply check-in to local Dexie immediately
|
||||
if (payload.data?.type === 'attendee' && payload.data?.attendee) {
|
||||
db.attendees.put(payload.data.attendee)
|
||||
await db.attendees.put(payload.data.attendee)
|
||||
}
|
||||
if (payload.data?.type === 'volunteer' && payload.data?.volunteer) {
|
||||
db.volunteers.put(payload.data.volunteer)
|
||||
await db.volunteers.put(payload.data.volunteer)
|
||||
}
|
||||
onEvent?.(payload)
|
||||
}
|
||||
} catch {}
|
||||
} catch (err) {
|
||||
console.warn('SSE message error:', err.message)
|
||||
}
|
||||
}
|
||||
|
||||
sseSource.onerror = () => {
|
||||
sseSource?.close()
|
||||
sseSource = null
|
||||
// Reconnect after 5s
|
||||
setTimeout(connect, 5000)
|
||||
setTimeout(() => {
|
||||
connect()
|
||||
syncPull()
|
||||
}, 5000)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue