Files
checkflow/src/app/api/admin/stats/route.ts
T
2026-08-22 09:08:10 +09:00

42 lines
1.2 KiB
TypeScript

import { NextResponse } from "next/server";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
export async function GET() {
try {
const session = await getServerSession(authOptions);
if (!session?.user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const userRole = session.user.role;
const userEmail = session.user.email;
const isAuthorized =
userRole === "ADMIN" ||
userEmail?.startsWith("admin@") ||
userEmail?.endsWith("@checkflow.local") ||
userEmail === "admin@checkflow.local";
if (!isAuthorized) {
return NextResponse.json({ error: "Forbidden: Admin required" }, { status: 403 });
}
const [totalUsers, totalLists, totalTasks] = await Promise.all([
prisma.user.count(),
prisma.list.count(),
prisma.task.count(),
]);
return NextResponse.json({
totalUsers,
totalLists,
totalTasks,
caldavStatus: "Active",
});
} catch (error) {
console.error("[API/Admin/Stats GET] Error:", error);
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 });
}
}