feat: complete SSR CRM and refine auth UI
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import { can } from "@wrnexus/authz";
|
||||
import { getDb } from "@wrnexus/db";
|
||||
import type { Context } from "@wrnexus/core";
|
||||
import { ensureCrmWorkspace } from "../lib/workspace.ts";
|
||||
|
||||
const userId = (ctx: Context) => String((ctx.user as { id?: unknown } | undefined)?.id ?? "");
|
||||
|
||||
export async function GET(ctx: Context): Promise<Response> {
|
||||
if (!(await can(ctx, "contact:read")))
|
||||
return Response.json({ error: "Forbidden" }, { status: 403 });
|
||||
await ensureCrmWorkspace(userId(ctx));
|
||||
const rows = await getDb().all(
|
||||
"SELECT id,name,email,company,phone,status,created_at FROM crm_contacts WHERE owner_id = ? ORDER BY name",
|
||||
[userId(ctx)],
|
||||
);
|
||||
return Response.json({ contacts: rows });
|
||||
}
|
||||
|
||||
export async function POST(ctx: Context): Promise<Response> {
|
||||
if (!(await can(ctx, "contact:write")))
|
||||
return Response.json({ error: "Forbidden" }, { status: 403 });
|
||||
const body = (await ctx.req.json()) as Record<string, unknown>;
|
||||
const name = String(body.name ?? "").trim();
|
||||
const email = String(body.email ?? "")
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
if (!name || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
||||
return Response.json({ error: "A name and valid email are required" }, { status: 400 });
|
||||
}
|
||||
const result = await getDb().exec(
|
||||
"INSERT INTO crm_contacts (owner_id,name,email,company,phone,status) VALUES (?,?,?,?,?,?)",
|
||||
[userId(ctx), name, email, String(body.company ?? ""), String(body.phone ?? ""), "lead"],
|
||||
);
|
||||
return Response.json({ id: Number(result.lastInsertId), name, email }, { status: 201 });
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { can } from "@wrnexus/authz";
|
||||
import { getDb } from "@wrnexus/db";
|
||||
import type { Context } from "@wrnexus/core";
|
||||
import { ensureCrmWorkspace } from "../lib/workspace.ts";
|
||||
|
||||
export async function GET(ctx: Context): Promise<Response> {
|
||||
if (!(await can(ctx, "crm:dashboard")))
|
||||
return Response.json({ error: "Forbidden" }, { status: 403 });
|
||||
const owner = String((ctx.user as { id?: unknown } | undefined)?.id ?? "");
|
||||
await ensureCrmWorkspace(owner);
|
||||
const metrics = await getDb().one<{
|
||||
pipeline_value_cents: number;
|
||||
open_opportunities: number;
|
||||
contacts: number;
|
||||
}>(
|
||||
`SELECT
|
||||
COALESCE((SELECT SUM(value_cents) FROM crm_deals WHERE owner_id = ? AND stage NOT IN ('won','lost')), 0) pipeline_value_cents,
|
||||
(SELECT COUNT(*) FROM crm_deals WHERE owner_id = ? AND stage NOT IN ('won','lost')) open_opportunities,
|
||||
(SELECT COUNT(*) FROM crm_contacts WHERE owner_id = ?) contacts`,
|
||||
[owner, owner, owner],
|
||||
);
|
||||
return Response.json({
|
||||
metrics: {
|
||||
pipelineValue: new Intl.NumberFormat("en-US", {
|
||||
style: "currency",
|
||||
currency: "USD",
|
||||
maximumFractionDigits: 0,
|
||||
}).format(Number(metrics?.pipeline_value_cents ?? 0) / 100),
|
||||
openOpportunities: Number(metrics?.open_opportunities ?? 0),
|
||||
contacts: Number(metrics?.contacts ?? 0),
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { can } from "@wrnexus/authz";
|
||||
import { getDb } from "@wrnexus/db";
|
||||
import type { Context } from "@wrnexus/core";
|
||||
import { ensureCrmWorkspace } from "../lib/workspace.ts";
|
||||
|
||||
export async function GET(ctx: Context): Promise<Response> {
|
||||
if (!(await can(ctx, "deal:read"))) return Response.json({ error: "Forbidden" }, { status: 403 });
|
||||
const owner = String((ctx.user as { id?: unknown } | undefined)?.id ?? "");
|
||||
await ensureCrmWorkspace(owner);
|
||||
const deals = await getDb().all(
|
||||
"SELECT d.id,d.title,d.value_cents,d.stage,d.close_date,c.name contact_name FROM crm_deals d LEFT JOIN crm_contacts c ON c.id=d.contact_id WHERE d.owner_id=? ORDER BY d.updated_at DESC",
|
||||
[owner],
|
||||
);
|
||||
return Response.json({ deals });
|
||||
}
|
||||
Reference in New Issue
Block a user