mirror_match/lib/activity-log.ts
ilia 01480586ff
All checks were successful
CI / skip-ci-check (pull_request) Successful in 1m23s
CI / lint-and-type-check (pull_request) Successful in 1m47s
CI / test (pull_request) Successful in 1m51s
CI / build (pull_request) Successful in 1m52s
CI / secret-scanning (pull_request) Successful in 1m24s
CI / dependency-scan (pull_request) Successful in 1m28s
CI / sast-scan (pull_request) Successful in 2m31s
CI / workflow-summary (pull_request) Successful in 1m21s
refactor: Update activity log details type for improved type safety
- Changed the type of `details` in the ActivityLog interface and logActivity function from `Record<string, any>` to `Record<string, unknown>` to enhance type safety and clarity.
- Updated the proxy function in Prisma client to use `keyof PrismaClient` for property access, improving type inference and reducing reliance on `any`.
2026-01-04 16:34:23 -05:00

53 lines
1.3 KiB
TypeScript

/**
* Activity logging utility for tracking user actions
*/
export interface ActivityLog {
timestamp: string
userId?: string
userEmail?: string
userRole?: string
action: string
path: string
method: string
ip?: string
details?: Record<string, unknown>
}
export function logActivity(
action: string,
path: string,
method: string,
user?: { id: string; email: string; role: string } | null,
details?: Record<string, unknown>,
request?: Request
) {
const timestamp = new Date().toISOString()
const ip = request?.headers.get("x-forwarded-for") ||
request?.headers.get("x-real-ip") ||
"unknown"
const log: ActivityLog = {
timestamp,
userId: user?.id,
userEmail: user?.email,
userRole: user?.role,
action,
path,
method,
ip: ip.split(",")[0].trim(), // Get first IP if multiple
details
}
// Format: [ACTION] timestamp | method path | User: email (role) | IP: ip | Details: {...}
const userInfo = user
? `${user.email} (${user.role})`
: "UNAUTHENTICATED"
const detailsStr = details ? ` | Details: ${JSON.stringify(details)}` : ""
console.log(`[${action}] ${timestamp} | ${method} ${path} | User: ${userInfo} | IP: ${ip.split(",")[0].trim()}${detailsStr}`)
return log
}