45 lines
1.0 KiB
TypeScript
45 lines
1.0 KiB
TypeScript
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 result = await readValidatedBody(event, (body) =>
|
|
z
|
|
.object({
|
|
id: z.string(),
|
|
name: z.string(),
|
|
agentId: z.string(),
|
|
})
|
|
.safeParse(body),
|
|
);
|
|
|
|
if (!result.success) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: result.error.issues[0]!.message,
|
|
});
|
|
}
|
|
|
|
const { id, name, agentId } = result.data;
|
|
|
|
const res = await db.insert(topics).values({
|
|
id,
|
|
userId,
|
|
name,
|
|
agentId,
|
|
createdAt: new Date(),
|
|
});
|
|
|
|
if (res.rowCount === 0) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Failed to create topic',
|
|
});
|
|
}
|
|
|
|
return { ok: true };
|
|
}); |