fantasy-manager/pages/api/admin/analytics.ts
Lukas Schaefer d3a5fe88f9
switch to kysely (#863)
Signed-off-by: Lukas Schaefer <lukas@lschaefer.xyz>
2025-05-09 19:12:32 -04:00

42 lines
1.1 KiB
TypeScript

import db from "#database";
import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth";
import { authOptions } from "#/pages/api/auth/[...nextauth]";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
// Check if the user is logged in and an admin
const user = await getServerSession(req, res, authOptions);
if (!user) {
res.status(401).end("Not logged in");
return;
}
if (!user.user.admin) {
res.status(403).end("Not an admin");
return;
}
switch (req.method) {
case "GET":
const day = parseInt(String(req.query.day));
// Gets analytics data
const result = await db
.selectFrom("analytics")
.selectAll()
.where("day", ">=", day)
.where("day", "<", day + 50)
.execute();
if (
process.env.APP_ENV !== "development" &&
process.env.APP_ENV !== "test"
) {
res.setHeader("Cache-Control", `private, max-age=108000`);
}
res.status(200).json(result);
break;
default:
res.status(405).end(`Method ${req.method} Not Allowed`);
break;
}
}