Add sync redesign with offline fallback (M9)

- Migration 003: adds logged_at to sync_log for TTL pruning; migrates
  settings_history to UUID TEXT PK with updated_at column
- SyncStore: Prune() deletes rows older than 30d and writes a '_pruned'
  marker at the boundary version; Pull() calls Prune lazily and returns
  ErrSyncStale (410) when the client's since_version is behind the marker
- sync_handler.go: GET /api/sync/pull?since=N; POST /api/sync/push with
  last-updated_at-wins conflict resolution for entries, balance_adjustments,
  settings_history; closed_days/closed_weeks skipped (server-only mutations)
- router.go: passes entryStore, adjustmentStore, settingsStore to SyncHandler
- settings_store.go: UUID PK, updated_at column, Upsert() for push path
- settings_service.go: generates UUID on create, sets updated_at on update
- settings_handler.go: ID params changed from int64 to string
- domain.go: Settings.ID string, Settings.UpdatedAt added
- client.ts: all mutation methods catch TypeError (offline) and fall back
  to Dexie write + outbox enqueue; crypto.randomUUID() for offline creates;
  Settings.id type changed to string
- db.ts: Dexie v3 — settings_history key path changed to string UUID;
  upgrade handler clears table for repopulation via pull
- sync.ts: real pushOutbox to POST /api/sync/push; pullChanges uses GET
  with ?since=N; 410 triggers coldStart() + retry; coldStart() wipes all
  tables and resets last_version
- 4 new Go store tests covering normal pull, stale client, empty prune,
  client-ahead-of-marker; all tests pass (store + service, 19 Vitest)
This commit is contained in:
2026-04-30 22:50:33 +02:00
parent 3214f48a6f
commit d8366f5c25
15 changed files with 864 additions and 144 deletions

View File

