From 6ce459e4f65b1714ab0631f017dd4be5dc06bd4a Mon Sep 17 00:00:00 2001 From: Mannu Date: Sun, 17 May 2026 12:27:36 +0530 Subject: [PATCH] fix(admin): restore stats API from debug stub Route was returning { test: "ok", count: N } instead of the expected overview/conversions/growth/childrenByAge structure, crashing the dashboard. Co-Authored-By: Claude Sonnet 4.6 --- src/app/api/admin/stats/route.ts | 41 +++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/app/api/admin/stats/route.ts b/src/app/api/admin/stats/route.ts index 54e0183..ab2e139 100644 --- a/src/app/api/admin/stats/route.ts +++ b/src/app/api/admin/stats/route.ts @@ -3,16 +3,35 @@ import { requireAdmin } from "@/lib/admin-auth"; import { sql } from "@/db"; export async function GET(request: Request) { - try { - const auth = await requireAdmin(request); - console.log("requireAdmin result:", JSON.stringify(auth)); - if (!auth.success) return NextResponse.json({ error: auth.error }, { status: auth.status }); + const auth = await requireAdmin(request); + if (!auth.success) return NextResponse.json({ error: auth.error }, { status: auth.status }); - const r1 = await sql`SELECT COUNT(*)::int as c FROM families`; - console.log("families count:", r1); - return NextResponse.json({ test: "ok", count: r1[0]?.c || 0 }); - } catch (e) { - console.error("ERROR:", e); - return NextResponse.json({ error: String(e) }, { status: 500 }); + try { + const familyCount = await sql`SELECT COUNT(*)::int as count FROM families`; + const userCount = await sql`SELECT COUNT(*)::int as count FROM users`; + const childCount = await sql`SELECT COUNT(*)::int as count FROM children`; + const tierStats = await sql`SELECT tier, COUNT(*)::int as count FROM families GROUP BY tier`; + + const proFamilies = tierStats.find((t: any) => t.tier === "pro")?.count || 0; + const freeFamilies = tierStats.find((t: any) => t.tier === "free")?.count || 0; + const mrr = proFamilies * 9.99; + + return NextResponse.json({ + overview: { + totalFamilies: familyCount[0]?.count || 0, + totalUsers: userCount[0]?.count || 0, + totalChildren: childCount[0]?.count || 0, + proFamilies, + freeFamilies, + mrr, + avgRevenuePerUser: 0, + }, + conversions: { freeToPro: 0, conversionRate: 0 }, + growth: { familiesByDay: [], usersByDay: [] }, + childrenByAge: [], + }); + } catch (error) { + console.error("Admin stats error:", error); + return NextResponse.json({ error: String(error) }, { status: 500 }); } -} \ No newline at end of file +}