import { desc, eq, like, or, sql } from 'drizzle-orm' import { useDB, schema } from '../../utils/db.js' import { requireAdmin } from '../../utils/auth.js' import { parsePagination } from '../../utils/pagination.js' export default defineEventHandler(async (event) => { requireAdmin(event) const db = useDB() const query = getQuery(event) const { page, limit, offset, search } = parsePagination(event) const type = query.type || '' const conditions = [] if (type) conditions.push(eq(schema.submissions.type, type)) if (search) conditions.push(like(schema.submissions.data, `%${search}%`)) const whereClause = conditions.length ? conditions.reduce((a, b) => sql`${a} AND ${b}`) : sql`1=1` const [{ count: total }] = db .select({ count: sql`count(*)` }) .from(schema.submissions) .where(whereClause) .all() const list = db .select() .from(schema.submissions) .where(whereClause) .orderBy(desc(schema.submissions.createdAt)) .limit(limit) .offset(offset) .all() const items = list.map(item => ({ ...item, data: JSON.parse(item.data), })) return { items, total, page, limit, totalPages: Math.ceil(total / limit) } })