Files
sofa/app/api/stats/route.ts
T
jakeandClaude Opus 4.6 1b7f64d327 Add dynamic period selector to dashboard stats cards
Movies and Episodes cards now have an interactive dropdown to switch
between Today, This Week, This Month, and This Year. Extracts reusable
getWatchCount helper in discovery service and adds /api/stats route.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 20:42:41 -05:00

40 lines
1.1 KiB
TypeScript

import { headers } from "next/headers";
import { NextResponse } from "next/server";
import { auth } from "@/lib/auth/server";
import { getWatchCount, type TimePeriod } from "@/lib/services/discovery";
const validTypes = ["movies", "episodes"] as const;
const validPeriods: TimePeriod[] = [
"today",
"this_week",
"this_month",
"this_year",
];
export async function GET(request: Request) {
const session = await auth.api.getSession({ headers: await headers() });
if (!session)
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const { searchParams } = new URL(request.url);
const type = searchParams.get("type");
const period = searchParams.get("period");
if (
!type ||
!period ||
!validTypes.includes(type as (typeof validTypes)[number]) ||
!validPeriods.includes(period as TimePeriod)
) {
return NextResponse.json({ error: "Invalid parameters" }, { status: 400 });
}
const count = getWatchCount(
session.user.id,
type as "movies" | "episodes",
period as TimePeriod,
);
return NextResponse.json({ count });
}