Files
peworkshop/app/api/teams/[teamId]/hunt-session/route.js
T

75 lines
2.0 KiB
JavaScript

import { getToken } from "next-auth/jwt";
import { queryOne } 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 });
}
const { teamId } = 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 }
);
}
// 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 }
);
}
}