mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-08 03:50:21 +00:00
Grey = cant click Red = current Green = complete blue = skipped? Need to add Clue page Need to fix Leaderboard.
87 lines
2.4 KiB
JavaScript
87 lines
2.4 KiB
JavaScript
import { getToken } from "next-auth/jwt";
|
|
import { queryOne, run } from "@/lib/db";
|
|
|
|
export async function GET(request, context) {
|
|
try {
|
|
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 });
|
|
}
|
|
|
|
// Properly await context.params
|
|
const { teamId } = await context.params;
|
|
|
|
// Validate that user belongs to this team
|
|
if (!token.teamId || parseInt(token.teamId, 10) !== parseInt(teamId, 10)) {
|
|
return Response.json(
|
|
{ error: "You are not a member of this team" },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
// First ensure the team_hunt_sessions table exists
|
|
await run(`
|
|
CREATE TABLE IF NOT EXISTS team_hunt_sessions (
|
|
team_id INTEGER PRIMARY KEY,
|
|
start_time TEXT NOT NULL,
|
|
initiated_by INTEGER,
|
|
FOREIGN KEY (team_id) REFERENCES teams(id),
|
|
FOREIGN KEY (initiated_by) REFERENCES users(id)
|
|
)
|
|
`);
|
|
|
|
// Get the team's hunt session from the database
|
|
const session = await queryOne(
|
|
"SELECT team_id, start_time FROM team_hunt_sessions WHERE team_id = ?",
|
|
[teamId]
|
|
);
|
|
|
|
if (!session) {
|
|
// Try to get the global hunt start time as fallback
|
|
const huntStatus = await queryOne(
|
|
"SELECT start_time FROM hunt_status WHERE is_active = 1 LIMIT 1"
|
|
);
|
|
if (huntStatus && huntStatus.start_time) {
|
|
return Response.json({
|
|
startTime: huntStatus.start_time,
|
|
source: "global_hunt_status",
|
|
});
|
|
}
|
|
|
|
// Final fallback to settings table
|
|
const huntActive = await queryOne(
|
|
"SELECT value FROM settings WHERE key = 'hunt_active'"
|
|
);
|
|
if (huntActive && huntActive.value === "true") {
|
|
return Response.json({
|
|
startTime: new Date().toISOString(),
|
|
source: "fallback_current_time",
|
|
});
|
|
}
|
|
|
|
return Response.json(
|
|
{ error: "No hunt session found for this team" },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
return Response.json({
|
|
teamId: session.team_id,
|
|
startTime: session.start_time,
|
|
source: "team_session",
|
|
});
|
|
} catch (error) {
|
|
console.error("Get team hunt session error:", error);
|
|
return Response.json(
|
|
{ error: "Failed to get hunt session: " + error.message },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|