import { json, error } from '@sveltejs/kit'; import type { RequestHandler } from './$types.js'; import { prisma } from '$lib/server/db.js'; import { boardSchema } from '$lib/server/validation.js'; // List boards for current tenant export const GET: RequestHandler = async ({ locals }) => { const tenant = locals.tenant; if (!tenant) throw error(400, 'Unknown tenant'); const where: Record = { tenantId: tenant.id, archived: false }; // If not logged in, only show public boards if (!locals.user) { where.visibility = 'PUBLIC'; } else { // Show boards user is a member of + public boards where.OR = [ { visibility: 'PUBLIC' }, { members: { some: { userId: locals.user.id } } } ]; delete where.visibility; } const boards = await prisma.board.findMany({ where, include: { members: { include: { user: { select: { id: true, name: true, avatarUrl: true } } } }, _count: { select: { columns: true } } }, orderBy: { updatedAt: 'desc' } }); return json({ boards }); }; // Create a new board export const POST: RequestHandler = async ({ request, locals }) => { const tenant = locals.tenant; if (!tenant) throw error(400, 'Unknown tenant'); if (!locals.user) throw error(401, 'Not authenticated'); const body = await request.json(); const result = boardSchema.safeParse(body); if (!result.success) { return json({ error: result.error.issues[0].message }, { status: 400 }); } const { title, description, visibility } = result.data; const board = await prisma.board.create({ data: { tenantId: tenant.id, title, description, visibility, members: { create: { userId: locals.user.id, role: 'OWNER' } } }, include: { members: { include: { user: { select: { id: true, name: true, avatarUrl: true } } } } } }); return json({ board }, { status: 201 }); };