a57f83c5cd
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
81 lines
2.5 KiB
TypeScript
81 lines
2.5 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';
|
|
import { dispatchWebhooks } from '$lib/server/webhooks.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 });
|
|
dispatchWebhooks(tenant.id, 'board:updated', { boardId: params.boardId, board: updated });
|
|
|
|
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 });
|
|
};
|