20 lines
983 B
TypeScript
20 lines
983 B
TypeScript
import { requireAdmin } from '../../../utils/auth'
|
|
import { writeAuditLog } from '../../../utils/audit'
|
|
import { useDatabase } from '../../../utils/db'
|
|
import { siteDocumentSchema } from '../../../utils/site-schema'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const user = await requireAdmin(event)
|
|
const parsed = siteDocumentSchema.safeParse(await readBody(event))
|
|
if (!parsed.success) {
|
|
throw createError({ statusCode: 422, statusMessage: '内容校验失败', data: { issues: parsed.error.issues } })
|
|
}
|
|
const document = parsed.data
|
|
const timestamp = new Date().toISOString()
|
|
const db = useDatabase()
|
|
db.prepare('UPDATE site_documents SET draft_json = ?, updated_by = ?, updated_at = ? WHERE document_key = ?')
|
|
.run(JSON.stringify(document), user.id, timestamp, 'website')
|
|
writeAuditLog(event, { operatorId: user.id, action: 'save_draft', entityType: 'site_document', entityKey: 'website' })
|
|
return { data: document, updatedAt: timestamp }
|
|
})
|