PronounsPage/server/audit.ts
Valentyne Stigloher 7ce1bb0e83 (ts) convert a lot to TypeScript
Co-authored-by: Sky <msurvival65@gmail.com>
Co-authored-by: tecc <tecc@tecc.me>
2024-02-23 17:48:16 +01:00

19 lines
620 B
TypeScript

import SQL from 'sql-template-strings';
import { ulid } from 'ulid';
import type { Request } from 'express';
export default async (
req: Pick<Request, 'db' | 'user' | 'rawUser'>,
event: string,
payload: object | null = null,
): Promise<void> => {
try {
const user = req.user || req.rawUser || { id: null, username: null };
await req.db.get(SQL`INSERT INTO audit_log (id, userId, username, event, payload) VALUES (
${ulid()}, ${user.id}, ${user.username}, ${event}, ${payload ? JSON.stringify(payload) : null}
)`);
} catch (e) {
console.error(e);
}
};