import { and, eq } from 'drizzle-orm'; import * as z from 'zod'; import { topics } from '~~/drizzle/schema'; import { db } from '~~/server/lib/db'; export default defineEventHandler(async (event) => { await protectRoute(event); const userId = event.context.user!.id; const topicId = getRouterParam(event, 'topicId')!; const result = await readValidatedBody(event, (body) => z .object({ name: z.string().optional(), }) .safeParse(body), ); if (!result.success) { throw createError({ statusCode: 400, message: result.error.issues[0]!.message, }); } const { name } = result.data; const res = await db.update(topics) .set({ name, }) .where( and( eq(topics.id, topicId), eq(topics.userId, userId), ) ); if (res.rowCount === 0) { throw createError({ statusCode: 400, statusMessage: 'Invalid topic', }); } userEvents.emit(userId, 'topics', { op: 'update', payload: { topicId, name, renaming: false, }, }); topicEvents.emit(topicId, { type: 'topic_updated', payload: { topicId, name, renaming: false, }, }); return { ok: true }; });