Files
open-safetylog-ehs/server/lib/audit.ts
T
jayjennings c69eccb5c1
CI / Lint, typecheck, test, build (push) Canceled after 0s
CI / End-to-end tests (push) Canceled after 0s
feat: implement incident reporting and management system
- Add incident reporting routes and logic to handle creation, updating, and retrieval of incidents.
- Implement visibility rules for users based on roles (reporters, supervisors).
- Create tests for incident reporting functionality to ensure correctness and reliability.
- Introduce site and area management routes, including validation for area hierarchy.
- Establish user management routes with role-based access control for creating and updating users.
- Add setup routes for initializing the application with an organization and admin user.
- Implement shared enums and schemas for consistent validation and data handling across the application.
- Create a test helper for setting up an in-memory database and API client for testing.
- Add TypeScript configuration for server-side code with strict type checking and module resolution.
2026-10-06 10:51:18 -07:00

30 lines
1.1 KiB
TypeScript

import type { Db } from '../db/client.ts'
import { auditLog } from '../db/schema.ts'
interface AuditEntry {
organizationId: string
actorId: string | null
entityType: string
entityId: string
action: string
changes?: Record<string, unknown> | null
}
export async function audit(db: Db, entry: AuditEntry): Promise<void> {
await db.insert(auditLog).values({ ...entry, changes: entry.changes ?? null })
}
const comparable = (value: unknown) => (value instanceof Date ? value.toISOString() : (value ?? null))
/** `{ field: [before, after] }` for each field in `next` whose value differs from `current`. */
export function diff(current: Record<string, unknown>, next: Record<string, unknown>): Record<string, [unknown, unknown]> {
const changes: Record<string, [unknown, unknown]> = {}
for (const [key, value] of Object.entries(next)) {
if (value === undefined) continue
const before = comparable(current[key])
const after = comparable(value)
if (JSON.stringify(before) !== JSON.stringify(after)) changes[key] = [before, after]
}
return changes
}