Spaces:
Sleeping
Sleeping
Merge branch 'main' of https://huggingface.co/spaces/gaurv007/ClauseGuard
Browse files- web/app/admin/page.tsx +334 -0
- web/app/api/admin/route.ts +162 -0
- web/app/api/api-keys/route.ts +70 -0
- web/app/api/custom-rules/route.ts +94 -0
- web/app/api/parse-upload/route.ts +45 -0
- web/app/api/teams/route.ts +99 -0
- web/app/dashboard-pages/analyze/page.tsx +108 -35
- web/app/dashboard-pages/team/page.tsx +142 -0
- web/components/nav.tsx +31 -11
- web/lib/admin-guard.ts +29 -0
- web/lib/supabase/admin-schema.sql +47 -0
- web/lib/supabase/client.ts +10 -1
- web/lib/supabase/schema.sql +106 -15
- web/next.config.ts +1 -1
- web/package.json +2 -0
web/app/admin/page.tsx
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use client";
|
| 2 |
+
|
| 3 |
+
import { useState, useEffect } from "react";
|
| 4 |
+
import Link from "next/link";
|
| 5 |
+
import {
|
| 6 |
+
LayoutDashboard, Users, ScanText, Key, Shield, ScrollText, Activity,
|
| 7 |
+
Search, Ban, CircleCheck, ChevronDown, ChevronUp, Trash2, Crown,
|
| 8 |
+
ArrowLeft, RefreshCw, TriangleAlert, UserCheck, Eye, EyeOff
|
| 9 |
+
} from "lucide-react";
|
| 10 |
+
|
| 11 |
+
type Tab = "overview" | "users" | "scans" | "teams" | "api-keys" | "logs";
|
| 12 |
+
|
| 13 |
+
export default function AdminPage() {
|
| 14 |
+
const [tab, setTab] = useState<Tab>("overview");
|
| 15 |
+
const [stats, setStats] = useState<any>(null);
|
| 16 |
+
const [users, setUsers] = useState<any[]>([]);
|
| 17 |
+
const [scans, setScans] = useState<any[]>([]);
|
| 18 |
+
const [teams, setTeams] = useState<any[]>([]);
|
| 19 |
+
const [apiKeys, setApiKeys] = useState<any[]>([]);
|
| 20 |
+
const [logs, setLogs] = useState<any[]>([]);
|
| 21 |
+
const [search, setSearch] = useState("");
|
| 22 |
+
const [loading, setLoading] = useState(false);
|
| 23 |
+
const [userTotal, setUserTotal] = useState(0);
|
| 24 |
+
const [scanTotal, setScanTotal] = useState(0);
|
| 25 |
+
|
| 26 |
+
async function fetchData(action: string, params?: Record<string, string>) {
|
| 27 |
+
const qs = new URLSearchParams({ action, ...params });
|
| 28 |
+
const res = await fetch(`/api/admin?${qs}`);
|
| 29 |
+
if (!res.ok) { window.location.href = "/dashboard-pages/dashboard"; return null; }
|
| 30 |
+
return res.json();
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
async function adminAction(body: any) {
|
| 34 |
+
setLoading(true);
|
| 35 |
+
await fetch("/api/admin", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
|
| 36 |
+
setLoading(false);
|
| 37 |
+
loadTab(tab);
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
async function loadTab(t: Tab) {
|
| 41 |
+
setLoading(true);
|
| 42 |
+
switch (t) {
|
| 43 |
+
case "overview": { const d = await fetchData("stats"); if (d) setStats(d); break; }
|
| 44 |
+
case "users": { const d = await fetchData("users", search ? { search } : {}); if (d) { setUsers(d.users); setUserTotal(d.total); } break; }
|
| 45 |
+
case "scans": { const d = await fetchData("scans"); if (d) { setScans(d.scans); setScanTotal(d.total); } break; }
|
| 46 |
+
case "teams": { const d = await fetchData("teams"); if (d) setTeams(d.teams); break; }
|
| 47 |
+
case "api-keys": { const d = await fetchData("api-keys"); if (d) setApiKeys(d.keys); break; }
|
| 48 |
+
case "logs": { const d = await fetchData("logs"); if (d) setLogs(d.logs); break; }
|
| 49 |
+
}
|
| 50 |
+
setLoading(false);
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
useEffect(() => { loadTab(tab); }, [tab]);
|
| 54 |
+
|
| 55 |
+
const TABS: { key: Tab; label: string; icon: any }[] = [
|
| 56 |
+
{ key: "overview", label: "Overview", icon: LayoutDashboard },
|
| 57 |
+
{ key: "users", label: "Users", icon: Users },
|
| 58 |
+
{ key: "scans", label: "Scans", icon: ScanText },
|
| 59 |
+
{ key: "teams", label: "Teams", icon: Shield },
|
| 60 |
+
{ key: "api-keys", label: "API Keys", icon: Key },
|
| 61 |
+
{ key: "logs", label: "Activity", icon: Activity },
|
| 62 |
+
];
|
| 63 |
+
|
| 64 |
+
const planBadge = (plan: string) => {
|
| 65 |
+
const c: Record<string, string> = { free: "bg-zinc-100 text-zinc-600", pro: "bg-blue-50 text-blue-700", team: "bg-purple-50 text-purple-700" };
|
| 66 |
+
return <span className={`text-[11px] font-medium px-2 py-0.5 rounded ${c[plan] || c.free}`}>{plan}</span>;
|
| 67 |
+
};
|
| 68 |
+
|
| 69 |
+
return (
|
| 70 |
+
<div className="min-h-screen bg-white">
|
| 71 |
+
<div className="max-w-6xl mx-auto px-5 py-8">
|
| 72 |
+
{/* Header */}
|
| 73 |
+
<div className="flex items-center justify-between mb-8">
|
| 74 |
+
<div>
|
| 75 |
+
<Link href="/dashboard-pages/dashboard" className="inline-flex items-center gap-1.5 text-sm text-zinc-400 hover:text-zinc-600 mb-2">
|
| 76 |
+
<ArrowLeft className="w-3.5 h-3.5" /> Dashboard
|
| 77 |
+
</Link>
|
| 78 |
+
<h1 className="text-2xl font-semibold tracking-tight flex items-center gap-2">
|
| 79 |
+
<Crown className="w-6 h-6 text-amber-500" />
|
| 80 |
+
Admin Panel
|
| 81 |
+
</h1>
|
| 82 |
+
</div>
|
| 83 |
+
<button onClick={() => loadTab(tab)} disabled={loading}
|
| 84 |
+
className="p-2 rounded-lg hover:bg-zinc-100 text-zinc-400 disabled:opacity-40">
|
| 85 |
+
<RefreshCw className={`w-4 h-4 ${loading ? "animate-spin" : ""}`} />
|
| 86 |
+
</button>
|
| 87 |
+
</div>
|
| 88 |
+
|
| 89 |
+
{/* Tabs */}
|
| 90 |
+
<div className="flex gap-1 mb-6 overflow-x-auto pb-1">
|
| 91 |
+
{TABS.map((t) => (
|
| 92 |
+
<button key={t.key} onClick={() => setTab(t.key)}
|
| 93 |
+
className={`flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-medium whitespace-nowrap transition-colors ${
|
| 94 |
+
tab === t.key ? "bg-zinc-900 text-white" : "text-zinc-500 hover:bg-zinc-100"
|
| 95 |
+
}`}>
|
| 96 |
+
<t.icon className="w-4 h-4" />
|
| 97 |
+
{t.label}
|
| 98 |
+
</button>
|
| 99 |
+
))}
|
| 100 |
+
</div>
|
| 101 |
+
|
| 102 |
+
{/* Overview */}
|
| 103 |
+
{tab === "overview" && stats && (
|
| 104 |
+
<div className="space-y-6">
|
| 105 |
+
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
|
| 106 |
+
{[
|
| 107 |
+
{ label: "Total Users", value: stats.total_users, sub: `${stats.banned_users} banned` },
|
| 108 |
+
{ label: "Pro Users", value: stats.pro_users },
|
| 109 |
+
{ label: "Team Users", value: stats.team_users },
|
| 110 |
+
{ label: "Free Users", value: stats.free_users },
|
| 111 |
+
{ label: "Total Scans", value: stats.total_scans },
|
| 112 |
+
{ label: "Scans Today", value: stats.scans_today },
|
| 113 |
+
{ label: "Teams", value: stats.total_teams },
|
| 114 |
+
{ label: "Active API Keys", value: stats.total_api_keys },
|
| 115 |
+
].map((s, i) => (
|
| 116 |
+
<div key={i} className="border border-zinc-200 rounded-xl p-4">
|
| 117 |
+
<p className="text-xs text-zinc-400">{s.label}</p>
|
| 118 |
+
<p className="text-2xl font-semibold mt-1">{s.value}</p>
|
| 119 |
+
{s.sub && <p className="text-xs text-zinc-400 mt-0.5">{s.sub}</p>}
|
| 120 |
+
</div>
|
| 121 |
+
))}
|
| 122 |
+
</div>
|
| 123 |
+
</div>
|
| 124 |
+
)}
|
| 125 |
+
|
| 126 |
+
{/* Users */}
|
| 127 |
+
{tab === "users" && (
|
| 128 |
+
<div>
|
| 129 |
+
<div className="flex gap-2 mb-4">
|
| 130 |
+
<div className="flex-1 relative">
|
| 131 |
+
<Search className="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-zinc-400" />
|
| 132 |
+
<input value={search} onChange={(e) => setSearch(e.target.value)}
|
| 133 |
+
onKeyDown={(e) => e.key === "Enter" && loadTab("users")}
|
| 134 |
+
placeholder="Search by email or name..."
|
| 135 |
+
className="w-full pl-10 pr-4 py-2.5 border border-zinc-200 rounded-lg text-sm focus:outline-none focus:border-zinc-400" />
|
| 136 |
+
</div>
|
| 137 |
+
<button onClick={() => loadTab("users")} className="px-4 bg-zinc-900 text-white rounded-lg text-sm font-medium hover:bg-zinc-800">Search</button>
|
| 138 |
+
</div>
|
| 139 |
+
<p className="text-xs text-zinc-400 mb-3">{userTotal} users total</p>
|
| 140 |
+
<div className="border border-zinc-200 rounded-xl overflow-hidden">
|
| 141 |
+
<table className="w-full text-sm">
|
| 142 |
+
<thead className="bg-zinc-50 border-b border-zinc-200">
|
| 143 |
+
<tr>
|
| 144 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">User</th>
|
| 145 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Plan</th>
|
| 146 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Scans</th>
|
| 147 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Joined</th>
|
| 148 |
+
<th className="text-right px-4 py-3 font-medium text-zinc-500">Actions</th>
|
| 149 |
+
</tr>
|
| 150 |
+
</thead>
|
| 151 |
+
<tbody className="divide-y divide-zinc-100">
|
| 152 |
+
{users.map((u) => (
|
| 153 |
+
<tr key={u.id} className={`${u.is_banned ? "opacity-50 bg-red-50/30" : "hover:bg-zinc-50"}`}>
|
| 154 |
+
<td className="px-4 py-3">
|
| 155 |
+
<p className="font-medium flex items-center gap-1.5">
|
| 156 |
+
{u.full_name || u.email}
|
| 157 |
+
{u.role === "admin" && <Crown className="w-3.5 h-3.5 text-amber-500" />}
|
| 158 |
+
{u.is_banned && <Ban className="w-3.5 h-3.5 text-red-500" />}
|
| 159 |
+
</p>
|
| 160 |
+
<p className="text-xs text-zinc-400">{u.email}</p>
|
| 161 |
+
</td>
|
| 162 |
+
<td className="px-4 py-3">{planBadge(u.plan)}</td>
|
| 163 |
+
<td className="px-4 py-3 text-zinc-500">{u.analyses_this_month}/mo</td>
|
| 164 |
+
<td className="px-4 py-3 text-xs text-zinc-400">{new Date(u.created_at).toLocaleDateString()}</td>
|
| 165 |
+
<td className="px-4 py-3 text-right">
|
| 166 |
+
<div className="flex gap-1 justify-end">
|
| 167 |
+
<select defaultValue={u.plan} onChange={(e) => adminAction({ action: "update_plan", userId: u.id, plan: e.target.value })}
|
| 168 |
+
className="text-xs border border-zinc-200 rounded px-2 py-1 bg-white">
|
| 169 |
+
<option value="free">Free</option>
|
| 170 |
+
<option value="pro">Pro</option>
|
| 171 |
+
<option value="team">Team</option>
|
| 172 |
+
</select>
|
| 173 |
+
<button onClick={() => adminAction({ action: "ban_user", userId: u.id, banned: !u.is_banned })}
|
| 174 |
+
className={`p-1.5 rounded hover:bg-zinc-100 ${u.is_banned ? "text-emerald-500" : "text-red-400"}`}
|
| 175 |
+
title={u.is_banned ? "Unban" : "Ban"}>
|
| 176 |
+
{u.is_banned ? <CircleCheck className="w-3.5 h-3.5" /> : <Ban className="w-3.5 h-3.5" />}
|
| 177 |
+
</button>
|
| 178 |
+
</div>
|
| 179 |
+
</td>
|
| 180 |
+
</tr>
|
| 181 |
+
))}
|
| 182 |
+
</tbody>
|
| 183 |
+
</table>
|
| 184 |
+
</div>
|
| 185 |
+
</div>
|
| 186 |
+
)}
|
| 187 |
+
|
| 188 |
+
{/* Scans */}
|
| 189 |
+
{tab === "scans" && (
|
| 190 |
+
<div>
|
| 191 |
+
<p className="text-xs text-zinc-400 mb-3">{scanTotal} scans total</p>
|
| 192 |
+
<div className="border border-zinc-200 rounded-xl overflow-hidden">
|
| 193 |
+
<table className="w-full text-sm">
|
| 194 |
+
<thead className="bg-zinc-50 border-b border-zinc-200">
|
| 195 |
+
<tr>
|
| 196 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Source</th>
|
| 197 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Grade</th>
|
| 198 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Risk</th>
|
| 199 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Clauses</th>
|
| 200 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Date</th>
|
| 201 |
+
<th className="text-right px-4 py-3 font-medium text-zinc-500">Actions</th>
|
| 202 |
+
</tr>
|
| 203 |
+
</thead>
|
| 204 |
+
<tbody className="divide-y divide-zinc-100">
|
| 205 |
+
{scans.map((s) => (
|
| 206 |
+
<tr key={s.id} className="hover:bg-zinc-50">
|
| 207 |
+
<td className="px-4 py-3 max-w-xs truncate text-zinc-700">{s.source_url || "Manual scan"}</td>
|
| 208 |
+
<td className="px-4 py-3">
|
| 209 |
+
<span className={`text-xs font-semibold px-2 py-0.5 rounded ${
|
| 210 |
+
s.grade === "F" || s.grade === "D" ? "bg-red-50 text-red-700" :
|
| 211 |
+
s.grade === "C" ? "bg-amber-50 text-amber-700" : "bg-emerald-50 text-emerald-700"
|
| 212 |
+
}`}>{s.grade}</span>
|
| 213 |
+
</td>
|
| 214 |
+
<td className="px-4 py-3 text-zinc-500">{s.risk_score}/100</td>
|
| 215 |
+
<td className="px-4 py-3 text-zinc-500">{s.flagged_count}/{s.total_clauses}</td>
|
| 216 |
+
<td className="px-4 py-3 text-xs text-zinc-400">{new Date(s.created_at).toLocaleDateString()}</td>
|
| 217 |
+
<td className="px-4 py-3 text-right">
|
| 218 |
+
<button onClick={() => adminAction({ action: "delete_scan", scanId: s.id })}
|
| 219 |
+
className="p-1.5 rounded hover:bg-red-50 text-red-400" title="Delete">
|
| 220 |
+
<Trash2 className="w-3.5 h-3.5" />
|
| 221 |
+
</button>
|
| 222 |
+
</td>
|
| 223 |
+
</tr>
|
| 224 |
+
))}
|
| 225 |
+
</tbody>
|
| 226 |
+
</table>
|
| 227 |
+
</div>
|
| 228 |
+
</div>
|
| 229 |
+
)}
|
| 230 |
+
|
| 231 |
+
{/* Teams */}
|
| 232 |
+
{tab === "teams" && (
|
| 233 |
+
<div className="border border-zinc-200 rounded-xl overflow-hidden">
|
| 234 |
+
<table className="w-full text-sm">
|
| 235 |
+
<thead className="bg-zinc-50 border-b border-zinc-200">
|
| 236 |
+
<tr>
|
| 237 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Team</th>
|
| 238 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Owner</th>
|
| 239 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Seats</th>
|
| 240 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Created</th>
|
| 241 |
+
<th className="text-right px-4 py-3 font-medium text-zinc-500">Actions</th>
|
| 242 |
+
</tr>
|
| 243 |
+
</thead>
|
| 244 |
+
<tbody className="divide-y divide-zinc-100">
|
| 245 |
+
{teams.map((t) => (
|
| 246 |
+
<tr key={t.id} className="hover:bg-zinc-50">
|
| 247 |
+
<td className="px-4 py-3 font-medium">{t.name}</td>
|
| 248 |
+
<td className="px-4 py-3 text-xs text-zinc-400 font-mono">{t.owner_id.slice(0, 8)}...</td>
|
| 249 |
+
<td className="px-4 py-3 text-zinc-500">{t.max_seats}</td>
|
| 250 |
+
<td className="px-4 py-3 text-xs text-zinc-400">{new Date(t.created_at).toLocaleDateString()}</td>
|
| 251 |
+
<td className="px-4 py-3 text-right">
|
| 252 |
+
<button onClick={() => adminAction({ action: "delete_team", teamId: t.id })}
|
| 253 |
+
className="p-1.5 rounded hover:bg-red-50 text-red-400" title="Delete team">
|
| 254 |
+
<Trash2 className="w-3.5 h-3.5" />
|
| 255 |
+
</button>
|
| 256 |
+
</td>
|
| 257 |
+
</tr>
|
| 258 |
+
))}
|
| 259 |
+
{teams.length === 0 && <tr><td colSpan={5} className="px-4 py-8 text-center text-zinc-400">No teams yet.</td></tr>}
|
| 260 |
+
</tbody>
|
| 261 |
+
</table>
|
| 262 |
+
</div>
|
| 263 |
+
)}
|
| 264 |
+
|
| 265 |
+
{/* API Keys */}
|
| 266 |
+
{tab === "api-keys" && (
|
| 267 |
+
<div className="border border-zinc-200 rounded-xl overflow-hidden">
|
| 268 |
+
<table className="w-full text-sm">
|
| 269 |
+
<thead className="bg-zinc-50 border-b border-zinc-200">
|
| 270 |
+
<tr>
|
| 271 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Name</th>
|
| 272 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Key</th>
|
| 273 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Usage</th>
|
| 274 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Status</th>
|
| 275 |
+
<th className="text-right px-4 py-3 font-medium text-zinc-500">Actions</th>
|
| 276 |
+
</tr>
|
| 277 |
+
</thead>
|
| 278 |
+
<tbody className="divide-y divide-zinc-100">
|
| 279 |
+
{apiKeys.map((k) => (
|
| 280 |
+
<tr key={k.id} className="hover:bg-zinc-50">
|
| 281 |
+
<td className="px-4 py-3 font-medium">{k.name}</td>
|
| 282 |
+
<td className="px-4 py-3 text-xs text-zinc-400 font-mono">{k.key_prefix}</td>
|
| 283 |
+
<td className="px-4 py-3 text-zinc-500">{k.calls_this_month}/{k.calls_limit}</td>
|
| 284 |
+
<td className="px-4 py-3">
|
| 285 |
+
<span className={`text-xs font-medium px-2 py-0.5 rounded ${k.is_active ? "bg-emerald-50 text-emerald-700" : "bg-red-50 text-red-700"}`}>
|
| 286 |
+
{k.is_active ? "Active" : "Revoked"}
|
| 287 |
+
</span>
|
| 288 |
+
</td>
|
| 289 |
+
<td className="px-4 py-3 text-right">
|
| 290 |
+
{k.is_active && (
|
| 291 |
+
<button onClick={() => adminAction({ action: "revoke_api_key", keyId: k.id })}
|
| 292 |
+
className="p-1.5 rounded hover:bg-red-50 text-red-400" title="Revoke">
|
| 293 |
+
<Ban className="w-3.5 h-3.5" />
|
| 294 |
+
</button>
|
| 295 |
+
)}
|
| 296 |
+
</td>
|
| 297 |
+
</tr>
|
| 298 |
+
))}
|
| 299 |
+
{apiKeys.length === 0 && <tr><td colSpan={5} className="px-4 py-8 text-center text-zinc-400">No API keys.</td></tr>}
|
| 300 |
+
</tbody>
|
| 301 |
+
</table>
|
| 302 |
+
</div>
|
| 303 |
+
)}
|
| 304 |
+
|
| 305 |
+
{/* Activity Logs */}
|
| 306 |
+
{tab === "logs" && (
|
| 307 |
+
<div className="border border-zinc-200 rounded-xl overflow-hidden">
|
| 308 |
+
<table className="w-full text-sm">
|
| 309 |
+
<thead className="bg-zinc-50 border-b border-zinc-200">
|
| 310 |
+
<tr>
|
| 311 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Action</th>
|
| 312 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Target</th>
|
| 313 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Details</th>
|
| 314 |
+
<th className="text-left px-4 py-3 font-medium text-zinc-500">Time</th>
|
| 315 |
+
</tr>
|
| 316 |
+
</thead>
|
| 317 |
+
<tbody className="divide-y divide-zinc-100">
|
| 318 |
+
{logs.map((l) => (
|
| 319 |
+
<tr key={l.id} className="hover:bg-zinc-50">
|
| 320 |
+
<td className="px-4 py-3 font-medium">{l.action}</td>
|
| 321 |
+
<td className="px-4 py-3 text-xs text-zinc-400">{l.target_type}: {l.target_id?.slice(0, 8)}...</td>
|
| 322 |
+
<td className="px-4 py-3 text-xs text-zinc-400 font-mono">{l.details ? JSON.stringify(l.details) : "β"}</td>
|
| 323 |
+
<td className="px-4 py-3 text-xs text-zinc-400">{new Date(l.created_at).toLocaleString()}</td>
|
| 324 |
+
</tr>
|
| 325 |
+
))}
|
| 326 |
+
{logs.length === 0 && <tr><td colSpan={4} className="px-4 py-8 text-center text-zinc-400">No admin actions yet.</td></tr>}
|
| 327 |
+
</tbody>
|
| 328 |
+
</table>
|
| 329 |
+
</div>
|
| 330 |
+
)}
|
| 331 |
+
</div>
|
| 332 |
+
</div>
|
| 333 |
+
);
|
| 334 |
+
}
|
web/app/api/admin/route.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
+
import { createClient } from "@/lib/supabase/server";
|
| 3 |
+
|
| 4 |
+
const ADMIN_EMAILS = ["ankygaur9972@gmail.com"];
|
| 5 |
+
|
| 6 |
+
async function checkAdmin() {
|
| 7 |
+
const supabase = await createClient();
|
| 8 |
+
const { data: { user } } = await supabase.auth.getUser();
|
| 9 |
+
if (!user || !ADMIN_EMAILS.includes(user.email || "")) {
|
| 10 |
+
return { supabase: null, user: null, error: true };
|
| 11 |
+
}
|
| 12 |
+
return { supabase, user, error: false };
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
// GET β admin stats + data
|
| 16 |
+
export async function GET(req: NextRequest) {
|
| 17 |
+
const { supabase, user, error } = await checkAdmin();
|
| 18 |
+
if (error || !supabase || !user) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
| 19 |
+
|
| 20 |
+
const url = new URL(req.url);
|
| 21 |
+
const action = url.searchParams.get("action");
|
| 22 |
+
|
| 23 |
+
switch (action) {
|
| 24 |
+
case "stats": {
|
| 25 |
+
const { count: totalUsers } = await supabase.from("profiles").select("id", { count: "exact", head: true });
|
| 26 |
+
const { count: proUsers } = await supabase.from("profiles").select("id", { count: "exact", head: true }).eq("plan", "pro");
|
| 27 |
+
const { count: teamUsers } = await supabase.from("profiles").select("id", { count: "exact", head: true }).eq("plan", "team");
|
| 28 |
+
const { count: totalScans } = await supabase.from("analyses").select("id", { count: "exact", head: true });
|
| 29 |
+
const { count: totalTeams } = await supabase.from("teams").select("id", { count: "exact", head: true });
|
| 30 |
+
const { count: totalApiKeys } = await supabase.from("api_keys").select("id", { count: "exact", head: true }).eq("is_active", true);
|
| 31 |
+
const { count: bannedUsers } = await supabase.from("profiles").select("id", { count: "exact", head: true }).eq("is_banned", true);
|
| 32 |
+
|
| 33 |
+
// Scans today
|
| 34 |
+
const today = new Date();
|
| 35 |
+
today.setHours(0, 0, 0, 0);
|
| 36 |
+
const { count: scansToday } = await supabase.from("analyses").select("id", { count: "exact", head: true }).gte("created_at", today.toISOString());
|
| 37 |
+
|
| 38 |
+
return NextResponse.json({
|
| 39 |
+
total_users: totalUsers || 0,
|
| 40 |
+
pro_users: proUsers || 0,
|
| 41 |
+
team_users: teamUsers || 0,
|
| 42 |
+
free_users: (totalUsers || 0) - (proUsers || 0) - (teamUsers || 0),
|
| 43 |
+
total_scans: totalScans || 0,
|
| 44 |
+
scans_today: scansToday || 0,
|
| 45 |
+
total_teams: totalTeams || 0,
|
| 46 |
+
total_api_keys: totalApiKeys || 0,
|
| 47 |
+
banned_users: bannedUsers || 0,
|
| 48 |
+
});
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
case "users": {
|
| 52 |
+
const limit = parseInt(url.searchParams.get("limit") || "50");
|
| 53 |
+
const offset = parseInt(url.searchParams.get("offset") || "0");
|
| 54 |
+
const search = url.searchParams.get("search") || "";
|
| 55 |
+
|
| 56 |
+
let query = supabase.from("profiles")
|
| 57 |
+
.select("id, email, full_name, plan, role, is_banned, analyses_this_month, team_id, razorpay_subscription_id, created_at", { count: "exact" })
|
| 58 |
+
.order("created_at", { ascending: false })
|
| 59 |
+
.range(offset, offset + limit - 1);
|
| 60 |
+
|
| 61 |
+
if (search) {
|
| 62 |
+
query = query.or(`email.ilike.%${search}%,full_name.ilike.%${search}%`);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
const { data: users, count } = await query;
|
| 66 |
+
return NextResponse.json({ users: users || [], total: count || 0 });
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
case "scans": {
|
| 70 |
+
const limit = parseInt(url.searchParams.get("limit") || "50");
|
| 71 |
+
const offset = parseInt(url.searchParams.get("offset") || "0");
|
| 72 |
+
|
| 73 |
+
const { data: scans, count } = await supabase.from("analyses")
|
| 74 |
+
.select("id, user_id, source_url, risk_score, grade, flagged_count, total_clauses, created_at", { count: "exact" })
|
| 75 |
+
.order("created_at", { ascending: false })
|
| 76 |
+
.range(offset, offset + limit - 1);
|
| 77 |
+
|
| 78 |
+
return NextResponse.json({ scans: scans || [], total: count || 0 });
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
case "teams": {
|
| 82 |
+
const { data: teams } = await supabase.from("teams").select("*").order("created_at", { ascending: false });
|
| 83 |
+
return NextResponse.json({ teams: teams || [] });
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
case "api-keys": {
|
| 87 |
+
const { data: keys } = await supabase.from("api_keys")
|
| 88 |
+
.select("id, user_id, name, key_prefix, calls_this_month, calls_limit, is_active, created_at")
|
| 89 |
+
.order("created_at", { ascending: false });
|
| 90 |
+
return NextResponse.json({ keys: keys || [] });
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
case "logs": {
|
| 94 |
+
const { data: logs } = await supabase.from("admin_logs")
|
| 95 |
+
.select("*").order("created_at", { ascending: false }).limit(100);
|
| 96 |
+
return NextResponse.json({ logs: logs || [] });
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
default:
|
| 100 |
+
return NextResponse.json({ error: "Invalid action" }, { status: 400 });
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
// POST β admin actions
|
| 105 |
+
export async function POST(req: NextRequest) {
|
| 106 |
+
const { supabase, user, error } = await checkAdmin();
|
| 107 |
+
if (error || !supabase || !user) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
| 108 |
+
|
| 109 |
+
const body = await req.json();
|
| 110 |
+
|
| 111 |
+
switch (body.action) {
|
| 112 |
+
case "update_plan": {
|
| 113 |
+
const { userId, plan } = body;
|
| 114 |
+
await supabase.from("profiles").update({ plan, updated_at: new Date().toISOString() }).eq("id", userId);
|
| 115 |
+
await supabase.from("admin_logs").insert({
|
| 116 |
+
admin_id: user.id, action: "update_plan", target_type: "user", target_id: userId,
|
| 117 |
+
details: { plan },
|
| 118 |
+
});
|
| 119 |
+
return NextResponse.json({ success: true });
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
case "ban_user": {
|
| 123 |
+
const { userId, banned } = body;
|
| 124 |
+
await supabase.from("profiles").update({ is_banned: banned, updated_at: new Date().toISOString() }).eq("id", userId);
|
| 125 |
+
await supabase.from("admin_logs").insert({
|
| 126 |
+
admin_id: user.id, action: banned ? "ban_user" : "unban_user", target_type: "user", target_id: userId,
|
| 127 |
+
});
|
| 128 |
+
return NextResponse.json({ success: true });
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
case "delete_scan": {
|
| 132 |
+
const { scanId } = body;
|
| 133 |
+
await supabase.from("analyses").delete().eq("id", scanId);
|
| 134 |
+
await supabase.from("admin_logs").insert({
|
| 135 |
+
admin_id: user.id, action: "delete_scan", target_type: "scan", target_id: scanId,
|
| 136 |
+
});
|
| 137 |
+
return NextResponse.json({ success: true });
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
case "revoke_api_key": {
|
| 141 |
+
const { keyId } = body;
|
| 142 |
+
await supabase.from("api_keys").update({ is_active: false }).eq("id", keyId);
|
| 143 |
+
await supabase.from("admin_logs").insert({
|
| 144 |
+
admin_id: user.id, action: "revoke_api_key", target_type: "api_key", target_id: keyId,
|
| 145 |
+
});
|
| 146 |
+
return NextResponse.json({ success: true });
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
case "delete_team": {
|
| 150 |
+
const { teamId } = body;
|
| 151 |
+
await supabase.from("profiles").update({ team_id: null }).eq("team_id", teamId);
|
| 152 |
+
await supabase.from("teams").delete().eq("id", teamId);
|
| 153 |
+
await supabase.from("admin_logs").insert({
|
| 154 |
+
admin_id: user.id, action: "delete_team", target_type: "team", target_id: teamId,
|
| 155 |
+
});
|
| 156 |
+
return NextResponse.json({ success: true });
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
default:
|
| 160 |
+
return NextResponse.json({ error: "Invalid action" }, { status: 400 });
|
| 161 |
+
}
|
| 162 |
+
}
|
web/app/api/api-keys/route.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
+
import { createClient } from "@/lib/supabase/server";
|
| 3 |
+
import crypto from "crypto";
|
| 4 |
+
|
| 5 |
+
// GET β list user's API keys
|
| 6 |
+
export async function GET() {
|
| 7 |
+
const supabase = await createClient();
|
| 8 |
+
const { data: { user } } = await supabase.auth.getUser();
|
| 9 |
+
if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
| 10 |
+
|
| 11 |
+
const { data: profile } = await supabase.from("profiles").select("plan, team_id").eq("id", user.id).single();
|
| 12 |
+
if (profile?.plan === "free") return NextResponse.json({ error: "API access requires Pro or Team plan" }, { status: 403 });
|
| 13 |
+
|
| 14 |
+
const { data: keys } = await supabase.from("api_keys")
|
| 15 |
+
.select("id, name, key_prefix, calls_this_month, calls_limit, is_active, last_used_at, created_at")
|
| 16 |
+
.eq("user_id", user.id)
|
| 17 |
+
.order("created_at", { ascending: false });
|
| 18 |
+
|
| 19 |
+
return NextResponse.json({ keys: keys || [] });
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
// POST β create new API key
|
| 23 |
+
export async function POST(req: NextRequest) {
|
| 24 |
+
const supabase = await createClient();
|
| 25 |
+
const { data: { user } } = await supabase.auth.getUser();
|
| 26 |
+
if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
| 27 |
+
|
| 28 |
+
const { data: profile } = await supabase.from("profiles").select("plan, team_id").eq("id", user.id).single();
|
| 29 |
+
if (profile?.plan === "free") return NextResponse.json({ error: "API access requires Pro or Team plan" }, { status: 403 });
|
| 30 |
+
|
| 31 |
+
const { name } = await req.json();
|
| 32 |
+
|
| 33 |
+
// Generate key: cg_live_ + 32 random hex chars
|
| 34 |
+
const rawKey = "cg_live_" + crypto.randomBytes(24).toString("hex");
|
| 35 |
+
const keyHash = crypto.createHash("sha256").update(rawKey).digest("hex");
|
| 36 |
+
const keyPrefix = rawKey.substring(0, 16) + "...";
|
| 37 |
+
|
| 38 |
+
const callsLimit = profile?.plan === "team" ? 10000 : 1000;
|
| 39 |
+
|
| 40 |
+
const { error } = await supabase.from("api_keys").insert({
|
| 41 |
+
user_id: user.id,
|
| 42 |
+
team_id: profile?.team_id || null,
|
| 43 |
+
name: name || "Default",
|
| 44 |
+
key_hash: keyHash,
|
| 45 |
+
key_prefix: keyPrefix,
|
| 46 |
+
calls_limit: callsLimit,
|
| 47 |
+
});
|
| 48 |
+
|
| 49 |
+
if (error) return NextResponse.json({ error: error.message }, { status: 500 });
|
| 50 |
+
|
| 51 |
+
// Return the full key ONCE β it's never shown again
|
| 52 |
+
return NextResponse.json({ key: rawKey, prefix: keyPrefix, name, calls_limit: callsLimit });
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
// DELETE β revoke an API key
|
| 56 |
+
export async function DELETE(req: NextRequest) {
|
| 57 |
+
const supabase = await createClient();
|
| 58 |
+
const { data: { user } } = await supabase.auth.getUser();
|
| 59 |
+
if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
| 60 |
+
|
| 61 |
+
const { keyId } = await req.json();
|
| 62 |
+
|
| 63 |
+
const { error } = await supabase.from("api_keys")
|
| 64 |
+
.update({ is_active: false })
|
| 65 |
+
.eq("id", keyId)
|
| 66 |
+
.eq("user_id", user.id);
|
| 67 |
+
|
| 68 |
+
if (error) return NextResponse.json({ error: error.message }, { status: 500 });
|
| 69 |
+
return NextResponse.json({ success: true });
|
| 70 |
+
}
|
web/app/api/custom-rules/route.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
+
import { createClient } from "@/lib/supabase/server";
|
| 3 |
+
|
| 4 |
+
// GET β list custom rules
|
| 5 |
+
export async function GET() {
|
| 6 |
+
const supabase = await createClient();
|
| 7 |
+
const { data: { user } } = await supabase.auth.getUser();
|
| 8 |
+
if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
| 9 |
+
|
| 10 |
+
const { data: profile } = await supabase.from("profiles").select("plan, team_id").eq("id", user.id).single();
|
| 11 |
+
if (profile?.plan !== "team") return NextResponse.json({ error: "Custom rules require Team plan" }, { status: 403 });
|
| 12 |
+
|
| 13 |
+
// Fetch user's own rules + team rules
|
| 14 |
+
let query = supabase.from("custom_rules").select("*").order("created_at", { ascending: false });
|
| 15 |
+
|
| 16 |
+
if (profile?.team_id) {
|
| 17 |
+
query = query.or(`user_id.eq.${user.id},team_id.eq.${profile.team_id}`);
|
| 18 |
+
} else {
|
| 19 |
+
query = query.eq("user_id", user.id);
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
const { data: rules } = await query;
|
| 23 |
+
return NextResponse.json({ rules: rules || [] });
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// POST β create a custom rule
|
| 27 |
+
export async function POST(req: NextRequest) {
|
| 28 |
+
const supabase = await createClient();
|
| 29 |
+
const { data: { user } } = await supabase.auth.getUser();
|
| 30 |
+
if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
| 31 |
+
|
| 32 |
+
const { data: profile } = await supabase.from("profiles").select("plan, team_id").eq("id", user.id).single();
|
| 33 |
+
if (profile?.plan !== "team") return NextResponse.json({ error: "Custom rules require Team plan" }, { status: 403 });
|
| 34 |
+
|
| 35 |
+
const { name, description, pattern, severity, category } = await req.json();
|
| 36 |
+
|
| 37 |
+
if (!name || !pattern || !category) {
|
| 38 |
+
return NextResponse.json({ error: "name, pattern, and category are required" }, { status: 400 });
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
// Validate regex pattern
|
| 42 |
+
try { new RegExp(pattern, "i"); } catch {
|
| 43 |
+
return NextResponse.json({ error: "Invalid regex pattern" }, { status: 400 });
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
const { data: rule, error } = await supabase.from("custom_rules").insert({
|
| 47 |
+
user_id: user.id,
|
| 48 |
+
team_id: profile?.team_id || null,
|
| 49 |
+
name,
|
| 50 |
+
description: description || null,
|
| 51 |
+
pattern,
|
| 52 |
+
severity: severity || "MEDIUM",
|
| 53 |
+
category,
|
| 54 |
+
}).select().single();
|
| 55 |
+
|
| 56 |
+
if (error) return NextResponse.json({ error: error.message }, { status: 500 });
|
| 57 |
+
return NextResponse.json({ rule });
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
// PUT β update a rule
|
| 61 |
+
export async function PUT(req: NextRequest) {
|
| 62 |
+
const supabase = await createClient();
|
| 63 |
+
const { data: { user } } = await supabase.auth.getUser();
|
| 64 |
+
if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
| 65 |
+
|
| 66 |
+
const { id, ...updates } = await req.json();
|
| 67 |
+
|
| 68 |
+
if (updates.pattern) {
|
| 69 |
+
try { new RegExp(updates.pattern, "i"); } catch {
|
| 70 |
+
return NextResponse.json({ error: "Invalid regex pattern" }, { status: 400 });
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
const { error } = await supabase.from("custom_rules")
|
| 75 |
+
.update({ ...updates, updated_at: new Date().toISOString() })
|
| 76 |
+
.eq("id", id)
|
| 77 |
+
.eq("user_id", user.id);
|
| 78 |
+
|
| 79 |
+
if (error) return NextResponse.json({ error: error.message }, { status: 500 });
|
| 80 |
+
return NextResponse.json({ success: true });
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
// DELETE β delete a rule
|
| 84 |
+
export async function DELETE(req: NextRequest) {
|
| 85 |
+
const supabase = await createClient();
|
| 86 |
+
const { data: { user } } = await supabase.auth.getUser();
|
| 87 |
+
if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
| 88 |
+
|
| 89 |
+
const { id } = await req.json();
|
| 90 |
+
|
| 91 |
+
const { error } = await supabase.from("custom_rules").delete().eq("id", id).eq("user_id", user.id);
|
| 92 |
+
if (error) return NextResponse.json({ error: error.message }, { status: 500 });
|
| 93 |
+
return NextResponse.json({ success: true });
|
| 94 |
+
}
|
web/app/api/parse-upload/route.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
+
|
| 3 |
+
export const runtime = "nodejs";
|
| 4 |
+
|
| 5 |
+
export async function POST(req: NextRequest) {
|
| 6 |
+
try {
|
| 7 |
+
const formData = await req.formData();
|
| 8 |
+
const file = formData.get("file") as File | null;
|
| 9 |
+
|
| 10 |
+
if (!file) {
|
| 11 |
+
return NextResponse.json({ error: "No file uploaded" }, { status: 400 });
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
const name = file.name.toLowerCase();
|
| 15 |
+
const buffer = Buffer.from(await file.arrayBuffer());
|
| 16 |
+
let text = "";
|
| 17 |
+
|
| 18 |
+
if (name.endsWith(".txt") || name.endsWith(".md")) {
|
| 19 |
+
text = new TextDecoder().decode(buffer);
|
| 20 |
+
} else if (name.endsWith(".pdf")) {
|
| 21 |
+
// pdf-parse v2
|
| 22 |
+
await import("pdf-parse/worker");
|
| 23 |
+
const { PDFParse } = await import("pdf-parse");
|
| 24 |
+
const parser = new PDFParse({ data: buffer });
|
| 25 |
+
const result = await parser.getText();
|
| 26 |
+
text = result.text;
|
| 27 |
+
await parser.destroy();
|
| 28 |
+
} else if (name.endsWith(".docx")) {
|
| 29 |
+
const mammoth = (await import("mammoth")).default;
|
| 30 |
+
const result = await mammoth.extractRawText({ buffer });
|
| 31 |
+
text = result.value;
|
| 32 |
+
} else {
|
| 33 |
+
return NextResponse.json({ error: "Unsupported file type. Use .pdf, .docx, .txt, or .md" }, { status: 400 });
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
if (!text || text.trim().length < 30) {
|
| 37 |
+
return NextResponse.json({ error: "Could not extract enough text from this file." }, { status: 400 });
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
return NextResponse.json({ text: text.trim(), filename: file.name, size: file.size });
|
| 41 |
+
} catch (error: any) {
|
| 42 |
+
console.error("File parse error:", error);
|
| 43 |
+
return NextResponse.json({ error: "Failed to parse file: " + (error.message || "Unknown error") }, { status: 500 });
|
| 44 |
+
}
|
| 45 |
+
}
|
web/app/api/teams/route.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
+
import { createClient } from "@/lib/supabase/server";
|
| 3 |
+
import crypto from "crypto";
|
| 4 |
+
|
| 5 |
+
// GET β fetch current team + members
|
| 6 |
+
export async function GET() {
|
| 7 |
+
const supabase = await createClient();
|
| 8 |
+
const { data: { user } } = await supabase.auth.getUser();
|
| 9 |
+
if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
| 10 |
+
|
| 11 |
+
const { data: profile } = await supabase.from("profiles").select("team_id, plan").eq("id", user.id).single();
|
| 12 |
+
if (!profile?.team_id) return NextResponse.json({ team: null, members: [], invites: [] });
|
| 13 |
+
|
| 14 |
+
const { data: team } = await supabase.from("teams").select("*").eq("id", profile.team_id).single();
|
| 15 |
+
const { data: members } = await supabase.from("profiles").select("id, email, full_name, avatar_url").eq("team_id", profile.team_id);
|
| 16 |
+
const { data: invites } = await supabase.from("team_invites").select("*").eq("team_id", profile.team_id).eq("status", "pending");
|
| 17 |
+
|
| 18 |
+
return NextResponse.json({ team, members: members || [], invites: invites || [] });
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
// POST β create team or invite member
|
| 22 |
+
export async function POST(req: NextRequest) {
|
| 23 |
+
const supabase = await createClient();
|
| 24 |
+
const { data: { user } } = await supabase.auth.getUser();
|
| 25 |
+
if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
| 26 |
+
|
| 27 |
+
const body = await req.json();
|
| 28 |
+
|
| 29 |
+
// Create team
|
| 30 |
+
if (body.action === "create") {
|
| 31 |
+
const { data: profile } = await supabase.from("profiles").select("plan, team_id").eq("id", user.id).single();
|
| 32 |
+
if (profile?.plan !== "team") return NextResponse.json({ error: "Team plan required" }, { status: 403 });
|
| 33 |
+
if (profile?.team_id) return NextResponse.json({ error: "Already in a team" }, { status: 400 });
|
| 34 |
+
|
| 35 |
+
const { data: team, error } = await supabase.from("teams").insert({
|
| 36 |
+
name: body.name || "My Team", owner_id: user.id,
|
| 37 |
+
}).select().single();
|
| 38 |
+
|
| 39 |
+
if (error) return NextResponse.json({ error: error.message }, { status: 500 });
|
| 40 |
+
|
| 41 |
+
await supabase.from("profiles").update({ team_id: team.id }).eq("id", user.id);
|
| 42 |
+
return NextResponse.json({ team });
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
// Invite member
|
| 46 |
+
if (body.action === "invite") {
|
| 47 |
+
const { data: profile } = await supabase.from("profiles").select("team_id").eq("id", user.id).single();
|
| 48 |
+
if (!profile?.team_id) return NextResponse.json({ error: "No team" }, { status: 400 });
|
| 49 |
+
|
| 50 |
+
// Check seat limit
|
| 51 |
+
const { count } = await supabase.from("profiles").select("id", { count: "exact" }).eq("team_id", profile.team_id);
|
| 52 |
+
const { data: team } = await supabase.from("teams").select("max_seats").eq("id", profile.team_id).single();
|
| 53 |
+
if ((count || 0) >= (team?.max_seats || 5)) return NextResponse.json({ error: "Team is full (max 5 seats)" }, { status: 400 });
|
| 54 |
+
|
| 55 |
+
const { error } = await supabase.from("team_invites").insert({
|
| 56 |
+
team_id: profile.team_id, email: body.email, invited_by: user.id, role: body.role || "member",
|
| 57 |
+
});
|
| 58 |
+
|
| 59 |
+
if (error) return NextResponse.json({ error: error.message }, { status: 500 });
|
| 60 |
+
return NextResponse.json({ success: true });
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
// Accept invite
|
| 64 |
+
if (body.action === "accept") {
|
| 65 |
+
const { data: invite } = await supabase.from("team_invites")
|
| 66 |
+
.select("*").eq("id", body.invite_id).eq("email", user.email).eq("status", "pending").single();
|
| 67 |
+
|
| 68 |
+
if (!invite) return NextResponse.json({ error: "Invite not found" }, { status: 404 });
|
| 69 |
+
|
| 70 |
+
await supabase.from("profiles").update({ team_id: invite.team_id }).eq("id", user.id);
|
| 71 |
+
await supabase.from("team_invites").update({ status: "accepted" }).eq("id", invite.id);
|
| 72 |
+
return NextResponse.json({ success: true });
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
return NextResponse.json({ error: "Invalid action" }, { status: 400 });
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
// DELETE β remove member or leave team
|
| 79 |
+
export async function DELETE(req: NextRequest) {
|
| 80 |
+
const supabase = await createClient();
|
| 81 |
+
const { data: { user } } = await supabase.auth.getUser();
|
| 82 |
+
if (!user) return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
|
| 83 |
+
|
| 84 |
+
const { memberId } = await req.json();
|
| 85 |
+
|
| 86 |
+
if (memberId === user.id) {
|
| 87 |
+
// Leave team
|
| 88 |
+
await supabase.from("profiles").update({ team_id: null }).eq("id", user.id);
|
| 89 |
+
} else {
|
| 90 |
+
// Remove member (owner only)
|
| 91 |
+
const { data: profile } = await supabase.from("profiles").select("team_id").eq("id", user.id).single();
|
| 92 |
+
const { data: team } = await supabase.from("teams").select("owner_id").eq("id", profile?.team_id).single();
|
| 93 |
+
if (team?.owner_id !== user.id) return NextResponse.json({ error: "Only owner can remove members" }, { status: 403 });
|
| 94 |
+
|
| 95 |
+
await supabase.from("profiles").update({ team_id: null }).eq("id", memberId);
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
return NextResponse.json({ success: true });
|
| 99 |
+
}
|
web/app/dashboard-pages/analyze/page.tsx
CHANGED
|
@@ -1,20 +1,21 @@
|
|
| 1 |
"use client";
|
| 2 |
|
| 3 |
-
import { useState } from "react";
|
| 4 |
import {
|
| 5 |
ScanText, ScanLine, TriangleAlert, CircleAlert, CircleCheck, Info,
|
| 6 |
-
|
| 7 |
-
ShieldCheck, ShieldAlert, Scale, Gavel, Ban, Globe, Eye, Stamp, FileX
|
|
|
|
| 8 |
} from "lucide-react";
|
| 9 |
|
| 10 |
interface Cat { name: string; severity: string; description?: string; confidence?: number; }
|
| 11 |
interface Clause { text: string; categories: Cat[]; }
|
| 12 |
interface Result { risk_score: number; grade: string; total_clauses: number; flagged_count: number; results: Clause[]; model: string; latency_ms: number; }
|
| 13 |
|
| 14 |
-
const SEV_CONFIG: Record<string, { icon: any; label: string; text: string; bg: string; border: string
|
| 15 |
-
HIGH: { icon: TriangleAlert, label: "High", text: "text-red-600", bg: "bg-red-50", border: "border-red-200"
|
| 16 |
-
MEDIUM: { icon: CircleAlert, label: "Medium", text: "text-amber-600", bg: "bg-amber-50", border: "border-amber-200"
|
| 17 |
-
LOW: { icon: Info, label: "Low", text: "text-blue-600", bg: "bg-blue-50", border: "border-blue-200"
|
| 18 |
};
|
| 19 |
|
| 20 |
const GRADE_STYLE: Record<string, string> = {
|
|
@@ -53,18 +54,58 @@ export default function AnalyzePage() {
|
|
| 53 |
const [expandedIdx, setExpandedIdx] = useState<number | null>(null);
|
| 54 |
const [filter, setFilter] = useState<string>("all");
|
| 55 |
const [copied, setCopied] = useState(false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
async function handleAnalyze() {
|
| 58 |
if (!text || text.trim().length < 50) { setError("Enter at least 50 characters."); return; }
|
|
|
|
|
|
|
|
|
|
| 59 |
setLoading(true); setError(""); setResults(null); setExpandedIdx(null);
|
| 60 |
try {
|
| 61 |
const res = await fetch("/api/analyze", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ text }) });
|
| 62 |
if (!res.ok) throw new Error((await res.json()).error || "Failed");
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
} catch (e: any) { setError(e.message); }
|
| 65 |
finally { setLoading(false); }
|
| 66 |
}
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
async function handleDownloadPDF() {
|
| 69 |
if (!results) return;
|
| 70 |
try {
|
|
@@ -94,13 +135,50 @@ export default function AnalyzePage() {
|
|
| 94 |
|
| 95 |
return (
|
| 96 |
<div className="min-h-screen bg-white">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
<div className="max-w-6xl mx-auto px-5 py-10">
|
| 98 |
-
<div className="mb-8">
|
| 99 |
-
<
|
| 100 |
-
<
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
</div>
|
| 105 |
|
| 106 |
<div className="grid lg:grid-cols-5 gap-6">
|
|
@@ -108,16 +186,21 @@ export default function AnalyzePage() {
|
|
| 108 |
<div className="lg:col-span-2">
|
| 109 |
<textarea value={text} onChange={(e) => setText(e.target.value)}
|
| 110 |
placeholder="Paste your document text here..."
|
| 111 |
-
className="w-full h-[
|
| 112 |
<div className="mt-3 flex gap-2">
|
| 113 |
<button onClick={handleAnalyze} disabled={loading}
|
| 114 |
className="flex-1 inline-flex items-center justify-center gap-2 bg-zinc-900 text-white py-2.5 rounded-lg text-sm font-medium hover:bg-zinc-800 disabled:opacity-40 transition-colors">
|
| 115 |
{loading ? <><ScanLine className="w-4 h-4 animate-pulse" /> Scanning...</> : <><ScanText className="w-4 h-4" /> Scan</>}
|
| 116 |
</button>
|
| 117 |
<button onClick={() => setText(EXAMPLE)}
|
| 118 |
-
className="px-
|
| 119 |
Example
|
| 120 |
</button>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
</div>
|
| 122 |
{error && <p className="mt-2 text-sm text-red-600 flex items-center gap-1.5"><TriangleAlert className="w-3.5 h-3.5" />{error}</p>}
|
| 123 |
</div>
|
|
@@ -140,11 +223,9 @@ export default function AnalyzePage() {
|
|
| 140 |
}`} style={{ width: `${results.risk_score}%` }} />
|
| 141 |
</div>
|
| 142 |
</div>
|
| 143 |
-
<
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
</span>
|
| 147 |
-
</div>
|
| 148 |
</div>
|
| 149 |
<div className="mt-4 flex items-center gap-4 text-xs text-zinc-400">
|
| 150 |
<span>{results.total_clauses} clauses</span>
|
|
@@ -154,7 +235,7 @@ export default function AnalyzePage() {
|
|
| 154 |
<span>{results.latency_ms}ms</span>
|
| 155 |
<span className="w-px h-3 bg-zinc-200" />
|
| 156 |
<span className="flex items-center gap-1">
|
| 157 |
-
{results.model === "ml"
|
| 158 |
{results.model === "ml" ? "Legal-BERT" : "Pattern matching"}
|
| 159 |
</span>
|
| 160 |
</div>
|
|
@@ -188,11 +269,11 @@ export default function AnalyzePage() {
|
|
| 188 |
</div>
|
| 189 |
|
| 190 |
{/* Clause list */}
|
| 191 |
-
<div className="space-y-2 max-h-[
|
| 192 |
{filtered.length === 0 ? (
|
| 193 |
<div className="border border-dashed border-zinc-200 rounded-xl p-10 text-center">
|
| 194 |
<CircleCheck className="w-8 h-8 text-emerald-400 mx-auto mb-2" />
|
| 195 |
-
<p className="text-sm text-zinc-500">{filter === "all" ? "No unfair clauses found." : "No clauses at this severity
|
| 196 |
</div>
|
| 197 |
) : filtered.map((clause, i) => {
|
| 198 |
const maxSev = clause.categories.reduce((m, c) => {
|
|
@@ -204,8 +285,7 @@ export default function AnalyzePage() {
|
|
| 204 |
const CatIcon = CATEGORY_ICONS[clause.categories[0]?.name] || TriangleAlert;
|
| 205 |
|
| 206 |
return (
|
| 207 |
-
<div key={i}
|
| 208 |
-
className={`border rounded-xl overflow-hidden transition-all ${conf.border} ${isExpanded ? "shadow-sm" : ""}`}>
|
| 209 |
<button onClick={() => setExpandedIdx(isExpanded ? null : i)}
|
| 210 |
className="w-full text-left p-4 flex items-start gap-3 hover:bg-zinc-50/50 transition-colors">
|
| 211 |
<div className={`w-8 h-8 rounded-lg flex items-center justify-center shrink-0 ${conf.bg}`}>
|
|
@@ -217,8 +297,7 @@ export default function AnalyzePage() {
|
|
| 217 |
const s = SEV_CONFIG[cat.severity] || SEV_CONFIG.MEDIUM;
|
| 218 |
return (
|
| 219 |
<span key={j} className={`text-[11px] font-medium px-2 py-0.5 rounded border ${s.bg} ${s.text} ${s.border}`}>
|
| 220 |
-
{cat.name}
|
| 221 |
-
{cat.confidence ? ` ${Math.round(cat.confidence * 100)}%` : ""}
|
| 222 |
</span>
|
| 223 |
);
|
| 224 |
})}
|
|
@@ -231,9 +310,7 @@ export default function AnalyzePage() {
|
|
| 231 |
</button>
|
| 232 |
{isExpanded && (
|
| 233 |
<div className="px-4 pb-4 pt-0 border-t border-zinc-100">
|
| 234 |
-
<p className="text-sm text-zinc-700 leading-relaxed mt-3 font-mono bg-zinc-50 rounded-lg p-3">
|
| 235 |
-
{clause.text}
|
| 236 |
-
</p>
|
| 237 |
{clause.categories.map((cat, j) => (
|
| 238 |
<div key={j} className="mt-3 flex items-start gap-2">
|
| 239 |
<TriangleAlert className={`w-3.5 h-3.5 mt-0.5 shrink-0 ${(SEV_CONFIG[cat.severity] || SEV_CONFIG.MEDIUM).text}`} />
|
|
@@ -261,7 +338,3 @@ export default function AnalyzePage() {
|
|
| 261 |
</div>
|
| 262 |
);
|
| 263 |
}
|
| 264 |
-
|
| 265 |
-
function Sparkles({ className }: { className?: string }) {
|
| 266 |
-
return <svg className={className} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M9.937 15.5A2 2 0 0 0 8.5 14.063l-6.135-1.582a.5.5 0 0 1 0-.962L8.5 9.936A2 2 0 0 0 9.937 8.5l1.582-6.135a.5.5 0 0 1 .963 0L14.063 8.5A2 2 0 0 0 15.5 9.937l6.135 1.581a.5.5 0 0 1 0 .964L15.5 14.063a2 2 0 0 0-1.437 1.437l-1.582 6.135a.5.5 0 0 1-.963 0z"/><path d="M20 3v4"/><path d="M22 5h-4"/></svg>;
|
| 267 |
-
}
|
|
|
|
| 1 |
"use client";
|
| 2 |
|
| 3 |
+
import { useState, useRef, useEffect } from "react";
|
| 4 |
import {
|
| 5 |
ScanText, ScanLine, TriangleAlert, CircleAlert, CircleCheck, Info,
|
| 6 |
+
FileDown, ChevronDown, ChevronUp, Copy, Check, Upload, FileText,
|
| 7 |
+
ShieldCheck, ShieldAlert, Scale, Gavel, Ban, Globe, Eye, Stamp, FileX,
|
| 8 |
+
Lock, Sparkles as SparklesIcon, X
|
| 9 |
} from "lucide-react";
|
| 10 |
|
| 11 |
interface Cat { name: string; severity: string; description?: string; confidence?: number; }
|
| 12 |
interface Clause { text: string; categories: Cat[]; }
|
| 13 |
interface Result { risk_score: number; grade: string; total_clauses: number; flagged_count: number; results: Clause[]; model: string; latency_ms: number; }
|
| 14 |
|
| 15 |
+
const SEV_CONFIG: Record<string, { icon: any; label: string; text: string; bg: string; border: string }> = {
|
| 16 |
+
HIGH: { icon: TriangleAlert, label: "High", text: "text-red-600", bg: "bg-red-50", border: "border-red-200" },
|
| 17 |
+
MEDIUM: { icon: CircleAlert, label: "Medium", text: "text-amber-600", bg: "bg-amber-50", border: "border-amber-200" },
|
| 18 |
+
LOW: { icon: Info, label: "Low", text: "text-blue-600", bg: "bg-blue-50", border: "border-blue-200" },
|
| 19 |
};
|
| 20 |
|
| 21 |
const GRADE_STYLE: Record<string, string> = {
|
|
|
|
| 54 |
const [expandedIdx, setExpandedIdx] = useState<number | null>(null);
|
| 55 |
const [filter, setFilter] = useState<string>("all");
|
| 56 |
const [copied, setCopied] = useState(false);
|
| 57 |
+
const [scanCount, setScanCount] = useState(0);
|
| 58 |
+
const [userPlan, setUserPlan] = useState("free");
|
| 59 |
+
const [showUpgrade, setShowUpgrade] = useState(false);
|
| 60 |
+
const fileInputRef = useRef<HTMLInputElement>(null);
|
| 61 |
+
|
| 62 |
+
const FREE_LIMIT = 10;
|
| 63 |
+
const canScan = userPlan !== "free" || scanCount < FREE_LIMIT;
|
| 64 |
|
| 65 |
async function handleAnalyze() {
|
| 66 |
if (!text || text.trim().length < 50) { setError("Enter at least 50 characters."); return; }
|
| 67 |
+
|
| 68 |
+
if (!canScan) { setShowUpgrade(true); return; }
|
| 69 |
+
|
| 70 |
setLoading(true); setError(""); setResults(null); setExpandedIdx(null);
|
| 71 |
try {
|
| 72 |
const res = await fetch("/api/analyze", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ text }) });
|
| 73 |
if (!res.ok) throw new Error((await res.json()).error || "Failed");
|
| 74 |
+
const data = await res.json();
|
| 75 |
+
setResults(data);
|
| 76 |
+
setScanCount(prev => prev + 1);
|
| 77 |
} catch (e: any) { setError(e.message); }
|
| 78 |
finally { setLoading(false); }
|
| 79 |
}
|
| 80 |
|
| 81 |
+
async function handleFileUpload(e: React.ChangeEvent<HTMLInputElement>) {
|
| 82 |
+
const file = e.target.files?.[0];
|
| 83 |
+
if (!file) return;
|
| 84 |
+
|
| 85 |
+
if (userPlan === "free") { setShowUpgrade(true); return; }
|
| 86 |
+
|
| 87 |
+
setLoading(true);
|
| 88 |
+
setError("");
|
| 89 |
+
|
| 90 |
+
try {
|
| 91 |
+
const formData = new FormData();
|
| 92 |
+
formData.append("file", file);
|
| 93 |
+
|
| 94 |
+
const res = await fetch("/api/parse-upload", { method: "POST", body: formData });
|
| 95 |
+
if (!res.ok) {
|
| 96 |
+
const err = await res.json();
|
| 97 |
+
throw new Error(err.error || "Failed to parse file");
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
const { text: extractedText } = await res.json();
|
| 101 |
+
setText(extractedText);
|
| 102 |
+
} catch (e: any) {
|
| 103 |
+
setError(e.message || "Could not read file.");
|
| 104 |
+
}
|
| 105 |
+
setLoading(false);
|
| 106 |
+
if (fileInputRef.current) fileInputRef.current.value = "";
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
async function handleDownloadPDF() {
|
| 110 |
if (!results) return;
|
| 111 |
try {
|
|
|
|
| 135 |
|
| 136 |
return (
|
| 137 |
<div className="min-h-screen bg-white">
|
| 138 |
+
{/* Upgrade modal */}
|
| 139 |
+
{showUpgrade && (
|
| 140 |
+
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/40">
|
| 141 |
+
<div className="bg-white rounded-2xl p-6 max-w-sm mx-4 shadow-xl">
|
| 142 |
+
<div className="flex justify-between items-start">
|
| 143 |
+
<div className="w-10 h-10 rounded-xl bg-amber-50 flex items-center justify-center">
|
| 144 |
+
<Lock className="w-5 h-5 text-amber-600" />
|
| 145 |
+
</div>
|
| 146 |
+
<button onClick={() => setShowUpgrade(false)} className="p-1 hover:bg-zinc-100 rounded-md"><X className="w-4 h-4 text-zinc-400" /></button>
|
| 147 |
+
</div>
|
| 148 |
+
<h3 className="mt-4 text-lg font-semibold">
|
| 149 |
+
{userPlan === "free" && scanCount >= FREE_LIMIT ? "Free limit reached" : "Pro feature"}
|
| 150 |
+
</h3>
|
| 151 |
+
<p className="mt-1.5 text-sm text-zinc-500 leading-relaxed">
|
| 152 |
+
{userPlan === "free" && scanCount >= FREE_LIMIT
|
| 153 |
+
? `You have used all ${FREE_LIMIT} free scans this month. Upgrade to Pro for unlimited scans, file uploads, and AI explanations.`
|
| 154 |
+
: "File upload is available on the Pro plan. Upgrade to scan contracts and leases directly."}
|
| 155 |
+
</p>
|
| 156 |
+
<div className="mt-5 flex gap-2">
|
| 157 |
+
<a href="/#pricing" className="flex-1 bg-zinc-900 text-white py-2.5 rounded-lg text-sm font-medium text-center hover:bg-zinc-800 transition-colors">
|
| 158 |
+
View plans
|
| 159 |
+
</a>
|
| 160 |
+
<button onClick={() => setShowUpgrade(false)} className="flex-1 border border-zinc-200 py-2.5 rounded-lg text-sm font-medium hover:bg-zinc-50 transition-colors">
|
| 161 |
+
Not now
|
| 162 |
+
</button>
|
| 163 |
+
</div>
|
| 164 |
+
</div>
|
| 165 |
+
</div>
|
| 166 |
+
)}
|
| 167 |
+
|
| 168 |
<div className="max-w-6xl mx-auto px-5 py-10">
|
| 169 |
+
<div className="mb-8 flex items-start justify-between">
|
| 170 |
+
<div>
|
| 171 |
+
<h1 className="text-2xl font-semibold tracking-tight flex items-center gap-2">
|
| 172 |
+
<ScanText className="w-6 h-6 text-zinc-400" />
|
| 173 |
+
Scan a document
|
| 174 |
+
</h1>
|
| 175 |
+
<p className="mt-1 text-sm text-zinc-500">Paste text or upload a file (.pdf, .docx, .txt).</p>
|
| 176 |
+
</div>
|
| 177 |
+
{userPlan === "free" && (
|
| 178 |
+
<span className="text-xs text-zinc-400 border border-zinc-200 px-2.5 py-1 rounded-md">
|
| 179 |
+
{scanCount}/{FREE_LIMIT} free scans
|
| 180 |
+
</span>
|
| 181 |
+
)}
|
| 182 |
</div>
|
| 183 |
|
| 184 |
<div className="grid lg:grid-cols-5 gap-6">
|
|
|
|
| 186 |
<div className="lg:col-span-2">
|
| 187 |
<textarea value={text} onChange={(e) => setText(e.target.value)}
|
| 188 |
placeholder="Paste your document text here..."
|
| 189 |
+
className="w-full h-[380px] p-4 border border-zinc-200 rounded-xl text-sm leading-relaxed resize-none focus:outline-none focus:ring-2 focus:ring-zinc-900/10 focus:border-zinc-300 placeholder:text-zinc-300 font-mono" />
|
| 190 |
<div className="mt-3 flex gap-2">
|
| 191 |
<button onClick={handleAnalyze} disabled={loading}
|
| 192 |
className="flex-1 inline-flex items-center justify-center gap-2 bg-zinc-900 text-white py-2.5 rounded-lg text-sm font-medium hover:bg-zinc-800 disabled:opacity-40 transition-colors">
|
| 193 |
{loading ? <><ScanLine className="w-4 h-4 animate-pulse" /> Scanning...</> : <><ScanText className="w-4 h-4" /> Scan</>}
|
| 194 |
</button>
|
| 195 |
<button onClick={() => setText(EXAMPLE)}
|
| 196 |
+
className="px-3 border border-zinc-200 rounded-lg text-sm text-zinc-500 hover:bg-zinc-50 transition-colors">
|
| 197 |
Example
|
| 198 |
</button>
|
| 199 |
+
<input ref={fileInputRef} type="file" accept=".txt,.md,.pdf,.docx" className="hidden" onChange={handleFileUpload} />
|
| 200 |
+
<button onClick={() => fileInputRef.current?.click()}
|
| 201 |
+
className="px-3 border border-zinc-200 rounded-lg text-zinc-500 hover:bg-zinc-50 transition-colors" title="Upload file">
|
| 202 |
+
<Upload className="w-4 h-4" />
|
| 203 |
+
</button>
|
| 204 |
</div>
|
| 205 |
{error && <p className="mt-2 text-sm text-red-600 flex items-center gap-1.5"><TriangleAlert className="w-3.5 h-3.5" />{error}</p>}
|
| 206 |
</div>
|
|
|
|
| 223 |
}`} style={{ width: `${results.risk_score}%` }} />
|
| 224 |
</div>
|
| 225 |
</div>
|
| 226 |
+
<span className={`text-sm font-semibold px-3 py-1 rounded-lg border ${GRADE_STYLE[results.grade] || GRADE_STYLE.C}`}>
|
| 227 |
+
Grade {results.grade}
|
| 228 |
+
</span>
|
|
|
|
|
|
|
| 229 |
</div>
|
| 230 |
<div className="mt-4 flex items-center gap-4 text-xs text-zinc-400">
|
| 231 |
<span>{results.total_clauses} clauses</span>
|
|
|
|
| 235 |
<span>{results.latency_ms}ms</span>
|
| 236 |
<span className="w-px h-3 bg-zinc-200" />
|
| 237 |
<span className="flex items-center gap-1">
|
| 238 |
+
{results.model === "ml" && <SparklesIcon className="w-3 h-3" />}
|
| 239 |
{results.model === "ml" ? "Legal-BERT" : "Pattern matching"}
|
| 240 |
</span>
|
| 241 |
</div>
|
|
|
|
| 269 |
</div>
|
| 270 |
|
| 271 |
{/* Clause list */}
|
| 272 |
+
<div className="space-y-2 max-h-[380px] overflow-y-auto pr-1">
|
| 273 |
{filtered.length === 0 ? (
|
| 274 |
<div className="border border-dashed border-zinc-200 rounded-xl p-10 text-center">
|
| 275 |
<CircleCheck className="w-8 h-8 text-emerald-400 mx-auto mb-2" />
|
| 276 |
+
<p className="text-sm text-zinc-500">{filter === "all" ? "No unfair clauses found." : "No clauses at this severity."}</p>
|
| 277 |
</div>
|
| 278 |
) : filtered.map((clause, i) => {
|
| 279 |
const maxSev = clause.categories.reduce((m, c) => {
|
|
|
|
| 285 |
const CatIcon = CATEGORY_ICONS[clause.categories[0]?.name] || TriangleAlert;
|
| 286 |
|
| 287 |
return (
|
| 288 |
+
<div key={i} className={`border rounded-xl overflow-hidden transition-all ${conf.border} ${isExpanded ? "shadow-sm" : ""}`}>
|
|
|
|
| 289 |
<button onClick={() => setExpandedIdx(isExpanded ? null : i)}
|
| 290 |
className="w-full text-left p-4 flex items-start gap-3 hover:bg-zinc-50/50 transition-colors">
|
| 291 |
<div className={`w-8 h-8 rounded-lg flex items-center justify-center shrink-0 ${conf.bg}`}>
|
|
|
|
| 297 |
const s = SEV_CONFIG[cat.severity] || SEV_CONFIG.MEDIUM;
|
| 298 |
return (
|
| 299 |
<span key={j} className={`text-[11px] font-medium px-2 py-0.5 rounded border ${s.bg} ${s.text} ${s.border}`}>
|
| 300 |
+
{cat.name}{cat.confidence ? ` ${Math.round(cat.confidence * 100)}%` : ""}
|
|
|
|
| 301 |
</span>
|
| 302 |
);
|
| 303 |
})}
|
|
|
|
| 310 |
</button>
|
| 311 |
{isExpanded && (
|
| 312 |
<div className="px-4 pb-4 pt-0 border-t border-zinc-100">
|
| 313 |
+
<p className="text-sm text-zinc-700 leading-relaxed mt-3 font-mono bg-zinc-50 rounded-lg p-3">{clause.text}</p>
|
|
|
|
|
|
|
| 314 |
{clause.categories.map((cat, j) => (
|
| 315 |
<div key={j} className="mt-3 flex items-start gap-2">
|
| 316 |
<TriangleAlert className={`w-3.5 h-3.5 mt-0.5 shrink-0 ${(SEV_CONFIG[cat.severity] || SEV_CONFIG.MEDIUM).text}`} />
|
|
|
|
| 338 |
</div>
|
| 339 |
);
|
| 340 |
}
|
|
|
|
|
|
|
|
|
|
|
|
web/app/dashboard-pages/team/page.tsx
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { createClient } from "@/lib/supabase/server";
|
| 2 |
+
import { redirect } from "next/navigation";
|
| 3 |
+
import Link from "next/link";
|
| 4 |
+
import { ArrowLeft, Users, UserPlus, Crown, Mail, Key, ScrollText, Trash2, Shield } from "lucide-react";
|
| 5 |
+
|
| 6 |
+
export default async function TeamPage() {
|
| 7 |
+
const supabase = await createClient();
|
| 8 |
+
const { data: { user } } = await supabase.auth.getUser();
|
| 9 |
+
if (!user) redirect("/auth/login");
|
| 10 |
+
|
| 11 |
+
const { data: profile } = await supabase.from("profiles").select("plan, team_id").eq("id", user.id).single();
|
| 12 |
+
|
| 13 |
+
if (profile?.plan !== "team") {
|
| 14 |
+
return (
|
| 15 |
+
<div className="min-h-screen bg-white flex items-center justify-center">
|
| 16 |
+
<div className="text-center max-w-sm">
|
| 17 |
+
<Users className="w-10 h-10 text-zinc-300 mx-auto mb-4" />
|
| 18 |
+
<h2 className="text-xl font-semibold">Team features</h2>
|
| 19 |
+
<p className="mt-2 text-sm text-zinc-500">Upgrade to the Team plan to invite members, share dashboards, and manage API keys.</p>
|
| 20 |
+
<Link href="/#pricing" className="mt-4 inline-block bg-zinc-900 text-white px-5 py-2.5 rounded-lg text-sm font-medium hover:bg-zinc-800">View plans</Link>
|
| 21 |
+
</div>
|
| 22 |
+
</div>
|
| 23 |
+
);
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
// Fetch team data
|
| 27 |
+
let team = null, members: any[] = [], invites: any[] = [], analyses: any[] = [];
|
| 28 |
+
|
| 29 |
+
if (profile?.team_id) {
|
| 30 |
+
const { data: t } = await supabase.from("teams").select("*").eq("id", profile.team_id).single();
|
| 31 |
+
team = t;
|
| 32 |
+
const { data: m } = await supabase.from("profiles").select("id, email, full_name, avatar_url, analyses_this_month").eq("team_id", profile.team_id);
|
| 33 |
+
members = m || [];
|
| 34 |
+
const { data: inv } = await supabase.from("team_invites").select("*").eq("team_id", profile.team_id).eq("status", "pending");
|
| 35 |
+
invites = inv || [];
|
| 36 |
+
const { data: a } = await supabase.from("analyses").select("id, source_url, risk_score, grade, flagged_count, created_at, user_id")
|
| 37 |
+
.eq("team_id", profile.team_id).order("created_at", { ascending: false }).limit(20);
|
| 38 |
+
analyses = a || [];
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
const isOwner = team?.owner_id === user.id;
|
| 42 |
+
|
| 43 |
+
return (
|
| 44 |
+
<div className="min-h-screen bg-white">
|
| 45 |
+
<div className="max-w-4xl mx-auto px-5 py-12">
|
| 46 |
+
<div className="mb-8">
|
| 47 |
+
<Link href="/dashboard-pages/dashboard" className="inline-flex items-center gap-1.5 text-sm text-zinc-400 hover:text-zinc-600">
|
| 48 |
+
<ArrowLeft className="w-3.5 h-3.5" /> Dashboard
|
| 49 |
+
</Link>
|
| 50 |
+
<h1 className="mt-4 text-2xl font-semibold tracking-tight flex items-center gap-2">
|
| 51 |
+
<Users className="w-6 h-6 text-zinc-400" />
|
| 52 |
+
{team?.name || "Team"}
|
| 53 |
+
</h1>
|
| 54 |
+
</div>
|
| 55 |
+
|
| 56 |
+
{!team ? (
|
| 57 |
+
<div className="border border-zinc-200 rounded-xl p-8 text-center">
|
| 58 |
+
<Users className="w-10 h-10 text-zinc-300 mx-auto mb-3" />
|
| 59 |
+
<h3 className="font-semibold">Create your team</h3>
|
| 60 |
+
<p className="mt-1 text-sm text-zinc-500">Set up a team to invite members and share scans.</p>
|
| 61 |
+
<form action="/api/teams" method="POST" className="mt-4">
|
| 62 |
+
<input type="hidden" name="action" value="create" />
|
| 63 |
+
<button type="submit" className="bg-zinc-900 text-white px-5 py-2.5 rounded-lg text-sm font-medium hover:bg-zinc-800">Create team</button>
|
| 64 |
+
</form>
|
| 65 |
+
</div>
|
| 66 |
+
) : (
|
| 67 |
+
<div className="space-y-8">
|
| 68 |
+
{/* Members */}
|
| 69 |
+
<section>
|
| 70 |
+
<div className="flex items-center justify-between mb-3">
|
| 71 |
+
<div className="flex items-center gap-2">
|
| 72 |
+
<Users className="w-4 h-4 text-zinc-400" />
|
| 73 |
+
<h2 className="text-sm font-medium text-zinc-500 uppercase tracking-wider">Members ({members.length}/{team.max_seats})</h2>
|
| 74 |
+
</div>
|
| 75 |
+
</div>
|
| 76 |
+
<div className="border border-zinc-200 rounded-xl divide-y divide-zinc-100">
|
| 77 |
+
{members.map((m) => (
|
| 78 |
+
<div key={m.id} className="px-5 py-3.5 flex items-center justify-between">
|
| 79 |
+
<div className="flex items-center gap-3">
|
| 80 |
+
<div className="w-8 h-8 rounded-full bg-zinc-100 flex items-center justify-center text-xs font-medium text-zinc-600">
|
| 81 |
+
{(m.full_name || m.email || "?")[0].toUpperCase()}
|
| 82 |
+
</div>
|
| 83 |
+
<div>
|
| 84 |
+
<p className="text-sm font-medium flex items-center gap-1.5">
|
| 85 |
+
{m.full_name || m.email}
|
| 86 |
+
{m.id === team.owner_id && <Crown className="w-3.5 h-3.5 text-amber-500" />}
|
| 87 |
+
</p>
|
| 88 |
+
<p className="text-xs text-zinc-400">{m.email} Β· {m.analyses_this_month || 0} scans this month</p>
|
| 89 |
+
</div>
|
| 90 |
+
</div>
|
| 91 |
+
</div>
|
| 92 |
+
))}
|
| 93 |
+
{invites.map((inv) => (
|
| 94 |
+
<div key={inv.id} className="px-5 py-3.5 flex items-center justify-between opacity-60">
|
| 95 |
+
<div className="flex items-center gap-3">
|
| 96 |
+
<div className="w-8 h-8 rounded-full bg-zinc-50 border border-dashed border-zinc-300 flex items-center justify-center">
|
| 97 |
+
<Mail className="w-3.5 h-3.5 text-zinc-400" />
|
| 98 |
+
</div>
|
| 99 |
+
<div>
|
| 100 |
+
<p className="text-sm">{inv.email}</p>
|
| 101 |
+
<p className="text-xs text-zinc-400">Invite pending</p>
|
| 102 |
+
</div>
|
| 103 |
+
</div>
|
| 104 |
+
</div>
|
| 105 |
+
))}
|
| 106 |
+
</div>
|
| 107 |
+
</section>
|
| 108 |
+
|
| 109 |
+
{/* Recent team scans */}
|
| 110 |
+
<section>
|
| 111 |
+
<div className="flex items-center gap-2 mb-3">
|
| 112 |
+
<Shield className="w-4 h-4 text-zinc-400" />
|
| 113 |
+
<h2 className="text-sm font-medium text-zinc-500 uppercase tracking-wider">Recent team scans</h2>
|
| 114 |
+
</div>
|
| 115 |
+
<div className="border border-zinc-200 rounded-xl divide-y divide-zinc-100">
|
| 116 |
+
{analyses.length === 0 ? (
|
| 117 |
+
<div className="px-5 py-8 text-center text-sm text-zinc-400">No scans yet.</div>
|
| 118 |
+
) : analyses.slice(0, 10).map((a) => {
|
| 119 |
+
const member = members.find(m => m.id === a.user_id);
|
| 120 |
+
return (
|
| 121 |
+
<div key={a.id} className="px-5 py-3 flex items-center justify-between">
|
| 122 |
+
<div>
|
| 123 |
+
<p className="text-sm text-zinc-700 truncate max-w-xs">{a.source_url || "Manual scan"}</p>
|
| 124 |
+
<p className="text-xs text-zinc-400 mt-0.5">
|
| 125 |
+
{member?.full_name || member?.email || "Unknown"} Β· {new Date(a.created_at).toLocaleDateString()}
|
| 126 |
+
</p>
|
| 127 |
+
</div>
|
| 128 |
+
<span className={`text-xs font-semibold px-2.5 py-1 rounded-md ${
|
| 129 |
+
a.grade === "F" || a.grade === "D" ? "bg-red-50 text-red-700" :
|
| 130 |
+
a.grade === "C" ? "bg-amber-50 text-amber-700" : "bg-emerald-50 text-emerald-700"
|
| 131 |
+
}`}>{a.grade} Β· {a.risk_score}</span>
|
| 132 |
+
</div>
|
| 133 |
+
);
|
| 134 |
+
})}
|
| 135 |
+
</div>
|
| 136 |
+
</section>
|
| 137 |
+
</div>
|
| 138 |
+
)}
|
| 139 |
+
</div>
|
| 140 |
+
</div>
|
| 141 |
+
);
|
| 142 |
+
}
|
web/components/nav.tsx
CHANGED
|
@@ -2,8 +2,9 @@
|
|
| 2 |
|
| 3 |
import Link from "next/link";
|
| 4 |
import { usePathname } from "next/navigation";
|
| 5 |
-
import { ShieldCheck, Menu, X } from "lucide-react";
|
| 6 |
-
import { useState } from "react";
|
|
|
|
| 7 |
|
| 8 |
const links = [
|
| 9 |
{ href: "/#features", label: "Features" },
|
|
@@ -11,10 +12,21 @@ const links = [
|
|
| 11 |
{ href: "/dashboard-pages/analyze", label: "Scanner" },
|
| 12 |
];
|
| 13 |
|
|
|
|
|
|
|
| 14 |
export function Nav() {
|
| 15 |
const [open, setOpen] = useState(false);
|
|
|
|
| 16 |
const pathname = usePathname();
|
| 17 |
const isDashboard = pathname?.startsWith("/dashboard");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
return (
|
| 20 |
<nav className="sticky top-0 z-50 bg-white/80 backdrop-blur-md border-b border-zinc-100">
|
|
@@ -24,7 +36,6 @@ export function Nav() {
|
|
| 24 |
<span className="font-semibold text-[15px] tracking-tight text-zinc-900">ClauseGuard</span>
|
| 25 |
</Link>
|
| 26 |
|
| 27 |
-
{/* Desktop */}
|
| 28 |
<div className="hidden md:flex items-center gap-1">
|
| 29 |
{links.map((l) => (
|
| 30 |
<a key={l.href} href={l.href}
|
|
@@ -32,8 +43,17 @@ export function Nav() {
|
|
| 32 |
{l.label}
|
| 33 |
</a>
|
| 34 |
))}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
<div className="w-px h-4 bg-zinc-200 mx-2" />
|
| 36 |
-
|
|
|
|
| 37 |
<Link href="/dashboard-pages/settings"
|
| 38 |
className="px-3 py-1.5 text-[13px] text-zinc-500 hover:text-zinc-900 rounded-md hover:bg-zinc-50">
|
| 39 |
Settings
|
|
@@ -44,27 +64,27 @@ export function Nav() {
|
|
| 44 |
Log in
|
| 45 |
</Link>
|
| 46 |
)}
|
| 47 |
-
<Link href={isDashboard ? "/dashboard-pages/analyze" : "/auth/signup"}
|
| 48 |
className="ml-1 px-3.5 py-1.5 text-[13px] font-medium text-white bg-zinc-900 rounded-md hover:bg-zinc-800 transition-colors">
|
| 49 |
-
{isDashboard ? "New scan" : "Get started"}
|
| 50 |
</Link>
|
| 51 |
</div>
|
| 52 |
|
| 53 |
-
{/* Mobile toggle */}
|
| 54 |
<button className="md:hidden p-1.5 rounded-md hover:bg-zinc-100" onClick={() => setOpen(!open)}>
|
| 55 |
{open ? <X className="w-5 h-5" /> : <Menu className="w-5 h-5" />}
|
| 56 |
</button>
|
| 57 |
</div>
|
| 58 |
|
| 59 |
-
{/* Mobile menu */}
|
| 60 |
{open && (
|
| 61 |
<div className="md:hidden border-t border-zinc-100 bg-white px-5 py-3 space-y-1">
|
| 62 |
{links.map((l) => (
|
| 63 |
<a key={l.href} href={l.href} onClick={() => setOpen(false)}
|
| 64 |
-
className="block px-3 py-2 text-sm text-zinc-600 rounded-md hover:bg-zinc-50">
|
| 65 |
-
{l.label}
|
| 66 |
-
</a>
|
| 67 |
))}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
<Link href="/auth/login" onClick={() => setOpen(false)}
|
| 69 |
className="block px-3 py-2 text-sm text-zinc-600 rounded-md hover:bg-zinc-50">Log in</Link>
|
| 70 |
</div>
|
|
|
|
| 2 |
|
| 3 |
import Link from "next/link";
|
| 4 |
import { usePathname } from "next/navigation";
|
| 5 |
+
import { ShieldCheck, Menu, X, Crown } from "lucide-react";
|
| 6 |
+
import { useState, useEffect } from "react";
|
| 7 |
+
import { createClient } from "@/lib/supabase/client";
|
| 8 |
|
| 9 |
const links = [
|
| 10 |
{ href: "/#features", label: "Features" },
|
|
|
|
| 12 |
{ href: "/dashboard-pages/analyze", label: "Scanner" },
|
| 13 |
];
|
| 14 |
|
| 15 |
+
const ADMIN_EMAILS = ["ankygaur9972@gmail.com"];
|
| 16 |
+
|
| 17 |
export function Nav() {
|
| 18 |
const [open, setOpen] = useState(false);
|
| 19 |
+
const [userEmail, setUserEmail] = useState<string | null>(null);
|
| 20 |
const pathname = usePathname();
|
| 21 |
const isDashboard = pathname?.startsWith("/dashboard");
|
| 22 |
+
const isAdmin = userEmail && ADMIN_EMAILS.includes(userEmail);
|
| 23 |
+
|
| 24 |
+
useEffect(() => {
|
| 25 |
+
const supabase = createClient();
|
| 26 |
+
supabase.auth.getUser().then(({ data }) => {
|
| 27 |
+
setUserEmail(data.user?.email || null);
|
| 28 |
+
});
|
| 29 |
+
}, []);
|
| 30 |
|
| 31 |
return (
|
| 32 |
<nav className="sticky top-0 z-50 bg-white/80 backdrop-blur-md border-b border-zinc-100">
|
|
|
|
| 36 |
<span className="font-semibold text-[15px] tracking-tight text-zinc-900">ClauseGuard</span>
|
| 37 |
</Link>
|
| 38 |
|
|
|
|
| 39 |
<div className="hidden md:flex items-center gap-1">
|
| 40 |
{links.map((l) => (
|
| 41 |
<a key={l.href} href={l.href}
|
|
|
|
| 43 |
{l.label}
|
| 44 |
</a>
|
| 45 |
))}
|
| 46 |
+
|
| 47 |
+
{isAdmin && (
|
| 48 |
+
<Link href="/admin"
|
| 49 |
+
className="px-3 py-1.5 text-[13px] text-amber-600 hover:text-amber-700 rounded-md hover:bg-amber-50 transition-colors flex items-center gap-1">
|
| 50 |
+
<Crown className="w-3.5 h-3.5" /> Admin
|
| 51 |
+
</Link>
|
| 52 |
+
)}
|
| 53 |
+
|
| 54 |
<div className="w-px h-4 bg-zinc-200 mx-2" />
|
| 55 |
+
|
| 56 |
+
{isDashboard || userEmail ? (
|
| 57 |
<Link href="/dashboard-pages/settings"
|
| 58 |
className="px-3 py-1.5 text-[13px] text-zinc-500 hover:text-zinc-900 rounded-md hover:bg-zinc-50">
|
| 59 |
Settings
|
|
|
|
| 64 |
Log in
|
| 65 |
</Link>
|
| 66 |
)}
|
| 67 |
+
<Link href={isDashboard || userEmail ? "/dashboard-pages/analyze" : "/auth/signup"}
|
| 68 |
className="ml-1 px-3.5 py-1.5 text-[13px] font-medium text-white bg-zinc-900 rounded-md hover:bg-zinc-800 transition-colors">
|
| 69 |
+
{isDashboard || userEmail ? "New scan" : "Get started"}
|
| 70 |
</Link>
|
| 71 |
</div>
|
| 72 |
|
|
|
|
| 73 |
<button className="md:hidden p-1.5 rounded-md hover:bg-zinc-100" onClick={() => setOpen(!open)}>
|
| 74 |
{open ? <X className="w-5 h-5" /> : <Menu className="w-5 h-5" />}
|
| 75 |
</button>
|
| 76 |
</div>
|
| 77 |
|
|
|
|
| 78 |
{open && (
|
| 79 |
<div className="md:hidden border-t border-zinc-100 bg-white px-5 py-3 space-y-1">
|
| 80 |
{links.map((l) => (
|
| 81 |
<a key={l.href} href={l.href} onClick={() => setOpen(false)}
|
| 82 |
+
className="block px-3 py-2 text-sm text-zinc-600 rounded-md hover:bg-zinc-50">{l.label}</a>
|
|
|
|
|
|
|
| 83 |
))}
|
| 84 |
+
{isAdmin && (
|
| 85 |
+
<Link href="/admin" onClick={() => setOpen(false)}
|
| 86 |
+
className="block px-3 py-2 text-sm text-amber-600 rounded-md hover:bg-amber-50">Admin</Link>
|
| 87 |
+
)}
|
| 88 |
<Link href="/auth/login" onClick={() => setOpen(false)}
|
| 89 |
className="block px-3 py-2 text-sm text-zinc-600 rounded-md hover:bg-zinc-50">Log in</Link>
|
| 90 |
</div>
|
web/lib/admin-guard.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { createClient } from "@/lib/supabase/server";
|
| 2 |
+
import { redirect } from "next/navigation";
|
| 3 |
+
|
| 4 |
+
const ADMIN_EMAILS = ["ankygaur9972@gmail.com"];
|
| 5 |
+
|
| 6 |
+
export async function requireAdmin() {
|
| 7 |
+
const supabase = await createClient();
|
| 8 |
+
const { data: { user } } = await supabase.auth.getUser();
|
| 9 |
+
|
| 10 |
+
if (!user) redirect("/auth/login");
|
| 11 |
+
|
| 12 |
+
// Check email first (fast)
|
| 13 |
+
if (!ADMIN_EMAILS.includes(user.email || "")) {
|
| 14 |
+
redirect("/dashboard-pages/dashboard");
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
// Double check role in DB
|
| 18 |
+
const { data: profile } = await supabase
|
| 19 |
+
.from("profiles")
|
| 20 |
+
.select("role")
|
| 21 |
+
.eq("id", user.id)
|
| 22 |
+
.single();
|
| 23 |
+
|
| 24 |
+
if (profile?.role !== "admin") {
|
| 25 |
+
redirect("/dashboard-pages/dashboard");
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
return { user, supabase };
|
| 29 |
+
}
|
web/lib/supabase/admin-schema.sql
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-- ClauseGuard β Admin Schema Extension
|
| 2 |
+
-- Run AFTER the main schema.sql
|
| 3 |
+
|
| 4 |
+
-- Add role column to profiles
|
| 5 |
+
ALTER TABLE public.profiles ADD COLUMN IF NOT EXISTS role TEXT DEFAULT 'user' CHECK (role IN ('user', 'admin'));
|
| 6 |
+
|
| 7 |
+
-- Add is_banned column
|
| 8 |
+
ALTER TABLE public.profiles ADD COLUMN IF NOT EXISTS is_banned BOOLEAN DEFAULT false;
|
| 9 |
+
|
| 10 |
+
-- Set admin
|
| 11 |
+
UPDATE public.profiles SET role = 'admin' WHERE email = 'ankygaur9972@gmail.com';
|
| 12 |
+
|
| 13 |
+
-- Activity log
|
| 14 |
+
CREATE TABLE IF NOT EXISTS public.admin_logs (
|
| 15 |
+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
| 16 |
+
admin_id UUID REFERENCES auth.users NOT NULL,
|
| 17 |
+
action TEXT NOT NULL,
|
| 18 |
+
target_type TEXT, -- 'user', 'team', 'scan', 'api_key', 'rule'
|
| 19 |
+
target_id TEXT,
|
| 20 |
+
details JSONB,
|
| 21 |
+
created_at TIMESTAMPTZ DEFAULT NOW()
|
| 22 |
+
);
|
| 23 |
+
|
| 24 |
+
CREATE INDEX IF NOT EXISTS idx_admin_logs_created ON public.admin_logs(created_at DESC);
|
| 25 |
+
|
| 26 |
+
-- Admin RLS: admins can read all tables
|
| 27 |
+
CREATE POLICY "Admins can read all profiles" ON public.profiles FOR SELECT
|
| 28 |
+
USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
|
| 29 |
+
|
| 30 |
+
CREATE POLICY "Admins can update all profiles" ON public.profiles FOR UPDATE
|
| 31 |
+
USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
|
| 32 |
+
|
| 33 |
+
CREATE POLICY "Admins can read all analyses" ON public.analyses FOR SELECT
|
| 34 |
+
USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
|
| 35 |
+
|
| 36 |
+
CREATE POLICY "Admins can read all teams" ON public.teams FOR SELECT
|
| 37 |
+
USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
|
| 38 |
+
|
| 39 |
+
CREATE POLICY "Admins can read all api_keys" ON public.api_keys FOR SELECT
|
| 40 |
+
USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
|
| 41 |
+
|
| 42 |
+
CREATE POLICY "Admins can read all custom_rules" ON public.custom_rules FOR SELECT
|
| 43 |
+
USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
|
| 44 |
+
|
| 45 |
+
ALTER TABLE public.admin_logs ENABLE ROW LEVEL SECURITY;
|
| 46 |
+
CREATE POLICY "Admins can manage logs" ON public.admin_logs FOR ALL
|
| 47 |
+
USING (auth.uid() IN (SELECT id FROM public.profiles WHERE role = 'admin'));
|
web/lib/supabase/client.ts
CHANGED
|
@@ -3,6 +3,15 @@ import { createBrowserClient } from "@supabase/ssr";
|
|
| 3 |
export function createClient() {
|
| 4 |
return createBrowserClient(
|
| 5 |
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
| 6 |
-
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
);
|
| 8 |
}
|
|
|
|
| 3 |
export function createClient() {
|
| 4 |
return createBrowserClient(
|
| 5 |
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
| 6 |
+
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
|
| 7 |
+
{
|
| 8 |
+
auth: {
|
| 9 |
+
autoRefreshToken: true,
|
| 10 |
+
persistSession: true,
|
| 11 |
+
// 7 day session β Supabase default is 3600s (1 hour)
|
| 12 |
+
// This must also be set in Supabase Dashboard β Auth β Settings β JWT Expiry
|
| 13 |
+
// Set to 604800 (7 days) in the dashboard
|
| 14 |
+
},
|
| 15 |
+
}
|
| 16 |
);
|
| 17 |
}
|
web/lib/supabase/schema.sql
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
-- ClauseGuard β
|
| 2 |
|
| 3 |
-
-- Profiles
|
| 4 |
CREATE TABLE IF NOT EXISTS public.profiles (
|
| 5 |
id UUID REFERENCES auth.users ON DELETE CASCADE PRIMARY KEY,
|
| 6 |
email TEXT,
|
|
@@ -8,16 +8,41 @@ CREATE TABLE IF NOT EXISTS public.profiles (
|
|
| 8 |
avatar_url TEXT,
|
| 9 |
razorpay_subscription_id TEXT,
|
| 10 |
plan TEXT DEFAULT 'free' CHECK (plan IN ('free', 'pro', 'team')),
|
|
|
|
| 11 |
analyses_this_month INT DEFAULT 0,
|
| 12 |
monthly_reset_at TIMESTAMPTZ DEFAULT date_trunc('month', NOW()),
|
| 13 |
created_at TIMESTAMPTZ DEFAULT NOW(),
|
| 14 |
updated_at TIMESTAMPTZ DEFAULT NOW()
|
| 15 |
);
|
| 16 |
|
| 17 |
-
--
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
CREATE TABLE IF NOT EXISTS public.analyses (
|
| 19 |
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
| 20 |
user_id UUID REFERENCES public.profiles ON DELETE CASCADE NOT NULL,
|
|
|
|
| 21 |
source_url TEXT,
|
| 22 |
source_type TEXT DEFAULT 'tos' CHECK (source_type IN ('tos', 'contract', 'rental', 'other')),
|
| 23 |
total_clauses INT NOT NULL,
|
|
@@ -28,21 +53,86 @@ CREATE TABLE IF NOT EXISTS public.analyses (
|
|
| 28 |
created_at TIMESTAMPTZ DEFAULT NOW()
|
| 29 |
);
|
| 30 |
|
| 31 |
-
--
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
CREATE INDEX IF NOT EXISTS idx_analyses_user_id ON public.analyses(user_id);
|
|
|
|
| 33 |
CREATE INDEX IF NOT EXISTS idx_analyses_created_at ON public.analyses(created_at DESC);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
-- Row Level Security
|
| 36 |
ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;
|
| 37 |
ALTER TABLE public.analyses ENABLE ROW LEVEL SECURITY;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
CREATE POLICY "Users
|
| 41 |
-
|
| 42 |
-
CREATE POLICY "Users
|
| 43 |
-
CREATE POLICY "Users can delete own analyses" ON public.analyses FOR DELETE USING (auth.uid() = user_id);
|
| 44 |
|
| 45 |
-
-- Auto-create profile on signup
|
| 46 |
CREATE OR REPLACE FUNCTION public.handle_new_user()
|
| 47 |
RETURNS TRIGGER AS $$
|
| 48 |
BEGIN
|
|
@@ -56,16 +146,17 @@ BEGIN
|
|
| 56 |
END;
|
| 57 |
$$ LANGUAGE plpgsql SECURITY DEFINER;
|
| 58 |
|
| 59 |
-
|
|
|
|
| 60 |
AFTER INSERT ON auth.users
|
| 61 |
FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();
|
| 62 |
|
| 63 |
-
-- Monthly
|
| 64 |
CREATE OR REPLACE FUNCTION public.reset_monthly_usage()
|
| 65 |
RETURNS void AS $$
|
| 66 |
BEGIN
|
| 67 |
-
UPDATE public.profiles
|
| 68 |
-
SET analyses_this_month = 0, monthly_reset_at = date_trunc('month', NOW())
|
| 69 |
WHERE monthly_reset_at < date_trunc('month', NOW());
|
|
|
|
| 70 |
END;
|
| 71 |
$$ LANGUAGE plpgsql SECURITY DEFINER;
|
|
|
|
| 1 |
+
-- ClauseGuard β Full Database Schema (with Teams, API Keys, Custom Rules)
|
| 2 |
|
| 3 |
+
-- βββ Profiles βββ
|
| 4 |
CREATE TABLE IF NOT EXISTS public.profiles (
|
| 5 |
id UUID REFERENCES auth.users ON DELETE CASCADE PRIMARY KEY,
|
| 6 |
email TEXT,
|
|
|
|
| 8 |
avatar_url TEXT,
|
| 9 |
razorpay_subscription_id TEXT,
|
| 10 |
plan TEXT DEFAULT 'free' CHECK (plan IN ('free', 'pro', 'team')),
|
| 11 |
+
team_id UUID REFERENCES public.teams(id) ON DELETE SET NULL,
|
| 12 |
analyses_this_month INT DEFAULT 0,
|
| 13 |
monthly_reset_at TIMESTAMPTZ DEFAULT date_trunc('month', NOW()),
|
| 14 |
created_at TIMESTAMPTZ DEFAULT NOW(),
|
| 15 |
updated_at TIMESTAMPTZ DEFAULT NOW()
|
| 16 |
);
|
| 17 |
|
| 18 |
+
-- βββ Teams βββ
|
| 19 |
+
CREATE TABLE IF NOT EXISTS public.teams (
|
| 20 |
+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
| 21 |
+
name TEXT NOT NULL,
|
| 22 |
+
owner_id UUID REFERENCES auth.users NOT NULL,
|
| 23 |
+
plan TEXT DEFAULT 'team',
|
| 24 |
+
max_seats INT DEFAULT 5,
|
| 25 |
+
razorpay_subscription_id TEXT,
|
| 26 |
+
created_at TIMESTAMPTZ DEFAULT NOW()
|
| 27 |
+
);
|
| 28 |
+
|
| 29 |
+
CREATE TABLE IF NOT EXISTS public.team_invites (
|
| 30 |
+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
| 31 |
+
team_id UUID REFERENCES public.teams ON DELETE CASCADE NOT NULL,
|
| 32 |
+
email TEXT NOT NULL,
|
| 33 |
+
role TEXT DEFAULT 'member' CHECK (role IN ('admin', 'member')),
|
| 34 |
+
status TEXT DEFAULT 'pending' CHECK (status IN ('pending', 'accepted', 'expired')),
|
| 35 |
+
invited_by UUID REFERENCES auth.users NOT NULL,
|
| 36 |
+
created_at TIMESTAMPTZ DEFAULT NOW(),
|
| 37 |
+
expires_at TIMESTAMPTZ DEFAULT NOW() + INTERVAL '7 days',
|
| 38 |
+
UNIQUE(team_id, email)
|
| 39 |
+
);
|
| 40 |
+
|
| 41 |
+
-- βββ Analyses βββ
|
| 42 |
CREATE TABLE IF NOT EXISTS public.analyses (
|
| 43 |
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
| 44 |
user_id UUID REFERENCES public.profiles ON DELETE CASCADE NOT NULL,
|
| 45 |
+
team_id UUID REFERENCES public.teams ON DELETE SET NULL,
|
| 46 |
source_url TEXT,
|
| 47 |
source_type TEXT DEFAULT 'tos' CHECK (source_type IN ('tos', 'contract', 'rental', 'other')),
|
| 48 |
total_clauses INT NOT NULL,
|
|
|
|
| 53 |
created_at TIMESTAMPTZ DEFAULT NOW()
|
| 54 |
);
|
| 55 |
|
| 56 |
+
-- βββ API Keys βββ
|
| 57 |
+
CREATE TABLE IF NOT EXISTS public.api_keys (
|
| 58 |
+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
| 59 |
+
user_id UUID REFERENCES public.profiles ON DELETE CASCADE NOT NULL,
|
| 60 |
+
team_id UUID REFERENCES public.teams ON DELETE CASCADE,
|
| 61 |
+
name TEXT NOT NULL,
|
| 62 |
+
key_hash TEXT NOT NULL UNIQUE,
|
| 63 |
+
key_prefix TEXT NOT NULL, -- first 8 chars for display: "cg_live_a1b2..."
|
| 64 |
+
calls_this_month INT DEFAULT 0,
|
| 65 |
+
calls_limit INT DEFAULT 1000, -- Pro: 1000, Team: 10000
|
| 66 |
+
last_used_at TIMESTAMPTZ,
|
| 67 |
+
is_active BOOLEAN DEFAULT true,
|
| 68 |
+
created_at TIMESTAMPTZ DEFAULT NOW()
|
| 69 |
+
);
|
| 70 |
+
|
| 71 |
+
-- βββ Custom Clause Rules βββ
|
| 72 |
+
CREATE TABLE IF NOT EXISTS public.custom_rules (
|
| 73 |
+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
| 74 |
+
user_id UUID REFERENCES public.profiles ON DELETE CASCADE,
|
| 75 |
+
team_id UUID REFERENCES public.teams ON DELETE CASCADE,
|
| 76 |
+
name TEXT NOT NULL,
|
| 77 |
+
description TEXT,
|
| 78 |
+
pattern TEXT NOT NULL, -- regex pattern to match
|
| 79 |
+
severity TEXT DEFAULT 'MEDIUM' CHECK (severity IN ('HIGH', 'MEDIUM', 'LOW')),
|
| 80 |
+
category TEXT NOT NULL, -- custom category name
|
| 81 |
+
is_active BOOLEAN DEFAULT true,
|
| 82 |
+
created_at TIMESTAMPTZ DEFAULT NOW(),
|
| 83 |
+
updated_at TIMESTAMPTZ DEFAULT NOW()
|
| 84 |
+
);
|
| 85 |
+
|
| 86 |
+
-- βββ Indexes βββ
|
| 87 |
CREATE INDEX IF NOT EXISTS idx_analyses_user_id ON public.analyses(user_id);
|
| 88 |
+
CREATE INDEX IF NOT EXISTS idx_analyses_team_id ON public.analyses(team_id);
|
| 89 |
CREATE INDEX IF NOT EXISTS idx_analyses_created_at ON public.analyses(created_at DESC);
|
| 90 |
+
CREATE INDEX IF NOT EXISTS idx_api_keys_key_hash ON public.api_keys(key_hash);
|
| 91 |
+
CREATE INDEX IF NOT EXISTS idx_api_keys_user_id ON public.api_keys(user_id);
|
| 92 |
+
CREATE INDEX IF NOT EXISTS idx_team_invites_email ON public.team_invites(email);
|
| 93 |
+
CREATE INDEX IF NOT EXISTS idx_custom_rules_user_id ON public.custom_rules(user_id);
|
| 94 |
+
CREATE INDEX IF NOT EXISTS idx_custom_rules_team_id ON public.custom_rules(team_id);
|
| 95 |
|
| 96 |
+
-- βββ Row Level Security βββ
|
| 97 |
ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;
|
| 98 |
ALTER TABLE public.analyses ENABLE ROW LEVEL SECURITY;
|
| 99 |
+
ALTER TABLE public.teams ENABLE ROW LEVEL SECURITY;
|
| 100 |
+
ALTER TABLE public.team_invites ENABLE ROW LEVEL SECURITY;
|
| 101 |
+
ALTER TABLE public.api_keys ENABLE ROW LEVEL SECURITY;
|
| 102 |
+
ALTER TABLE public.custom_rules ENABLE ROW LEVEL SECURITY;
|
| 103 |
+
|
| 104 |
+
-- Profiles
|
| 105 |
+
CREATE POLICY "Users see own profile" ON public.profiles FOR SELECT USING (auth.uid() = id);
|
| 106 |
+
CREATE POLICY "Users update own profile" ON public.profiles FOR UPDATE USING (auth.uid() = id);
|
| 107 |
+
|
| 108 |
+
-- Analyses: own + team
|
| 109 |
+
CREATE POLICY "Users see own analyses" ON public.analyses FOR SELECT
|
| 110 |
+
USING (auth.uid() = user_id OR team_id IN (SELECT team_id FROM public.profiles WHERE id = auth.uid()));
|
| 111 |
+
CREATE POLICY "Users insert analyses" ON public.analyses FOR INSERT WITH CHECK (auth.uid() = user_id);
|
| 112 |
+
CREATE POLICY "Users delete own analyses" ON public.analyses FOR DELETE USING (auth.uid() = user_id);
|
| 113 |
+
|
| 114 |
+
-- Teams: members can view, owner can update
|
| 115 |
+
CREATE POLICY "Team members can view" ON public.teams FOR SELECT
|
| 116 |
+
USING (id IN (SELECT team_id FROM public.profiles WHERE id = auth.uid()) OR owner_id = auth.uid());
|
| 117 |
+
CREATE POLICY "Owner can update team" ON public.teams FOR UPDATE USING (owner_id = auth.uid());
|
| 118 |
+
|
| 119 |
+
-- Team invites: team members can view, admins can insert
|
| 120 |
+
CREATE POLICY "Members see team invites" ON public.team_invites FOR SELECT
|
| 121 |
+
USING (team_id IN (SELECT team_id FROM public.profiles WHERE id = auth.uid()));
|
| 122 |
+
CREATE POLICY "Admins can invite" ON public.team_invites FOR INSERT
|
| 123 |
+
WITH CHECK (invited_by = auth.uid());
|
| 124 |
+
|
| 125 |
+
-- API Keys: own + team
|
| 126 |
+
CREATE POLICY "Users see own API keys" ON public.api_keys FOR SELECT
|
| 127 |
+
USING (user_id = auth.uid() OR team_id IN (SELECT team_id FROM public.profiles WHERE id = auth.uid()));
|
| 128 |
+
CREATE POLICY "Users manage own API keys" ON public.api_keys FOR ALL USING (user_id = auth.uid());
|
| 129 |
|
| 130 |
+
-- Custom Rules: own + team
|
| 131 |
+
CREATE POLICY "Users see own rules" ON public.custom_rules FOR SELECT
|
| 132 |
+
USING (user_id = auth.uid() OR team_id IN (SELECT team_id FROM public.profiles WHERE id = auth.uid()));
|
| 133 |
+
CREATE POLICY "Users manage own rules" ON public.custom_rules FOR ALL USING (user_id = auth.uid());
|
|
|
|
| 134 |
|
| 135 |
+
-- βββ Auto-create profile on signup βββ
|
| 136 |
CREATE OR REPLACE FUNCTION public.handle_new_user()
|
| 137 |
RETURNS TRIGGER AS $$
|
| 138 |
BEGIN
|
|
|
|
| 146 |
END;
|
| 147 |
$$ LANGUAGE plpgsql SECURITY DEFINER;
|
| 148 |
|
| 149 |
+
DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users;
|
| 150 |
+
CREATE TRIGGER on_auth_user_created
|
| 151 |
AFTER INSERT ON auth.users
|
| 152 |
FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();
|
| 153 |
|
| 154 |
+
-- βββ Monthly reset βββ
|
| 155 |
CREATE OR REPLACE FUNCTION public.reset_monthly_usage()
|
| 156 |
RETURNS void AS $$
|
| 157 |
BEGIN
|
| 158 |
+
UPDATE public.profiles SET analyses_this_month = 0, monthly_reset_at = date_trunc('month', NOW())
|
|
|
|
| 159 |
WHERE monthly_reset_at < date_trunc('month', NOW());
|
| 160 |
+
UPDATE public.api_keys SET calls_this_month = 0;
|
| 161 |
END;
|
| 162 |
$$ LANGUAGE plpgsql SECURITY DEFINER;
|
web/next.config.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { NextConfig } from "next";
|
|
| 2 |
|
| 3 |
const nextConfig: NextConfig = {
|
| 4 |
output: "standalone",
|
| 5 |
-
serverExternalPackages: ["@react-pdf/renderer"],
|
| 6 |
images: {
|
| 7 |
remotePatterns: [
|
| 8 |
{ protocol: "https", hostname: "avatars.githubusercontent.com" },
|
|
|
|
| 2 |
|
| 3 |
const nextConfig: NextConfig = {
|
| 4 |
output: "standalone",
|
| 5 |
+
serverExternalPackages: ["@react-pdf/renderer", "pdf-parse", "mammoth"],
|
| 6 |
images: {
|
| 7 |
remotePatterns: [
|
| 8 |
{ protocol: "https", hostname: "avatars.githubusercontent.com" },
|
web/package.json
CHANGED
|
@@ -17,6 +17,8 @@
|
|
| 17 |
"razorpay": "2.9.6",
|
| 18 |
"resend": "6.12.2",
|
| 19 |
"@react-pdf/renderer": "4.5.1",
|
|
|
|
|
|
|
| 20 |
"jose": "6.2.2",
|
| 21 |
"lucide-react": "0.474.0",
|
| 22 |
"clsx": "2.1.1",
|
|
|
|
| 17 |
"razorpay": "2.9.6",
|
| 18 |
"resend": "6.12.2",
|
| 19 |
"@react-pdf/renderer": "4.5.1",
|
| 20 |
+
"pdf-parse": "2.4.5",
|
| 21 |
+
"mammoth": "1.12.0",
|
| 22 |
"jose": "6.2.2",
|
| 23 |
"lucide-react": "0.474.0",
|
| 24 |
"clsx": "2.1.1",
|