Files
TREK/client/tests/helpers/msw/handlers/trips.ts
T
jubnl b194e8317d feat(pwa): implement real offline mode with IndexedDB sync
Add genuine offline read/write capability for trips:

- Dexie IndexedDB schema (trips, places, packing, todo, budget,
  reservations, files, mutationQueue, syncMeta, blobCache)
- Repo layer for all domains: offline reads from Dexie, writes
  optimistically to Dexie and enqueue mutations for later replay
- Mutation queue with UUID idempotency keys (X-Idempotency-Key),
  FIFO flush, temp-ID reconciliation on 2xx, fail-and-continue on 4xx
- Trip sync manager: caches all trips with end_date >= today or null,
  auto-evicts 7d after end_date, fetches bundle endpoint in one request
- Map tile prefetcher: bbox from place coords, zooms 10-16, 50MB cap,
  warms SW cache via fetch
- Sync triggers: network online → flush + syncAll; WS reconnect →
  flush only (rate-limiter safe); visibilitychange/30s → flush only
- WS remoteEventHandler writes through to Dexie on every event
- Server idempotency middleware + idempotency_keys table (migration 100,
  24h TTL nightly cleanup)
- GET /api/trips/:id/bundle endpoint for efficient single-request sync
- OfflineBanner component: amber (offline) / blue (syncing) / hidden
- OfflineTab in Settings: cached trip list, re-sync and clear actions
- usePendingMutations hook for per-item pending indicators

Closes #505 #541
2026-04-14 23:04:25 +02:00

76 lines
2.8 KiB
TypeScript

import { http, HttpResponse } from 'msw';
import { buildTrip, buildDay, buildUser, buildPlace, buildPackingItem, buildTodoItem, buildBudgetItem, buildReservation, buildTripFile } from '../../factories';
export const tripsHandlers = [
// List all trips (active or archived)
http.get('/api/trips', ({ request }) => {
const url = new URL(request.url);
const archived = url.searchParams.get('archived');
if (archived) {
return HttpResponse.json({ trips: [] });
}
const trip1 = buildTrip({ title: 'Paris Adventure', start_date: '2026-07-01', end_date: '2026-07-10' });
const trip2 = buildTrip({ title: 'Tokyo Trip', start_date: '2026-09-01', end_date: '2026-09-15' });
return HttpResponse.json({ trips: [trip1, trip2] });
}),
http.get('/api/trips/:id', ({ params }) => {
const trip = buildTrip({ id: Number(params.id) });
return HttpResponse.json({ trip });
}),
http.get('/api/trips/:id/days', ({ params }) => {
const tripId = Number(params.id);
const day1 = buildDay({ trip_id: tripId, assignments: [], notes_items: [] });
const day2 = buildDay({ trip_id: tripId, assignments: [], notes_items: [] });
return HttpResponse.json({ days: [day1, day2] });
}),
http.put('/api/trips/:id', async ({ params, request }) => {
const body = await request.json() as Record<string, unknown>;
const trip = buildTrip({ id: Number(params.id), ...body });
return HttpResponse.json({ trip });
}),
http.post('/api/trips', async ({ request }) => {
const body = await request.json() as Record<string, unknown>;
const trip = buildTrip({ ...body });
return HttpResponse.json({ trip });
}),
http.get('/api/trips/:id/members', ({ params }) => {
const owner = buildUser();
return HttpResponse.json({ owner, members: [] });
}),
http.get('/api/trips/:id/accommodations', () => {
return HttpResponse.json({ accommodations: [] });
}),
http.get('/api/trips/:id/bundle', ({ params }) => {
const tripId = Number(params.id);
const trip = buildTrip({ id: tripId });
const day = buildDay({ trip_id: tripId, assignments: [], notes_items: [] });
return HttpResponse.json({
trip,
days: [day],
places: [buildPlace({ trip_id: tripId })],
packingItems: [buildPackingItem({ trip_id: tripId })],
todoItems: [buildTodoItem({ trip_id: tripId })],
budgetItems: [buildBudgetItem({ trip_id: tripId })],
reservations: [buildReservation({ trip_id: tripId })],
files: [buildTripFile({ trip_id: tripId })],
});
}),
http.delete('/api/trips/:id', () => {
return HttpResponse.json({ success: true });
}),
http.post('/api/trips/:id/copy', async ({ params, request }) => {
const body = await request.json() as Record<string, unknown>;
const trip = buildTrip({ id: Number(params.id) + 1000, ...body });
return HttpResponse.json({ trip });
}),
];