buildmymcpserver/apps/web/app/admin/builds/page.tsx
Marco Sadjadi c62fcd07ef feat(admin): password-auth admin panel with 8 pages + 15 API endpoints
Schema migrations:
- users.is_admin boolean
- users.password_hash text (scrypt N=16384, 16-byte salt)
- users.last_login_at timestamp
- organizations.suspended + suspended_reason
- admin_settings table (DB-stored prompt override + future settings)

Auth (@bmm/auth):
- hashPassword + verifyPassword via node:crypto scrypt (no extra dep)
- loginWithPassword: scrypt-verifies, issues 30-day session, updates last_login_at
- seedAdmin: idempotent upsert keyed on email; creates org + membership on first run
- AuthedUser now carries isAdmin flag

API:
- POST /v1/auth/admin/login (email + password) — 300ms throttle on failure
- requireAdmin preHandler — 401 if no session, 403 if non-admin
- Bootstrap: api on boot calls seedAdmin(ADMIN_EMAIL, ADMIN_PASSWORD, ADMIN_NAME)
  if env present. Idempotent.

Admin API routes (all gated by requireAdmin):
- GET /v1/admin/overview (totals, trends 7d, server-status breakdown, builds 24h, recent activity)
- GET /v1/admin/users (search, per-row org + plan + serverCount)
- PATCH /v1/admin/users/:id (isAdmin, name)
- DELETE /v1/admin/users/:id (self-delete blocked)
- GET /v1/admin/orgs (member + server counts)
- PATCH /v1/admin/orgs/:id (plan, quota, suspended; cascades to mcp_servers.status=paused on suspend)
- GET /v1/admin/servers (cross-org with status filter)
- POST /v1/admin/servers/:id/rebuild (re-queues build using last prompt)
- DELETE /v1/admin/servers/:id
- GET /v1/admin/builds (status filter, error messages, prompt previews)
- GET /v1/admin/builds/:id/logs
- GET /v1/admin/audit (system-wide with user email join)
- GET /v1/admin/system (DB ping, Redis ping, BullMQ queue depth, docker ps count)
- GET /v1/admin/prompt (builtin + override + updatedAt)
- PATCH /v1/admin/prompt (value: string | null) — saves DB override or drops it

UI (apps/web/app/admin/*):
- /admin/login — password form, separate from /login magic-link
- AdminLayout — Linear-style sidebar (8 nav items), bottom panel with user email +
  'user view' shortcut + logout, client-side requireAdmin guard with redirect
- /admin — overview dashboard with 4 metric cards, 2 panels (status + 24h builds),
  recent activity table linking to full audit
- /admin/users — search + admin toggle + delete (self-delete blocked)
- /admin/orgs — plan/quota/suspend actions via prompts
- /admin/servers — cross-org table with rebuild + delete actions, status filter
- /admin/builds — every build cross-fleet with error vs prompt preview
- /admin/audit — system-wide log + CSV export + filter dropdowns
- /admin/system — auto-refreshing 5s health probes for Postgres, Redis, queue, Docker
- /admin/prompt — live editor for the LLM system prompt with built-in baseline,
  override-state badge, drop-override action, diff preview, save-as-override

End-to-end verified: login as marco.frangiskatos@gmail.com + Melusa112233.*, every
admin page returns 200, admin login + overview tested via screenshot, docker probe
returns true count of running MCP containers.
2026-05-19 23:01:26 +02:00

108 lines
4.0 KiB
TypeScript

'use client';
import { useEffect, useState } from 'react';
import { apiFetch } from '@/lib/api';
import { StatusPill } from '@/components/status-pill';
interface Row {
build: {
id: string;
version: number;
prompt: string;
status: string;
errorMessage: string | null;
startedAt: string | null;
finishedAt: string | null;
createdAt: string;
};
server: { id: string; name: string; slug: string };
org: { id: string; name: string };
}
const STATUS_FILTERS = ['', 'success', 'failed', 'queued', 'generating', 'building', 'deploying', 'cancelled'];
export default function AdminBuildsPage() {
const [rows, setRows] = useState<Row[] | null>(null);
const [status, setStatus] = useState('');
useEffect(() => {
apiFetch<{ builds: Row[] }>(`/v1/admin/builds${status ? `?status=${status}` : ''}`)
.then((r) => setRows(r.builds))
.catch(() => setRows([]));
}, [status]);
return (
<div className="px-8 py-8">
<header className="mb-6">
<h1 className="text-[22px] font-semibold tracking-tight">Builds</h1>
<p className="mt-1 text-[13px] text-[--color-fg-muted]">
Every spec image deploy run across the fleet.
</p>
</header>
<div className="mb-4 flex gap-2">
<select
value={status}
onChange={(e) => setStatus(e.target.value)}
className="h-8 rounded-md border border-[--color-border] bg-[--color-bg-subtle] px-2 text-[13px] focus:border-[--color-accent] focus:outline-none"
>
{STATUS_FILTERS.map((s) => (
<option key={s} value={s}>
{s ? s : 'All statuses'}
</option>
))}
</select>
</div>
<div className="panel">
{rows === null && (
<p className="px-4 py-3 text-[12.5px] text-[--color-fg-muted]">Loading</p>
)}
{rows && rows.length === 0 && (
<p className="px-4 py-12 text-center text-[13px] text-[--color-fg-muted]">No builds.</p>
)}
{rows && rows.length > 0 && (
<table className="w-full text-[12px]">
<thead className="border-b border-[--color-border] text-[--color-fg-subtle]">
<tr>
<th className="px-4 py-2 text-left font-medium">When</th>
<th className="px-4 py-2 text-left font-medium">Server</th>
<th className="px-4 py-2 text-left font-medium">Org</th>
<th className="px-4 py-2 text-left font-medium">Status</th>
<th className="px-4 py-2 text-left font-medium">Prompt / Error</th>
</tr>
</thead>
<tbody>
{rows.map((r) => (
<tr key={r.build.id} className="border-b border-[--color-border] last:border-0 align-top">
<td className="px-4 py-2 mono text-[--color-fg-muted] whitespace-nowrap">
{new Date(r.build.createdAt).toLocaleString()}
<div className="text-[10px] text-[--color-fg-subtle]">v{r.build.version}</div>
</td>
<td className="px-4 py-2">
<div className="font-medium">{r.server.name}</div>
<div className="mono text-[10.5px] text-[--color-fg-subtle]">{r.server.slug}</div>
</td>
<td className="px-4 py-2 text-[--color-fg-muted]">{r.org.name}</td>
<td className="px-4 py-2">
<StatusPill status={r.build.status as never} />
</td>
<td className="px-4 py-2 mono text-[--color-fg-muted] max-w-[500px]">
{r.build.errorMessage ? (
<div className="text-[--color-danger] whitespace-pre-wrap">
{r.build.errorMessage}
</div>
) : (
<div className="line-clamp-3 whitespace-pre-wrap">{r.build.prompt}</div>
)}
</td>
</tr>
))}
</tbody>
</table>
)}
</div>
</div>
);
}