mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
fixed leaderboard, added feature to drop a team out of the teams table once it has hit 0 members, that check for members occurs when a user leaves a team. need to fix error with the timer in admin dashboard, make a separate page for it to be centrally run rather than be based on session instances.
Grey = cant click Red = current Green = complete blue = skipped? Need to add Clue page Need to fix Leaderboard.
This commit is contained in:
1 parent
c098ad40bf
commit
a762528494
5 files changed
+276
-108
No files matched your search
@@ -0,0 +1,86 @@
|
||||
import { query, queryOne, run } from "@/lib/db";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
|
||||
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, token };
|
||||
}
|
||||
|
||||
export async function GET(request) {
|
||||
try {
|
||||
// Verify admin access
|
||||
const { authorized, error, status } = await verifyAdmin(request);
|
||||
if (!authorized) {
|
||||
return Response.json({ error }, { status });
|
||||
}
|
||||
|
||||
// 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 member count and clues found
|
||||
const teams = await query(`
|
||||
SELECT
|
||||
t.id,
|
||||
t.name,
|
||||
t.code,
|
||||
COUNT(DISTINCT u.id) as memberCount,
|
||||
COUNT(DISTINCT tc.clue_id) as cluesFound,
|
||||
MAX(tc.found_at) as lastActivity
|
||||
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
|
||||
`);
|
||||
|
||||
// Enhance team data with total clues
|
||||
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("Admin leaderboard error:", error);
|
||||
return Response.json(
|
||||
{ error: "Failed to fetch leaderboard data: " + error.message },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user