Files
HealthLog/src/lib/auth/audit.ts
T
Marc-André Bombeck 38309932f3 Initial release v1.0.0
HealthLog — personal health-tracking PWA with weight, blood pressure,
pulse, mood tracking, medication compliance, Withings integration,
doctor report PDF export, and Dracula-themed UI.
2026-03-10 20:14:33 +01:00

41 lines
1.0 KiB
TypeScript

import { prisma } from "@/lib/db";
import { lookupIpLocation } from "@/lib/geo";
export async function auditLog(
action: string,
opts: {
userId?: string | null;
details?: Record<string, unknown>;
ipAddress?: string | null;
} = {},
) {
const entry = await prisma.auditLog.create({
data: {
action,
userId: opts.userId ?? null,
details: opts.details ? JSON.stringify(opts.details) : null,
ipAddress: opts.ipAddress ?? null,
},
});
// Fire-and-forget: resolve IP location and update the entry (3s timeout)
if (opts.ipAddress && action.startsWith("auth.")) {
const timeout = new Promise<null>((resolve) =>
setTimeout(() => resolve(null), 3000),
);
Promise.race([lookupIpLocation(opts.ipAddress), timeout])
.then((location) => {
if (location) {
return prisma.auditLog.update({
where: { id: entry.id },
data: { location },
});
}
})
.catch(() => {
// Silently ignore geo lookup failures
});
}
}