mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
Grey = cant click Red = current Green = complete blue = skipped? Need to add Clue page Need to fix Leaderboard.
74 lines
2.0 KiB
JavaScript
74 lines
2.0 KiB
JavaScript
import { query, queryOne } from "@/lib/db";
|
|
import { getToken } from "next-auth/jwt";
|
|
|
|
export const dynamic = "force-dynamic"; // Disable caching
|
|
|
|
export async function GET(request) {
|
|
try {
|
|
// Verify authentication - users need to be logged in to see leaderboard
|
|
const token = await getToken({
|
|
req: request,
|
|
secret:
|
|
process.env.NEXTAUTH_SECRET ||
|
|
"your-fallback-secret-should-be-at-least-32-chars",
|
|
});
|
|
|
|
if (!token) {
|
|
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
// Get the total number of clues for calculating progress
|
|
const clueCountResult = await queryOne(
|
|
"SELECT COUNT(*) as total FROM clues"
|
|
);
|
|
const totalClues = clueCountResult?.total || 0;
|
|
|
|
// Get all teams with progress info and member count
|
|
// Only include teams that have at least one member
|
|
const teams = await query(`
|
|
SELECT
|
|
t.id,
|
|
t.name,
|
|
t.code,
|
|
COUNT(DISTINCT tc.clue_id) as cluesFound,
|
|
MAX(tc.found_at) as lastActivity,
|
|
COUNT(DISTINCT u.id) as memberCount
|
|
FROM
|
|
teams t
|
|
LEFT JOIN
|
|
users u ON t.id = u.team_id
|
|
LEFT JOIN
|
|
team_clues tc ON t.id = tc.team_id
|
|
GROUP BY
|
|
t.id
|
|
HAVING
|
|
memberCount > 0
|
|
ORDER BY
|
|
cluesFound DESC,
|
|
lastActivity ASC
|
|
`);
|
|
|
|
// Add progress percentage and total clues to each team
|
|
const teamsWithProgress = teams.map((team) => ({
|
|
...team,
|
|
totalClues,
|
|
progress:
|
|
totalClues > 0 ? Math.round((team.cluesFound / totalClues) * 100) : 0,
|
|
}));
|
|
|
|
return Response.json(teamsWithProgress, {
|
|
headers: {
|
|
"Cache-Control": "no-store, no-cache, must-revalidate",
|
|
Pragma: "no-cache",
|
|
Expires: "0",
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("Leaderboard error:", error);
|
|
return Response.json(
|
|
{ error: "Failed to fetch leaderboard data" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|