Files
WRNexusJS/examples/crm-app/app/lib/workspace.ts
T
Clintchiz cd0dffa87d
Quality / quality (ubuntu-latest) (push) Failing after 11m17s
Quality / quality (windows-latest) (push) Canceled after 0s
feat: complete SSR CRM and refine auth UI
2026-08-20 16:17:59 +05:30

30 lines
1.2 KiB
TypeScript

import { getDb } from "@wrnexus/db";
/** Give every real CRM user a useful workspace on first access. */
export async function ensureCrmWorkspace(ownerId: string): Promise<void> {
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"],
);
}
}