import { eq } from 'drizzle-orm' import { useDB, schema } from '../../../utils/db.js' import { requireAdmin } from '../../../utils/auth.js' export default defineEventHandler(async (event) => { requireAdmin(event) const id = Number(getRouterParam(event, 'id')) if (!id || isNaN(id)) { throw createError({ statusCode: 400, message: '无效的 id 参数' }) } const body = await readBody(event) || {} const patch = {} if (typeof body.read === 'boolean') { patch.read = body.read } if (typeof body.replied === 'boolean') { patch.replied = body.replied patch.repliedAt = body.replied ? new Date().toISOString() : null // 标记为已回复时自动视为已读 if (body.replied) patch.read = true } if (body.data && typeof body.data === 'object' && !Array.isArray(body.data)) { patch.data = JSON.stringify(body.data) } if (Object.keys(patch).length === 0) { throw createError({ statusCode: 400, message: '请求体必须包含 read/replied/data 中的至少一个字段' }) } const db = useDB() const [existing] = db .select() .from(schema.submissions) .where(eq(schema.submissions.id, id)) .all() if (!existing) { throw createError({ statusCode: 404, message: '记录不存在' }) } db.update(schema.submissions) .set(patch) .where(eq(schema.submissions.id, id)) .run() const [updated] = db .select() .from(schema.submissions) .where(eq(schema.submissions.id, id)) .all() return { ...updated, data: JSON.parse(updated.data), } })