/** * Persist secret findings to the database */ import { createId } from "@paralleldrive/cuid2"; import type { secretAlerts } from "../db/schema.js"; import type { SecretFinding } from "./scanner.js"; /** * Database interface for inserting secret alerts * This allows the function to work with any Drizzle database instance */ export interface SecretAlertInsertable { insert: (table: typeof secretAlerts) => { values: ( records: Array<{ id: string; eventId: string; deviceId: string | null; type: SecretFinding["type"]; field: string; preview: string; redacted: boolean; acknowledged: boolean; detectedAt: Date; acknowledgedAt: null; }>, ) => Promise; }; } /** * Persist secret findings to the secretAlerts table * @param db - Database instance with insert capability * @param alertsTable - The secretAlerts table schema * @param findings - Array of secret findings from scanner * @param deviceId - Optional device ID for multi-device mode * @returns Number of alerts persisted */ export async function persistSecretFindings( db: SecretAlertInsertable, alertsTable: typeof secretAlerts, findings: SecretFinding[], deviceId?: string | null, ): Promise { if (findings.length === 0) return 0; const records = findings.map((finding) => ({ id: createId(), eventId: finding.eventId, deviceId: deviceId || null, type: finding.type, field: finding.field, preview: finding.preview, redacted: true, acknowledged: false, detectedAt: new Date(), acknowledgedAt: null, })); await db.insert(alertsTable).values(records); return records.length; }