|
| 1 | +import { NextResponse } from "next/server"; |
| 2 | +import { createClient } from "@supabase/supabase-js"; |
| 3 | + |
| 4 | +export const runtime = "nodejs"; |
| 5 | + |
| 6 | +/** |
| 7 | + * GET /api/community/stats |
| 8 | + * |
| 9 | + * Returns anonymized, aggregated community statistics. |
| 10 | + * No authentication required — publicly accessible. |
| 11 | + * Payload is precomputed by the hourly Inngest rollup function. |
| 12 | + * |
| 13 | + * Uses a raw Supabase client (not typed) because community_rollups |
| 14 | + * is not yet in the generated database types. |
| 15 | + */ |
| 16 | +export async function GET() { |
| 17 | + const url = process.env.NEXT_PUBLIC_SUPABASE_URL; |
| 18 | + const key = process.env.SUPABASE_SERVICE_ROLE_KEY; |
| 19 | + |
| 20 | + if (!url || !key) { |
| 21 | + return NextResponse.json({ error: "server_misconfigured" }, { status: 500 }); |
| 22 | + } |
| 23 | + |
| 24 | + const supabase = createClient(url, key); |
| 25 | + |
| 26 | + const { data, error } = await supabase |
| 27 | + .from("community_rollups") |
| 28 | + .select("payload_json") |
| 29 | + .eq("rollup_window", "30d") |
| 30 | + .order("as_of_date", { ascending: false }) |
| 31 | + .limit(1) |
| 32 | + .maybeSingle(); |
| 33 | + |
| 34 | + if (error) { |
| 35 | + console.error("Failed to fetch community rollup:", error.message); |
| 36 | + return NextResponse.json( |
| 37 | + { error: "internal_error" }, |
| 38 | + { status: 500 } |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + if (!data) { |
| 43 | + return NextResponse.json( |
| 44 | + { |
| 45 | + suppressed: true, |
| 46 | + reason: "no_data_yet", |
| 47 | + eligible_profiles: 0, |
| 48 | + threshold: 10, |
| 49 | + }, |
| 50 | + { |
| 51 | + headers: { |
| 52 | + "Cache-Control": "public, s-maxage=60, stale-while-revalidate=300", |
| 53 | + }, |
| 54 | + } |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + return NextResponse.json(data.payload_json, { |
| 59 | + headers: { |
| 60 | + "Cache-Control": "public, s-maxage=300, stale-while-revalidate=3600", |
| 61 | + }, |
| 62 | + }); |
| 63 | +} |
0 commit comments