import { json, error } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; import { setProgress } from '$lib/server/pocketbase'; import { log } from '$lib/server/logger'; /** * POST /api/progress * Body: { slug: string, chapter: number } * Records the user's reading position. * When the user is logged in, progress is keyed by user_id so it syncs across devices. * When anonymous, progress is keyed by the session cookie. */ export const POST: RequestHandler = async ({ request, locals }) => { const body = await request.json().catch(() => null); if (!body || typeof body.slug !== 'string' || typeof body.chapter !== 'number') { error(400, 'Invalid body — expected { slug, chapter }'); } try { await setProgress(locals.sessionId, body.slug, body.chapter, locals.user?.id); } catch (e) { log.error('progress', 'setProgress failed', { slug: body.slug, chapter: body.chapter, err: String(e) }); error(500, 'Failed to save progress'); } return json({ ok: true }); };