Private
Public Access
1
0
Files
Kanban/src/routes/api/boards/[boardId]/+server.ts
T
Catherine Renelle e5f2c64e14 Phase 6: Activity log and notifications
Add activity logging to all write API routes, notification system with
real-time delivery via Socket.IO, @mention parsing in comments, activity
feed sidebar on board page, per-card activity in card modal, and
notification bell with unread badge in the navbar.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-26 00:26:15 -05:00

79 lines
2.4 KiB
TypeScript

import { json, error } from '@sveltejs/kit';
import type { RequestHandler } from './$types.js';
import { boardSchema } from '$lib/server/validation.js';
import { withTenant } from '$lib/server/rls.js';
import { requireBoardView, requireBoardEditor, requireBoardOwner } from '$lib/server/guards.js';
import { broadcast } from '$lib/server/realtime.js';
import { logActivity } from '$lib/server/activity.js';
// Get single board with all columns and cards
export const GET: RequestHandler = async ({ params, locals }) => {
const tenant = locals.tenant;
if (!tenant) throw error(400, 'Unknown tenant');
const board = await withTenant(tenant.id, async (tx) => {
return requireBoardView(tx, locals.user, params.boardId, tenant.id);
});
return json({ board });
};
// Update board
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 result = boardSchema.partial().safeParse(body);
if (!result.success) {
return json({ error: result.error.issues[0].message }, { status: 400 });
}
const updated = await withTenant(tenant.id, async (tx) => {
await requireBoardEditor(tx, locals.user, params.boardId, tenant.id);
const board = await tx.board.update({
where: { id: params.boardId },
data: result.data
});
await logActivity(tx, {
boardId: params.boardId,
userId: locals.user!.id,
action: 'board.updated',
metadata: result.data
});
return board;
});
const socketId = request.headers.get('X-Socket-ID');
broadcast(params.boardId, 'board:updated', { board: updated, socketId });
broadcast(params.boardId, 'activity:new', { boardId: params.boardId, socketId });
return json({ board: updated });
};
// Delete board
export const DELETE: RequestHandler = async ({ params, request, locals }) => {
const tenant = locals.tenant;
if (!tenant) throw error(400, 'Unknown tenant');
await withTenant(tenant.id, async (tx) => {
await requireBoardOwner(tx, locals.user, params.boardId, tenant.id);
await logActivity(tx, {
boardId: params.boardId,
userId: locals.user!.id,
action: 'board.deleted'
});
await tx.board.delete({ where: { id: params.boardId } });
});
const socketId = request.headers.get('X-Socket-ID');
broadcast(params.boardId, 'board:deleted', { boardId: params.boardId, socketId });
return json({ ok: true });
};