mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
bug fixes and added stats viewer to admin leaderboard.
This commit is contained in:
1 parent
7075a816c3
commit
d60f52fef3
7 files changed
+1046
-233
No files matched your search
@@ -0,0 +1,98 @@
|
||||
import { getToken } from "next-auth/jwt";
|
||||
import { queryOne, query, run } from "@/lib/db";
|
||||
|
||||
export const dynamic = "force-dynamic"; // Disable caching
|
||||
|
||||
// Ensure the user is an admin
|
||||
async function verifyAdmin(request) {
|
||||
const token = await getToken({
|
||||
req: request,
|
||||
secret:
|
||||
process.env.NEXTAUTH_SECRET ||
|
||||
"your-fallback-secret-should-be-at-least-32-chars",
|
||||
});
|
||||
|
||||
if (!token) {
|
||||
return { authorized: false, error: "Unauthorized", status: 401 };
|
||||
}
|
||||
|
||||
if (token.role !== "admin") {
|
||||
return { authorized: false, error: "Admin access required", status: 403 };
|
||||
}
|
||||
|
||||
return { authorized: true };
|
||||
}
|
||||
|
||||
// GET - Fetch detailed stats for a specific team
|
||||
export async function GET(request, context) {
|
||||
try {
|
||||
// Verify admin access
|
||||
const { authorized, error, status } = await verifyAdmin(request);
|
||||
if (!authorized) {
|
||||
return Response.json({ error }, { status });
|
||||
}
|
||||
|
||||
// Get teamId parameter
|
||||
const { teamId } = await context.params;
|
||||
|
||||
// Validate teamId is a number
|
||||
if (isNaN(parseInt(teamId))) {
|
||||
return Response.json({ error: "Invalid team ID" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Check if team exists
|
||||
const team = await queryOne("SELECT id, name FROM teams WHERE id = ?", [
|
||||
teamId,
|
||||
]);
|
||||
if (!team) {
|
||||
return Response.json({ error: "Team not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
// Get all found clues (not skipped)
|
||||
const foundClues = await query(
|
||||
`
|
||||
SELECT c.id, c.title, tc.found_at
|
||||
FROM team_clues tc
|
||||
JOIN clues c ON tc.clue_id = c.id
|
||||
WHERE tc.team_id = ? AND (tc.skipped = 0 OR tc.skipped IS NULL)
|
||||
ORDER BY tc.found_at ASC
|
||||
`,
|
||||
[teamId]
|
||||
);
|
||||
|
||||
// Get all skipped clues
|
||||
const skippedClues = await query(
|
||||
`
|
||||
SELECT c.id, c.title, tc.found_at
|
||||
FROM team_clues tc
|
||||
JOIN clues c ON tc.clue_id = c.id
|
||||
WHERE tc.team_id = ? AND tc.skipped = 1
|
||||
ORDER BY tc.found_at ASC
|
||||
`,
|
||||
[teamId]
|
||||
);
|
||||
|
||||
// Return the data
|
||||
return Response.json(
|
||||
{
|
||||
teamId: parseInt(teamId),
|
||||
teamName: team.name,
|
||||
foundClues,
|
||||
skippedClues,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
"Cache-Control": "no-store, no-cache, must-revalidate",
|
||||
Pragma: "no-cache",
|
||||
Expires: "0",
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Error fetching team stats:", error);
|
||||
return Response.json(
|
||||
{ error: "Failed to fetch team stats: " + error.message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user