25 lines
681 B
TypeScript
25 lines
681 B
TypeScript
import type { H3Event } from 'h3'
|
|
import { useDatabase } from './db'
|
|
|
|
export function writeAuditLog(event: H3Event, input: {
|
|
operatorId?: number
|
|
action: string
|
|
entityType: string
|
|
entityKey?: string
|
|
metadata?: Record<string, unknown>
|
|
}) {
|
|
const db = useDatabase()
|
|
db.prepare(`
|
|
INSERT INTO audit_logs (operator_id, action, entity_type, entity_key, ip_address, metadata_json, created_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
`).run(
|
|
input.operatorId ?? null,
|
|
input.action,
|
|
input.entityType,
|
|
input.entityKey ?? null,
|
|
getRequestIP(event, { xForwardedFor: true }) ?? null,
|
|
JSON.stringify(input.metadata ?? {}),
|
|
new Date().toISOString(),
|
|
)
|
|
}
|