import { json, error } from '@sveltejs/kit'; import type { RequestHandler } from './$types.js'; import { withTenant } from '$lib/server/rls.js'; import { requireBoardEditor } from '$lib/server/guards.js'; export const PATCH: RequestHandler = async ({ params, request, locals }) => { const tenant = locals.tenant; if (!tenant) throw error(400, 'Unknown tenant'); const body = await request.json(); const field = await withTenant(tenant.id, async (tx) => { await requireBoardEditor(tx, locals.user, params.boardId, tenant.id); const existing = await tx.customFieldDef.findFirst({ where: { id: params.fieldId, boardId: params.boardId } }); if (!existing) throw error(404, 'Field not found'); const data: Record = {}; if (body.name !== undefined) data.name = body.name; if (body.options !== undefined) data.options = body.options; if (body.position !== undefined) data.position = body.position; return tx.customFieldDef.update({ where: { id: params.fieldId }, data }); }); return json({ field }); }; export const DELETE: RequestHandler = async ({ params, locals }) => { const tenant = locals.tenant; if (!tenant) throw error(400, 'Unknown tenant'); await withTenant(tenant.id, async (tx) => { await requireBoardEditor(tx, locals.user, params.boardId, tenant.id); const existing = await tx.customFieldDef.findFirst({ where: { id: params.fieldId, boardId: params.boardId } }); if (!existing) throw error(404, 'Field not found'); await tx.customFieldDef.delete({ where: { id: params.fieldId } }); }); return json({ ok: true }); };