Files
peworkshop/app/api/clues/[clueId]/route.js
T
bobbert 0afa6817a8 cannot fix issue with elapsed timer on admin dashboard. fixing issues with QR code editing.
Grey = cant click
Red = current
Green = complete
blue = skipped?
2025-03-15 20:01:51 +00:00

123 lines
3.7 KiB
JavaScript

import { queryOne } from "@/lib/db";
import { getToken } from "next-auth/jwt";
export const dynamic = "force-dynamic"; // Disable caching
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 });
}
// 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 }
);
}
const { clueId } = context.params;
// Validate clueId is a number
if (isNaN(parseInt(clueId))) {
return Response.json({ error: "Invalid clue ID" }, { status: 400 });
}
// Get the specific clue - removed coordinates
const clue = await queryOne(
"SELECT id, title, description, location FROM clues WHERE id = ?",
[clueId]
);
if (!clue) {
return Response.json({ error: "Clue not found" }, { status: 404 });
}
// Get timestamp parameter to determine if this is a refresh call
const timestamp = request.nextUrl?.searchParams?.get("t");
const isRefresh = timestamp && !isNaN(parseInt(timestamp));
// Check if this team has found this clue before
const found = await queryOne(
"SELECT * FROM team_clues WHERE team_id = ? AND clue_id = ?",
[token.teamId, clueId]
);
// Only mark as "foundNow" if:
// 1. The clue was not found before
// 2. This is not a refresh request (or we can't determine)
// 3. The user is not an admin (admins don't need "foundNow" animations)
let foundNow = false;
if (!found && (!isRefresh || token.role !== "admin")) {
try {
// First ensure the team_clues table exists
await queryOne(`
CREATE TABLE IF NOT EXISTS team_clues (
team_id INTEGER NOT NULL,
clue_id INTEGER NOT NULL,
found_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (team_id, clue_id),
FOREIGN KEY (team_id) REFERENCES teams(id),
FOREIGN KEY (clue_id) REFERENCES clues(id)
)
`);
// Record that the team found this clue
await queryOne(
"INSERT INTO team_clues (team_id, clue_id) VALUES (?, ?)",
[token.teamId, clueId]
);
// Set foundNow flag for non-admin users only
foundNow = token.role !== "admin";
console.log(
`Clue ${clueId} marked as found for team ${token.teamId}, foundNow=${foundNow}`
);
} catch (err) {
console.error("Error marking clue as found:", err);
// Continue even if there's an error recording the find
}
}
// Add different cache headers for admins vs regular users
const cacheHeaders =
token.role === "admin"
? {
"Cache-Control": "private, max-age=10", // Allow 10 seconds of caching for admins
Pragma: "no-cache",
Expires: "0",
}
: {
"Cache-Control": "no-store, no-cache, must-revalidate",
Pragma: "no-cache",
Expires: "0",
};
return Response.json(
{
...clue,
found: true, // Always return as found since the user is viewing it
foundNow, // Only true during initial discovery for non-admins
isAdmin: token.role === "admin", // Add flag so client knows user is admin
},
{ headers: cacheHeaders }
);
} catch (error) {
console.error("Error fetching clue:", error);
return Response.json(
{ error: "Failed to fetch clue: " + error.message },
{ status: 500 }
);
}
}