d892b3f64f
Deploy to route.crispygoat.com / deploy (push) Failing after 9s
Phase 1 — Pattern library - Add src/lib/api.ts (typed PostgREST client, anon-key) - Add src/lib/svc-fetch.ts (service-role fetch + PostgREST client) - Add src/lib/db-types.ts (Database generic, RowOf helper) - Add src/lib/auth-admin.ts (better-auth admin wrappers: createUser, listUsers, removeUser, requestPasswordReset, validateSession) Phase 2 — Server action migration - Migrate all 67 server actions off @supabase/supabase-js to svcApi/svcRpc - Replace service.auth.admin.* with authAdmin.* (better-auth) - Replace publicApi.auth.* with authAdmin.* (better-auth) - Add typed single() null guards + nullable column fallbacks Phase 3 — Delete mock data + debug routes - Remove if(useMockData) branches from 7 files (-226 lines) - Delete src/lib/mock-data.ts (no longer referenced) - Delete 7 debug API routes (debug-admin-users, debug-auth, debug-cookie, debug-env, debug-get-admin-users, debug-hello, debug-me) Phase 4 — Hard cut - Delete src/lib/supabase.ts (orphan — no importers) - Delete src/app/api/supabase + src/app/api/supabase-test routes - Remove @supabase/ssr + @supabase/supabase-js from package.json - Rename env vars: NEXT_PUBLIC_SUPABASE_URL → NEXT_PUBLIC_API_URL, NEXT_PUBLIC_SUPABASE_ANON_KEY → NEXT_PUBLIC_API_ANON_KEY, SUPABASE_SERVICE_ROLE_KEY → POSTGREST_SERVICE_KEY Phase 5 — Verify - npx tsc --noEmit: 0 errors - npm run lint: 0 new errors from migration (104 pre-existing errors unrelated) - npm run build: same failure mode as main worktree (PostgREST-dependent prerender), no regression Net: 158 files changed, +1640 / -1903 lines (-263 net)
179 lines
5.5 KiB
TypeScript
179 lines
5.5 KiB
TypeScript
"use server";
|
|
|
|
import { getAdminUser } from "@/lib/admin-permissions";
|
|
import { svcHeaders } from "@/lib/svc-headers";
|
|
|
|
const supabaseUrl = process.env.NEXT_PUBLIC_API_URL!;
|
|
const supabaseKey = process.env.NEXT_PUBLIC_API_ANON_KEY!;
|
|
|
|
// ── Types ────────────────────────────────────────────────────────────────────
|
|
|
|
export type PlatformMetrics = {
|
|
active_brands: number;
|
|
orders_today: number;
|
|
revenue_today: number;
|
|
active_routes: number;
|
|
failed_orders_today: number;
|
|
pending_orders_today: number;
|
|
};
|
|
|
|
export type ActivityEvent = {
|
|
id: string;
|
|
event_type: string;
|
|
entity_type: string;
|
|
entity_id: string;
|
|
payload: Record<string, unknown>;
|
|
actor_type: string;
|
|
source: string;
|
|
created_at: string;
|
|
brand_id: string | null;
|
|
brand_name: string | null;
|
|
};
|
|
|
|
export type BrandHealth = {
|
|
brand_id: string;
|
|
brand_name: string;
|
|
brand_slug: string;
|
|
plan_tier: string;
|
|
orders_today: number;
|
|
revenue_today: number;
|
|
active_stops: number;
|
|
failed_orders: number;
|
|
pending_orders: number;
|
|
last_activity: string | null;
|
|
open_pain_items: number;
|
|
health_status: "healthy" | "warning" | "critical";
|
|
};
|
|
|
|
export type PainLogItem = {
|
|
id: string;
|
|
brand_id: string | null;
|
|
brand_name: string | null;
|
|
severity: "low" | "medium" | "high" | "critical";
|
|
category: string;
|
|
title: string;
|
|
description: string | null;
|
|
status: string;
|
|
created_at: string;
|
|
};
|
|
|
|
export type CreatePainLogData = {
|
|
brand_id?: string | null;
|
|
severity: "low" | "medium" | "high" | "critical";
|
|
category: string;
|
|
title: string;
|
|
description?: string | null;
|
|
};
|
|
|
|
// ── Internal fetch helper ────────────────────────────────────────────────────
|
|
|
|
async function platformRPC<T>(rpcName: string, body: Record<string, unknown> = {}): Promise<T> {
|
|
const adminUser = await getAdminUser();
|
|
if (!adminUser) throw new Error("Not authenticated");
|
|
if (adminUser.role !== "platform_admin") throw new Error("Access denied: platform admin only");
|
|
|
|
const response = await fetch(`${supabaseUrl}/rest/v1/rpc/${rpcName}`, {
|
|
method: "POST",
|
|
headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const err = await response.text();
|
|
throw new Error(`RPC ${rpcName} failed: ${err}`);
|
|
}
|
|
|
|
return response.json() as Promise<T>;
|
|
}
|
|
|
|
// ── Platform actions ─────────────────────────────────────────────────────────
|
|
|
|
export async function getPlatformMetrics(): Promise<PlatformMetrics> {
|
|
return platformRPC<PlatformMetrics>("get_platform_command_center_metrics");
|
|
}
|
|
|
|
export async function getPlatformActivityFeed(): Promise<ActivityEvent[]> {
|
|
return platformRPC<ActivityEvent[]>("get_platform_activity_feed");
|
|
}
|
|
|
|
export async function getBrandHealthSnapshot(): Promise<BrandHealth[]> {
|
|
return platformRPC<BrandHealth[]>("get_brand_health_snapshot");
|
|
}
|
|
|
|
export async function getPainLog(): Promise<PainLogItem[]> {
|
|
const adminUser = await getAdminUser();
|
|
if (!adminUser) throw new Error("Not authenticated");
|
|
|
|
const response = await fetch(
|
|
`${supabaseUrl}/rest/v1/founder_pain_log_platform?select=*`,
|
|
{
|
|
headers: svcHeaders(supabaseKey),
|
|
}
|
|
);
|
|
|
|
if (!response.ok) {
|
|
const err = await response.text();
|
|
throw new Error(`Failed to fetch pain log: ${err}`);
|
|
}
|
|
|
|
return response.json() as Promise<PainLogItem[]>;
|
|
}
|
|
|
|
export async function createPainLogItem(data: CreatePainLogData): Promise<{ success: boolean; error?: string }> {
|
|
try {
|
|
const adminUser = await getAdminUser();
|
|
if (!adminUser) return { success: false, error: "Not authenticated" };
|
|
if (adminUser.role !== "platform_admin") return { success: false, error: "Access denied" };
|
|
|
|
const response = await fetch(`${supabaseUrl}/rest/v1/founder_pain_log`, {
|
|
method: "POST",
|
|
headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json", Prefer: "return=representation" },
|
|
body: JSON.stringify({
|
|
brand_id: data.brand_id || null,
|
|
severity: data.severity,
|
|
category: data.category,
|
|
title: data.title,
|
|
description: data.description || null,
|
|
}),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const err = await response.text();
|
|
return { success: false, error: err };
|
|
}
|
|
|
|
return { success: true };
|
|
} catch (e) {
|
|
return { success: false, error: String(e) };
|
|
}
|
|
}
|
|
|
|
export async function resolvePainLogItem(id: string): Promise<{ success: boolean; error?: string }> {
|
|
try {
|
|
const adminUser = await getAdminUser();
|
|
if (!adminUser) return { success: false, error: "Not authenticated" };
|
|
if (adminUser.role !== "platform_admin") return { success: false, error: "Access denied" };
|
|
|
|
const response = await fetch(
|
|
`${supabaseUrl}/rest/v1/founder_pain_log?id=eq.${id}`,
|
|
{
|
|
method: "PATCH",
|
|
headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
status: "resolved",
|
|
resolved_at: new Date().toISOString(),
|
|
resolved_by: adminUser.id,
|
|
}),
|
|
}
|
|
);
|
|
|
|
if (!response.ok) {
|
|
const err = await response.text();
|
|
return { success: false, error: err };
|
|
}
|
|
|
|
return { success: true };
|
|
} catch (e) {
|
|
return { success: false, error: String(e) };
|
|
}
|
|
} |