wulf-pulse/app/api/admin/integrations/route.ts
lorentz e1427b62d7 feat(admin): DB-backed integration toggles + sticky cols + dark contrast
Builds on the env-var INTEGRATIONS_DISABLED shipped with the nav-design
overhaul.  Adds a DB-backed admin UI so operators can flip integrations
without editing .env and restarting the container, plus the remaining
visual cleanup items from the design backlog.

Integration toggles
- Migration 081 — integration_settings table (key PK, disabled flag,
  reason, disabled_by audit, disabled_at).  Seeded with all 13 known
  integrations as enabled.
- GET / PATCH /api/admin/integrations — gated by requirePermission
  (admin, access).  PATCH clears the in-process integration-health
  cache so toggles take effect within seconds.
- /admin/integrations admin page with a Switch per integration, optional
  reason input, audit-info subtitle (disabled by, when, why), live
  status light from /api/dashboard/integration-health.
- integration-health service merges env-var disable list with DB rows;
  degrades gracefully if migration unapplied / DB unreachable.
- Wired into the Admin nav dropdown (eight items now).
- CLAUDE.md describes both env + DB sources.

Sticky first column on tables
- Table primitive accepts stickyFirstColumn?: boolean.  When true, TH
  and TD :first-child stay pinned during horizontal scroll, with
  background inheritance preserving hover and selected row tints.
- DataTable exposes the prop too — on by default for paginated tables.
- /addigy-devices opts in.

Dark-mode contrast
- --border lifted from 10% to 14% in .dark; --input from 15% to 18%;
  --sidebar-border to 14%.
- StatusLight outline ring lifted from /10 to /15 (light) and /20 (dark).
- DetailModal empty-cell em-dash lifted from /40 to /70 so missing
  values are legible on dark surfaces.

DESIGN.md
- Closed sticky-first-column, dark-mode contrast, and palette-audit
  items (palette deprioritized — most uses are semantic).
- Skeleton helpers documented as preferred for new code; existing
  ad-hoc patterns left in place.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 09:55:22 -04:00

99 lines
3.4 KiB
TypeScript

/**
* GET /api/admin/integrations — list every integration_settings row
* PATCH /api/admin/integrations — body: { key, disabled, reason? }
*
* Backed by the `integration_settings` table (migration 081). Toggling
* here takes effect within the 5-minute health cache without restarting
* the container. Forces a cache refresh on PATCH so the UI reflects the
* change immediately.
*/
import { NextRequest, NextResponse } from 'next/server';
import { requirePermission } from '@/lib/auth-utils';
import postgresClient from '@/lib/services/postgres-client';
import { clearIntegrationHealthCache } from '@/lib/services/integration-health';
interface IntegrationSettingRow {
key: string;
disabled: boolean;
disabled_reason: string | null;
disabled_by: string | null;
disabled_at: string | null;
updated_at: string;
}
interface IntegrationSetting {
key: string;
disabled: boolean;
reason: string | null;
disabledBy: string | null;
disabledAt: string | null;
updatedAt: string;
}
function rowToDto(r: IntegrationSettingRow): IntegrationSetting {
return {
key: r.key,
disabled: r.disabled,
reason: r.disabled_reason,
disabledBy: r.disabled_by,
disabledAt: r.disabled_at,
updatedAt: r.updated_at,
};
}
export async function GET() {
const { error } = await requirePermission('admin', 'access');
if (error) return error;
const res = await postgresClient.query<IntegrationSettingRow>(
`SELECT key, disabled, disabled_reason, disabled_by, disabled_at::text,
updated_at::text
FROM integration_settings
ORDER BY key`,
);
return NextResponse.json({ items: res.rows.map(rowToDto) });
}
export async function PATCH(request: NextRequest) {
const { session, error } = await requirePermission('admin', 'access');
if (error) return error;
let body: { key?: unknown; disabled?: unknown; reason?: unknown };
try {
body = (await request.json()) as typeof body;
} catch {
return NextResponse.json({ error: 'Invalid JSON body' }, { status: 400 });
}
const key = typeof body.key === 'string' ? body.key.trim() : '';
const disabled = body.disabled === true;
const reason =
typeof body.reason === 'string' && body.reason.trim().length > 0
? body.reason.trim().slice(0, 500)
: null;
if (!key) {
return NextResponse.json({ error: '`key` is required' }, { status: 400 });
}
const actor = (session?.user as { email?: string } | undefined)?.email ?? null;
const res = await postgresClient.query<IntegrationSettingRow>(
`INSERT INTO integration_settings (key, disabled, disabled_reason, disabled_by, disabled_at, updated_at)
VALUES ($1, $2, $3, $4, CASE WHEN $2 THEN NOW() ELSE NULL END, NOW())
ON CONFLICT (key) DO UPDATE
SET disabled = EXCLUDED.disabled,
disabled_reason = CASE WHEN EXCLUDED.disabled THEN EXCLUDED.disabled_reason ELSE NULL END,
disabled_by = CASE WHEN EXCLUDED.disabled THEN EXCLUDED.disabled_by ELSE NULL END,
disabled_at = CASE WHEN EXCLUDED.disabled THEN NOW() ELSE NULL END,
updated_at = NOW()
RETURNING key, disabled, disabled_reason, disabled_by, disabled_at::text, updated_at::text`,
[key, disabled, reason, disabled ? actor : null],
);
// Force the next /api/dashboard/integration-health request to re-check.
clearIntegrationHealthCache();
return NextResponse.json({ item: rowToDto(res.rows[0]) });
}