import { createSkinSnapshot, deleteSkinSnapshot, getSkinSnapshots, updateSkinSnapshot } from '$lib/api'; import { fail } from '@sveltejs/kit'; import type { Actions, PageServerLoad } from './$types'; export const load: PageServerLoad = async ({ url }) => { const overall_state = url.searchParams.get('overall_state') ?? undefined; const snapshots = await getSkinSnapshots({ overall_state }); return { snapshots, overall_state }; }; export const actions: Actions = { create: async ({ request }) => { const form = await request.formData(); const snapshot_date = form.get('snapshot_date') as string; const overall_state = form.get('overall_state') as string; const texture = form.get('texture') as string; const notes = form.get('notes') as string; const hydration_level = form.get('hydration_level') as string; const sensitivity_level = form.get('sensitivity_level') as string; const barrier_state = form.get('barrier_state') as string; const active_concerns_raw = form.get('active_concerns') as string; const priorities_raw = form.get('priorities') as string; if (!snapshot_date) { return fail(400, { error: 'Date is required' }); } const active_concerns = active_concerns_raw ?.split(',') .map((c) => c.trim()) .filter(Boolean) ?? []; const priorities = priorities_raw ?.split(',') .map((p) => p.trim()) .filter(Boolean) ?? []; const skin_type = form.get('skin_type') as string; const sebum_tzone = form.get('sebum_tzone') as string; const sebum_cheeks = form.get('sebum_cheeks') as string; const body: Record = { snapshot_date, active_concerns, priorities }; if (overall_state) body.overall_state = overall_state; if (texture) body.texture = texture; if (notes) body.notes = notes; if (hydration_level) body.hydration_level = Number(hydration_level); if (sensitivity_level) body.sensitivity_level = Number(sensitivity_level); if (barrier_state) body.barrier_state = barrier_state; if (skin_type) body.skin_type = skin_type; if (sebum_tzone) body.sebum_tzone = Number(sebum_tzone); if (sebum_cheeks) body.sebum_cheeks = Number(sebum_cheeks); try { await createSkinSnapshot(body); return { created: true }; } catch (e) { return fail(500, { error: (e as Error).message }); } }, update: async ({ request }) => { const form = await request.formData(); const id = form.get('id') as string; const snapshot_date = form.get('snapshot_date') as string; const overall_state = form.get('overall_state') as string; const texture = form.get('texture') as string; const notes = form.get('notes') as string; const hydration_level = form.get('hydration_level') as string; const sensitivity_level = form.get('sensitivity_level') as string; const barrier_state = form.get('barrier_state') as string; const active_concerns_raw = form.get('active_concerns') as string; const priorities_raw = form.get('priorities') as string; const skin_type = form.get('skin_type') as string; const sebum_tzone = form.get('sebum_tzone') as string; const sebum_cheeks = form.get('sebum_cheeks') as string; if (!id) return fail(400, { error: 'Missing id' }); const active_concerns = active_concerns_raw ?.split(',') .map((c) => c.trim()) .filter(Boolean) ?? []; const priorities = priorities_raw ?.split(',') .map((p) => p.trim()) .filter(Boolean) ?? []; const body: Record = { active_concerns, priorities }; if (snapshot_date) body.snapshot_date = snapshot_date; if (overall_state) body.overall_state = overall_state; if (texture) body.texture = texture; if (notes) body.notes = notes; if (hydration_level) body.hydration_level = Number(hydration_level); if (sensitivity_level) body.sensitivity_level = Number(sensitivity_level); if (barrier_state) body.barrier_state = barrier_state; if (skin_type) body.skin_type = skin_type; if (sebum_tzone) body.sebum_tzone = Number(sebum_tzone); if (sebum_cheeks) body.sebum_cheeks = Number(sebum_cheeks); try { await updateSkinSnapshot(id, body); return { updated: true }; } catch (e) { return fail(500, { error: (e as Error).message }); } }, delete: async ({ request }) => { const form = await request.formData(); const id = form.get('id') as string; if (!id) return fail(400, { error: 'Missing id' }); try { await deleteSkinSnapshot(id); return { deleted: true }; } catch (e) { return fail(500, { error: (e as Error).message }); } } };