import { headers } from "next/headers"; import { prisma } from "@/lib/prisma"; /** * Append a staff-action audit entry (AtomCMS StaffActivity). Never throws — * logging must not block the action it records. */ export async function logStaffActivity(opts: { staffId: number; action: string; description: string; targetType?: string; targetId?: number; }): Promise { try { let ip: string | null = null; try { const h = await headers(); ip = h.get("x-real-client-ip") ?? h.get("x-forwarded-for")?.split(",")[0]?.trim() ?? null; } catch { ip = null; } await prisma.staffActivities.create({ data: { userId: BigInt(opts.staffId), action: opts.action.slice(0, 50), description: opts.description, targetType: opts.targetType ?? null, targetId: opts.targetId != null ? BigInt(opts.targetId) : null, ipAddress: ip, createdAt: new Date(), }, }); } catch { // swallow — audit logging is best-effort } }