mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
95 lines
2.6 KiB
JavaScript
95 lines
2.6 KiB
JavaScript
import { getServerSession } from "next-auth/next";
|
|
import { authOptions } from "../../auth/[...nextauth]/route";
|
|
import { query, queryOne } from "../../../../lib/db";
|
|
|
|
export async function GET(request) {
|
|
try {
|
|
// Get the user's session with better error handling
|
|
let session;
|
|
try {
|
|
session = await getServerSession(authOptions);
|
|
} catch (sessionError) {
|
|
console.error("Session fetch error:", sessionError);
|
|
return Response.json(
|
|
{ error: "Authentication error", details: sessionError.message },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Check if user is authenticated
|
|
if (!session || !session.user) {
|
|
return Response.json({ error: "You must be logged in" }, { status: 401 });
|
|
}
|
|
|
|
// Check if user is part of a team
|
|
if (!session.user.teamId) {
|
|
return Response.json(
|
|
{ error: "You are not part of a team" },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// Fetch team data
|
|
const team = await queryOne("SELECT * FROM teams WHERE id = ?", [
|
|
session.user.teamId,
|
|
]);
|
|
|
|
if (!team) {
|
|
return Response.json({ error: "Team not found" }, { status: 404 });
|
|
}
|
|
|
|
// Fetch team members
|
|
const members = await query(
|
|
"SELECT id, username FROM users WHERE team_id = ? ORDER BY username ASC",
|
|
[session.user.teamId]
|
|
);
|
|
|
|
// Fetch hunt status to provide start time - safely check multiple places
|
|
let huntStartTime = null;
|
|
|
|
try {
|
|
// Try hunt_status table first
|
|
const huntStatus = await queryOne("SELECT * FROM hunt_status LIMIT 1");
|
|
if (huntStatus) {
|
|
huntStartTime = huntStatus.start_time;
|
|
} else {
|
|
// Fallback to hunts table if it exists
|
|
const oldHuntRecord = await queryOne(
|
|
"SELECT start_time FROM hunts ORDER BY id DESC LIMIT 1"
|
|
);
|
|
if (oldHuntRecord) {
|
|
huntStartTime = oldHuntRecord.start_time;
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.log("Error fetching hunt status (non-critical):", err.message);
|
|
// Continue even if hunt status check fails
|
|
}
|
|
|
|
// Add cache-busting headers
|
|
return Response.json(
|
|
{
|
|
success: true,
|
|
team,
|
|
members,
|
|
huntStartTime: huntStartTime,
|
|
refreshed: new Date().toISOString(),
|
|
},
|
|
{
|
|
status: 200,
|
|
headers: {
|
|
"Cache-Control": "no-store, must-revalidate, max-age=0",
|
|
Pragma: "no-cache",
|
|
Expires: "0",
|
|
},
|
|
}
|
|
);
|
|
} catch (error) {
|
|
console.error("Team fetch error:", error);
|
|
return Response.json(
|
|
{ error: "Failed to fetch team data" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|