mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
Grey = cant click Red = current Green = complete blue = skipped? Need to add Clue page Need to fix Leaderboard.
81 lines
2.1 KiB
JavaScript
81 lines
2.1 KiB
JavaScript
import { getToken } from "next-auth/jwt";
|
|
import { queryOne, run } from "@/lib/db";
|
|
|
|
export const dynamic = "force-dynamic"; // Disable caching
|
|
|
|
export async function POST(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 mark clues as found" },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
const { clueId } = await request.json();
|
|
|
|
// Validate clueId is a number
|
|
if (!clueId || isNaN(parseInt(clueId))) {
|
|
return Response.json({ error: "Invalid clue ID" }, { status: 400 });
|
|
}
|
|
|
|
// Ensure the team_clues table exists
|
|
await run(`
|
|
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)
|
|
)
|
|
`);
|
|
|
|
// Check if this clue is already found
|
|
const found = await queryOne(
|
|
"SELECT 1 FROM team_clues WHERE team_id = ? AND clue_id = ?",
|
|
[token.teamId, clueId]
|
|
);
|
|
|
|
// If already found, return success with a message
|
|
if (found) {
|
|
return Response.json({
|
|
success: true,
|
|
message: "This clue was already found",
|
|
alreadyFound: true,
|
|
});
|
|
}
|
|
|
|
// Record that the team found this clue
|
|
await run("INSERT INTO team_clues (team_id, clue_id) VALUES (?, ?)", [
|
|
token.teamId,
|
|
clueId,
|
|
]);
|
|
|
|
return Response.json({
|
|
success: true,
|
|
message: "Clue marked as found successfully",
|
|
clueId: clueId,
|
|
teamId: token.teamId,
|
|
});
|
|
} catch (error) {
|
|
console.error("Error marking clue as found:", error);
|
|
return Response.json(
|
|
{ error: "Failed to mark clue as found: " + error.message },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|