auth-yes/server/audit.ts

25 lines
746 B
TypeScript

import { sql } from "./db.ts";
/**
* SIDE EFFECT: Asynchronously logs an audit record to the database.
* Does not block the main execution thread. Errors are logged but swallowed
* to prevent failing the core request due to a logging issue.
*/
export function auditLog(
userId: string | null,
action: string,
resource: string | null,
details: Record<string, unknown> | null,
ipAddress: string,
): void {
// Fire and forget
sql`
INSERT INTO audit_records (user_id, action, resource, details, ip_address)
VALUES (${userId}, ${action}, ${resource}, ${
details ? JSON.stringify(details) : null
}, ${ipAddress})
`.catch((error) => {
console.error("[Audit Logger] Failed to insert audit record:", error);
});
}