import type { PageServerLoad } from './$types.js'; import { withTenant } from '$lib/server/rls.js'; import { prisma } from '$lib/server/db.js'; export const load: PageServerLoad = async ({ locals, url }) => { const tenant = locals.tenant; if (!tenant) return { boards: [], categories: [], templates: [] }; const showArchived = url.searchParams.get('archived') === 'true'; const result = await withTenant(tenant.id, async (tx) => { const where: any = { tenantId: tenant.id }; if (!showArchived) { where.archived = false; } if (!locals.user) { where.visibility = 'PUBLIC'; } else { where.OR = [ { visibility: 'PUBLIC' }, { members: { some: { userId: locals.user.id } } } ]; } const [boards, categories] = await Promise.all([ tx.board.findMany({ where, include: { members: { include: { user: { select: { id: true, name: true, avatarUrl: true } } } }, category: true, _count: { select: { columns: true } } }, orderBy: { updatedAt: 'desc' } }), locals.user ? tx.category.findMany({ where: { tenantId: tenant.id }, orderBy: { name: 'asc' } }) : [] ]); return { boards, categories }; }); // Fetch templates (system + tenant) — only if logged in let templates: any[] = []; if (locals.user) { const [systemTemplates, tenantTemplates] = await Promise.all([ prisma.boardTemplate.findMany({ where: { tenantId: null }, orderBy: { name: 'asc' } }), withTenant(tenant.id, async (tx) => { return tx.boardTemplate.findMany({ where: { tenantId: tenant.id }, orderBy: { name: 'asc' } }); }) ]); templates = [...systemTemplates, ...tenantTemplates]; } return { ...result, templates }; };