Files
karate_halloween/app/api/portal/stats/route.ts
T

43 lines
1.3 KiB
TypeScript
Raw Normal View History

import { NextRequest, NextResponse } from "next/server";
import { isAuthenticated } from "@/lib/admin-auth";
import { getGenerationStats } from "@/lib/storage";
export async function GET(req: NextRequest) {
if (!isAuthenticated(req)) {
return NextResponse.json(
{ error: "Unauthorized. Please login." },
{ status: 401 }
);
}
try {
const { searchParams } = new URL(req.url);
const periodParam = searchParams.get("period") as any;
const rawFromDate = searchParams.get("from") || undefined;
const rawToDate = searchParams.get("to") || undefined;
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
const fromDate = rawFromDate && dateRegex.test(rawFromDate) ? rawFromDate : undefined;
const toDate = rawToDate && dateRegex.test(rawToDate) ? rawToDate : undefined;
const validPeriods = ["day", "week", "month", "all", "custom"];
const period = validPeriods.includes(periodParam) ? periodParam : "week";
const stats = getGenerationStats({
period,
fromDate,
toDate,
});
return NextResponse.json({
success: true,
stats,
});
} catch (error: any) {
console.error("[Portal Stats API] Error:", error);
return NextResponse.json(
{ success: false, error: "Failed to retrieve statistics" },
{ status: 500 }
);
}
}