- 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)
355 lines
11 KiB
TypeScript
355 lines
11 KiB
TypeScript
// 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';
|
|
|
|
function getToken(): string {
|
|
return localStorage.getItem('auth_token') ?? '';
|
|
}
|
|
|
|
export function setToken(token: string) {
|
|
localStorage.setItem('auth_token', token);
|
|
}
|
|
|
|
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,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Authorization: `Bearer ${getToken()}`
|
|
},
|
|
body: body !== undefined ? JSON.stringify(body) : undefined
|
|
});
|
|
if (!res.ok) {
|
|
const err = await res.json().catch(() => ({ error: res.statusText }));
|
|
throw new ApiError(res.status, err.error ?? res.statusText);
|
|
}
|
|
if (res.status === 204) return undefined as T;
|
|
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,
|
|
message: string
|
|
) {
|
|
super(message);
|
|
}
|
|
}
|
|
|
|
// ─── Types ──────────────────────────────────────────────────────────────────
|
|
|
|
export interface Entry {
|
|
id: string;
|
|
start_time: number; // unix ms UTC
|
|
end_time: number | null;
|
|
auto_stopped: boolean;
|
|
note: string;
|
|
day_key: string;
|
|
updated_at: number;
|
|
}
|
|
|
|
export interface ClosedDay {
|
|
day_key: string;
|
|
start_time: number | null;
|
|
end_time: number | null;
|
|
worked_ms: number;
|
|
kind: 'work' | 'holiday' | 'vacation' | 'sick';
|
|
closed_at: number;
|
|
updated_at: number;
|
|
}
|
|
|
|
export interface ClosedWeek {
|
|
week_key: string;
|
|
expected_ms: number;
|
|
worked_ms: number;
|
|
delta_ms: number;
|
|
closed_at: number;
|
|
updated_at: number;
|
|
}
|
|
|
|
export interface Settings {
|
|
id: string; // UUID
|
|
effective_from: string;
|
|
hours_per_week: number;
|
|
workdays_mask: number;
|
|
timezone: string;
|
|
created_at: number;
|
|
updated_at: number;
|
|
}
|
|
|
|
export interface BalanceAdjustment {
|
|
id: string;
|
|
delta_ms: number;
|
|
note: string;
|
|
effective_at: number; // unix ms
|
|
created_at: number;
|
|
updated_at: number;
|
|
}
|
|
|
|
export interface BalanceSummary {
|
|
total_delta_ms: number;
|
|
weeks_delta_ms: number;
|
|
adjustments_delta_ms: number;
|
|
closed_week_count: number;
|
|
adjustment_count: number;
|
|
}
|
|
|
|
// ─── Entries ─────────────────────────────────────────────────────────────────
|
|
|
|
export const entries = {
|
|
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: 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 ────────────────────────────────────────────────────────────────────
|
|
|
|
export const days = {
|
|
list: (from?: string, to?: string) => {
|
|
const params = new URLSearchParams();
|
|
if (from) params.set('from', from);
|
|
if (to) params.set('to', to);
|
|
return request<ClosedDay[]>('GET', `/days?${params}`);
|
|
},
|
|
close: (dayKey: string) => request<ClosedDay>('POST', `/days/${dayKey}/close`),
|
|
mark: (dayKey: string, kind: 'holiday' | 'vacation' | 'sick') =>
|
|
request<ClosedDay>('POST', `/days/${dayKey}/mark`, { kind }),
|
|
reopen: (dayKey: string) => request<void>('DELETE', `/days/${dayKey}/close`)
|
|
};
|
|
|
|
// ─── Weeks ───────────────────────────────────────────────────────────────────
|
|
|
|
export const weeks = {
|
|
list: (from?: string, to?: string) => {
|
|
const params = new URLSearchParams();
|
|
if (from) params.set('from', from);
|
|
if (to) params.set('to', to);
|
|
return request<ClosedWeek[]>('GET', `/weeks?${params}`);
|
|
},
|
|
close: (weekKey: string) => request<ClosedWeek>('POST', `/weeks/${weekKey}/close`),
|
|
reopen: (weekKey: string) => request<void>('DELETE', `/weeks/${weekKey}/close`),
|
|
balance: () => request<BalanceSummary>('GET', '/weeks/balance')
|
|
};
|
|
|
|
// ─── Balance adjustments ─────────────────────────────────────────────────────
|
|
|
|
export const balance = {
|
|
list: () => request<BalanceAdjustment[]>('GET', '/balance/adjustments'),
|
|
|
|
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 ────────────────────────────────────────────────────────────────
|
|
|
|
export const settings = {
|
|
current: () => request<Settings>('GET', '/settings'),
|
|
history: () => request<Settings[]>('GET', '/settings/history'),
|
|
|
|
upsert: async (body: {
|
|
effective_from: string;
|
|
hours_per_week: number;
|
|
workdays_mask: number;
|
|
timezone: string;
|
|
}): 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;
|
|
}): 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 ──────────────────────────────────────────────────────────────────
|
|
|
|
export const healthz = () =>
|
|
fetch('/healthz').then((r) => r.ok);
|