import { and, eq, isNull, lte, or } from "drizzle-orm"; 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 as string; // Expired leases also recover jobs interrupted by a server restart. await db.update(topics).set({ renaming: false, renameAttemptId: null, renameExpiresAt: null, }).where(and( eq(topics.userId, userId), eq(topics.renaming, true), or(isNull(topics.renameExpiresAt), lte(topics.renameExpiresAt, new Date())), )); const agents = await db.query.agents.findMany({ where: { userId, }, orderBy: { createdAt: 'asc', }, with: { topics: { orderBy: { createdAt: 'desc', }, } } }); return agents; })