@@ -1,5 +1,11 @@
// API client for Wotra backend.
// Base URL: /api (relative, works both in dev proxy and production)
//
// Offline fallback: if a mutation throws a network error (TypeError: Failed to fetch),
// the call enqueues the operation in the Dexie outbox and resolves with the local object.
// The background sync loop will push the outbox to the server when connectivity returns.
import { db } from '$lib/stores/db';
const API_BASE = '/api';
@@ -15,6 +21,10 @@ export function hasToken(): boolean {
return !!localStorage.getItem('auth_token');
}
function isNetworkError(e: unknown): boolean {
return e instanceof TypeError && e.message.toLowerCase().includes('fetch');
}
async function request<T>(method: string, path: string, body?: unknown): Promise<T> {
const res = await fetch(`${API_BASE}${path}`, {
method,
@@ -32,6 +42,17 @@ async function request<T>(method: string, path: string, body?: unknown): Promise
return res.json();
}
/** Enqueue an outbox item for offline push. */
async function enqueue(entity: string, entity_id: string, op: 'upsert' | 'delete', payload: unknown) {
await db.outbox.add({
entity,
entity_id,
op,
payload: JSON.stringify(payload),
created_at: Date.now()
});
}
export class ApiError extends Error {
constructor(
public status: number,
@@ -73,12 +94,13 @@ export interface ClosedWeek {
}
export interface Settings {
id: number;
id: string; // UUID
effective_from: string;
hours_per_week: number;
workdays_mask: number;
timezone: string;
created_at: number;
updated_at: number;
}
export interface BalanceAdjustment {
@@ -101,19 +123,90 @@ export interface BalanceSummary {
// ─── Entries ─────────────────────────────────────────────────────────────────
export const entries = {
start: (note = '') => request<Entry>('POST', '/entries/start', { note }),
createInterval: (startTime: number, endTime: number, note = '') =>
request<Entry>('POST', '/entries', { start_time: startTime, end_time: endTime, note }),
stop: (id: string) => request<Entry>('POST', `/entries/${id}/stop`),
start: async (note = ''): Promise<Entry> => {
try {
return await request<Entry>('POST', '/entries/start', { note });
} catch (e) {
if (!isNetworkError(e)) throw e;
const entry: Entry = {
id: crypto.randomUUID(),
start_time: Date.now(),
end_time: null,
auto_stopped: false,
note,
day_key: new Date().toISOString().slice(0, 10),
updated_at: Date.now()
};
await db.entries.put(entry);
await enqueue('entries', entry.id, 'upsert', entry);
return entry;
}
},
createInterval: async (startTime: number, endTime: number, note = ''): Promise<Entry> => {
try {
return await request<Entry>('POST', '/entries', { start_time: startTime, end_time: endTime, note });
} catch (e) {
if (!isNetworkError(e)) throw e;
const entry: Entry = {
id: crypto.randomUUID(),
start_time: startTime,
end_time: endTime,
auto_stopped: false,
note,
day_key: new Date(startTime).toISOString().slice(0, 10),
updated_at: Date.now()
};
await db.entries.put(entry);
await enqueue('entries', entry.id, 'upsert', entry);
return entry;
}
},
stop: async (id: string): Promise<Entry> => {
try {
return await request<Entry>('POST', `/entries/${id}/stop`);
} catch (e) {
if (!isNetworkError(e)) throw e;
const existing = await db.entries.get(id);
if (!existing) throw e;
const updated: Entry = { ...existing, end_time: Date.now(), updated_at: Date.now() };
await db.entries.put(updated);
await enqueue('entries', id, 'upsert', updated);
return updated;
}
},
list: (from?: string, to?: string) => {
const params = new URLSearchParams();
if (from) params.set('from', from);
if (to) params.set('to', to);
return request<Entry[]>('GET', `/entries?${params}`);
},
update: (id: string, body: { start_time?: number; end_time?: number; note?: string }) =>
request<Entry>('PUT', `/entries/${id}`, body),
delete: (id: string) => request<void>('DELETE', `/entries/${id}`)
update: async (id: string, body: { start_time?: number; end_time?: number; note?: string }): Promise<Entry> => {
try {
return await request<Entry>('PUT', `/entries/${id}`, body);
} catch (e) {
if (!isNetworkError(e)) throw e;
const existing = await db.entries.get(id);
if (!existing) throw e;
const updated: Entry = { ...existing, ...body, updated_at: Date.now() };
await db.entries.put(updated);
await enqueue('entries', id, 'upsert', updated);
return updated;
}
},
delete: async (id: string): Promise<void> => {
try {
return await request<void>('DELETE', `/entries/${id}`);
} catch (e) {
if (!isNetworkError(e)) throw e;
await db.entries.delete(id);
await enqueue('entries', id, 'delete', { id, updated_at: Date.now() });
}
}
};
// ─── Days ────────────────────────────────────────────────────────────────────
@@ -149,11 +242,51 @@ export const weeks = {
export const balance = {
list: () => request<BalanceAdjustment[]>('GET', '/balance/adjustments'),
create: (body: { delta_ms: number; note?: string; effective_at?: number }) =>
request<BalanceAdjustment>('POST', '/balance/adjustments', body),
update: (id: string, body: { delta_ms: number; note?: string; effective_at?: number }) =>
request<BalanceAdjustment>('PUT', `/balance/adjustments/${id}`, body),
delete: (id: string) => request<void>('DELETE', `/balance/adjustments/${id}`)
create: async (body: { delta_ms: number; note?: string; effective_at?: number }): Promise<BalanceAdjustment> => {
try {
return await request<BalanceAdjustment>('POST', '/balance/adjustments', body);
} catch (e) {
if (!isNetworkError(e)) throw e;
const now = Date.now();
const adj: BalanceAdjustment = {
id: crypto.randomUUID(),
delta_ms: body.delta_ms,
note: body.note ?? '',
effective_at: body.effective_at ?? now,
created_at: now,
updated_at: now
};
await db.balance_adjustments.put(adj);
await enqueue('balance_adjustments', adj.id, 'upsert', adj);
return adj;
}
},
update: async (id: string, body: { delta_ms: number; note?: string; effective_at?: number }): Promise<BalanceAdjustment> => {
try {
return await request<BalanceAdjustment>('PUT', `/balance/adjustments/${id}`, body);
} catch (e) {
if (!isNetworkError(e)) throw e;
const existing = await db.balance_adjustments.get(id);
if (!existing) throw e;
const updated: BalanceAdjustment = { ...existing, ...body, updated_at: Date.now() };
await db.balance_adjustments.put(updated);
await enqueue('balance_adjustments', id, 'upsert', updated);
return updated;
}
},
delete: async (id: string): Promise<void> => {
try {
return await request<void>('DELETE', `/balance/adjustments/${id}`);
} catch (e) {
if (!isNetworkError(e)) throw e;
const existing = await db.balance_adjustments.get(id);
await db.balance_adjustments.delete(id);
await enqueue('balance_adjustments', id, 'delete', { id, updated_at: existing?.updated_at ?? Date.now() });
}
}
};
// ─── Settings ────────────────────────────────────────────────────────────────
@@ -161,19 +294,58 @@ export const balance = {
export const settings = {
current: () => request<Settings>('GET', '/settings'),
history: () => request<Settings[]>('GET', '/settings/history'),
upsert: (body: {
upsert: async (body: {
effective_from: string;
hours_per_week: number;
workdays_mask: number;
timezone: string;
}) => request<Settings>('PUT', '/settings', body),
update: (id: number, body: {
}): Promise<Settings> => {
try {
return await request<Settings>('PUT', '/settings', body);
} catch (e) {
if (!isNetworkError(e)) throw e;
const now = Date.now();
const s: Settings = {
id: crypto.randomUUID(),
...body,
created_at: now,
updated_at: now
};
await db.settings_history.put(s);
await enqueue('settings_history', s.id, 'upsert', s);
return s;
}
},
update: async (id: string, body: {
effective_from: string;
hours_per_week: number;
workdays_mask: number;
timezone: string;
}) => request<Settings>('PUT', `/settings/history/${id}`, body),
delete: (id: number) => request<void>('DELETE', `/settings/history/${id}`)
}): Promise<Settings> => {
try {
return await request<Settings>('PUT', `/settings/history/${id}`, body);
} catch (e) {
if (!isNetworkError(e)) throw e;
const existing = await db.settings_history.get(id);
if (!existing) throw e;
const updated: Settings = { ...existing, ...body, updated_at: Date.now() };
await db.settings_history.put(updated);
await enqueue('settings_history', id, 'upsert', updated);
return updated;
}
},
delete: async (id: string): Promise<void> => {
try {
return await request<void>('DELETE', `/settings/history/${id}`);
} catch (e) {
if (!isNetworkError(e)) throw e;
await db.settings_history.delete(id);
await enqueue('settings_history', id, 'delete', { id, updated_at: Date.now() });
}
}
};
// ─── Health ──────────────────────────────────────────────────────────────────

View File

@@ -3,7 +3,7 @@ import type { Entry, ClosedDay, ClosedWeek, Settings, BalanceAdjustment } from '
export interface OutboxItem {
id?: number; // auto-increment
entity: string; // 'entries' | 'closed_days' | 'closed_weeks' | 'settings' | 'balance_adjustments'
entity: string; // 'entries' | 'closed_days' | 'closed_weeks' | 'settings_history' | 'balance_adjustments'
entity_id: string;
op: 'upsert' | 'delete';
payload: string; // JSON
@@ -14,7 +14,7 @@ export class WotraDB extends Dexie {
entries!: Table<Entry, string>;
closed_days!: Table<ClosedDay, string>;
closed_weeks!: Table<ClosedWeek, string>;
settings_history!: Table<Settings, number>;
settings_history!: Table<Settings, string>; // UUID PK as of v3
balance_adjustments!: Table<BalanceAdjustment, string>;
outbox!: Table<OutboxItem, number>;
meta!: Table<{ key: string; value: string }, string>;
@@ -32,6 +32,13 @@ export class WotraDB extends Dexie {
this.version(2).stores({
balance_adjustments: 'id, effective_at, updated_at'
});
// v3: settings_history switches from integer autoincrement PK to UUID TEXT PK.
// Clear the table on upgrade; the next pull will repopulate it from the server.
this.version(3).stores({
settings_history: 'id, effective_from, updated_at'
}).upgrade(tx => {
return tx.table('settings_history').clear();
});
}
}

View File

@@ -1,10 +1,15 @@
/**
* Sync layer: push local outbox items to server, pull server changes.
* Uses last-write-wins based on updated_at.
* Sync layer: push local outbox to server, pull server changes.
*
* Online-first, offline-fallback:
* - Mutations go directly to the server via REST; on network error they are
* written to Dexie + outbox by the API client.
* - This loop pushes any queued outbox items, then pulls new server changes.
* - On 410 Gone the client is stale: wipe all tables and re-pull from 0.
*/
import { db, getLastVersion, setLastVersion } from './db';
import type { OutboxItem } from './db';
import { setToken, hasToken } from '$lib/api/client';
import { hasToken } from '$lib/api/client';
const API = '/api';
@@ -16,42 +21,69 @@ function headers() {
};
}
// ─── Push ─────────────────────────────────────────────────────────────────────
export async function pushOutbox(): Promise<void> {
if (!hasToken()) return;
const items = await db.outbox.toArray();
if (items.length === 0) return;
const res = await fetch(`${API}/sync/push`, {
method: 'POST',
headers: headers(),
body: JSON.stringify({ changes: items.map((i) => ({ ...JSON.parse(i.payload), _op: i.op, _entity: i.entity })) })
});
if (!res.ok) return; // will retry on next sync
let res: Response;
try {
res = await fetch(`${API}/sync/push`, {
method: 'POST',
headers: headers(),
body: JSON.stringify({
changes: items.map((i) => ({
entity: i.entity,
entity_id: i.entity_id,
op: i.op,
payload: JSON.parse(i.payload)
}))
})
});
} catch {
return; // network unavailable; retry next cycle
}
if (!res.ok) return;
const { applied } = await res.json() as { applied: string[]; conflicts: string[] };
// Remove applied items from outbox
const appliedIds = new Set(applied);
const toDelete = items.filter((i) => i.entity_id && appliedIds.has(i.entity_id)).map((i) => i.id!);
const { applied } = (await res.json()) as { applied: string[]; skipped: string[] };
const appliedSet = new Set(applied);
const toDelete = items
.filter((i) => appliedSet.has(i.entity_id))
.map((i) => i.id!);
if (toDelete.length > 0) await db.outbox.bulkDelete(toDelete);
}
// ─── Pull ─────────────────────────────────────────────────────────────────────
export async function pullChanges(): Promise<void> {
if (!hasToken()) return;
const since = await getLastVersion();
const res = await fetch(`${API}/sync/pull`, {
method: 'POST',
headers: headers(),
body: JSON.stringify({ since_version: since })
});
let res: Response;
try {
res = await fetch(`${API}/sync/pull?since=${since}`, { headers: headers() });
} catch {
return; // network unavailable
}
if (res.status === 410) {
// Server has pruned data the client hasn't seen — full re-sync.
await coldStart();
return pullChanges();
}
if (!res.ok) return;
const { changes, server_version } = await res.json() as {
const { changes, server_version } = (await res.json()) as {
changes: Array<{ entity: string; entity_id: string; op: string; payload: string }>;
server_version: number;
};
for (const change of changes) {
const data = JSON.parse(change.payload);
const data = typeof change.payload === 'string'
? JSON.parse(change.payload)
: change.payload;
if (change.op === 'delete') {
await applyDelete(change.entity, change.entity_id);
} else {
@@ -61,31 +93,50 @@ export async function pullChanges(): Promise<void> {
await setLastVersion(server_version);
}
// ─── Cold start ───────────────────────────────────────────────────────────────
/** Wipe all local tables and reset version so the next pull fetches everything. */
export async function coldStart(): Promise<void> {
await Promise.all([
db.entries.clear(),
db.closed_days.clear(),
db.closed_weeks.clear(),
db.settings_history.clear(),
db.balance_adjustments.clear()
]);
await setLastVersion(0);
}
// ─── Apply helpers ────────────────────────────────────────────────────────────
async function applyUpsert(entity: string, data: unknown) {
switch (entity) {
case 'entries': await db.entries.put(data as any); break;
case 'closed_days': await db.closed_days.put(data as any); break;
case 'closed_weeks': await db.closed_weeks.put(data as any); break;
case 'settings_history': await db.settings_history.put(data as any); break;
case 'entries': await db.entries.put(data as any); break;
case 'closed_days': await db.closed_days.put(data as any); break;
case 'closed_weeks': await db.closed_weeks.put(data as any); break;
case 'settings_history': await db.settings_history.put(data as any); break;
case 'balance_adjustments': await db.balance_adjustments.put(data as any); break;
}
}
async function applyDelete(entity: string, id: string) {
switch (entity) {
case 'entries': await db.entries.delete(id); break;
case 'closed_days': await db.closed_days.delete(id); break;
case 'closed_weeks': await db.closed_weeks.delete(id); break;
case 'entries': await db.entries.delete(id); break;
case 'closed_days': await db.closed_days.delete(id); break;
case 'closed_weeks': await db.closed_weeks.delete(id); break;
case 'settings_history': await db.settings_history.delete(id); break;
case 'balance_adjustments': await db.balance_adjustments.delete(id); break;
}
}
// ─── Sync loop ────────────────────────────────────────────────────────────────
let syncInterval: ReturnType<typeof setInterval> | null = null;
/** Start background sync loop (every 30 seconds). */
export function startSync() {
if (syncInterval) return;
sync(); // immediate
sync(); // immediate first run
syncInterval = setInterval(sync, 30_000);
}
@@ -101,6 +152,6 @@ async function sync() {
await pushOutbox();
await pullChanges();
} catch {
// Network unavailable — will retry
// Unexpected error — will retry on next interval.
}
}

View File

@@ -19,7 +19,7 @@
let formTimezone = $state('UTC');
// Inline edit state for history rows
let editingId = $state<number | null>(null);
let editingId = $state<string | null>(null);
let editEffectiveFrom = $state('');
let editHoursPerWeek = $state(0);
let editWorkdaysMask = $state(31);
@@ -109,7 +109,7 @@
editError = '';
}
async function saveEdit(id: number) {
async function saveEdit(id: string) {
editError = '';
try {
await settings.update(id, {
@@ -125,7 +125,7 @@
}
}
async function handleDelete(id: number) {
async function handleDelete(id: string) {
error = '';
try {
await settings.delete(id);