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 { 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 }); } // Check if user has a team if (!token.teamId) { return Response.json( { error: "You must be part of a team to view clues" }, { status: 403 } ); } // Get all clues with explicit ordering to ensure consistency const clues = await query(` SELECT id, title, description, location, x_coord, y_coord FROM clues ORDER BY id ASC `); // For each clue, check if the team has found it const cluesWithStatus = await Promise.all( clues.map(async (clue) => { const found = await queryOne( "SELECT 1 FROM team_clues WHERE team_id = ? AND clue_id = ?", [token.teamId, clue.id] ); return { ...clue, found: !!found, }; }) ); console.log(`Returning ${cluesWithStatus.length} clues`); return Response.json(cluesWithStatus, { headers: { "Cache-Control": "no-store, no-cache, must-revalidate", Pragma: "no-cache", Expires: "0", }, }); } catch (error) { console.error("Error fetching clues:", error); return Response.json( { error: "Failed to fetch clues: " + error.message }, { status: 500 } ); } }