import { getDb } from "@wrnexus/db"; /** Give every real CRM user a useful workspace on first access. */ export async function ensureCrmWorkspace(ownerId: string): Promise { if (!ownerId) return; const db = getDb(); await db.exec( "INSERT OR IGNORE INTO crm_contacts (owner_id,name,email,company,phone,status) VALUES (?,?,?,?,?,?)", [ownerId, "Ada Lovelace", "ada@example.test", "Analytical Engines", "+1 555 0101", "customer"], ); await db.exec( "INSERT OR IGNORE INTO crm_contacts (owner_id,name,email,company,phone,status) VALUES (?,?,?,?,?,?)", [ownerId, "Grace Hopper", "grace@example.test", "Compiler Labs", "+1 555 0102", "lead"], ); const existingDeal = await db.one<{ count: number }>( "SELECT COUNT(*) count FROM crm_deals WHERE owner_id = ?", [ownerId], ); if (Number(existingDeal?.count ?? 0) === 0) { const contact = await db.one<{ id: number }>( "SELECT id FROM crm_contacts WHERE owner_id = ? AND email = ?", [ownerId, "ada@example.test"], ); await db.exec( "INSERT INTO crm_deals (owner_id,contact_id,title,value_cents,stage,close_date) VALUES (?,?,?,?,?,?)", [ownerId, contact?.id ?? null, "Enterprise rollout", 12500000, "proposal", "2026-12-15"], ); } }