mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
fixed elapsed timer on clue page cannot fix it on admin page. I am done. now to move on to the styling to match UI
This commit is contained in:
1 parent
4cd449c0cf
commit
8fd2cabbe0
10 files changed
+226
-136
No files matched your search
+2
-13
@@ -1,7 +1,7 @@
|
||||
import { query, queryOne, run } from "@/lib/db";
|
||||
import { getToken } from "next-auth/jwt";
|
||||
|
||||
export const dynamic = "force-dynamic"; // Disable caching
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
export async function GET(request) {
|
||||
try {
|
||||
@@ -17,7 +17,6 @@ export async function GET(request) {
|
||||
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Check if user has a team
|
||||
if (!token.teamId) {
|
||||
console.log("API: User without team tried to access clues");
|
||||
return Response.json(
|
||||
@@ -26,9 +25,7 @@ export async function GET(request) {
|
||||
);
|
||||
}
|
||||
|
||||
// First, ensure the team_clues table has the 'skipped' column
|
||||
try {
|
||||
// Check if team_clues table exists, create it if it doesn't
|
||||
await run(`
|
||||
CREATE TABLE IF NOT EXISTS team_clues (
|
||||
team_id INTEGER NOT NULL,
|
||||
@@ -40,7 +37,6 @@ export async function GET(request) {
|
||||
)
|
||||
`);
|
||||
|
||||
// Check if skipped column exists
|
||||
const tableInfo = await query("PRAGMA table_info(team_clues)");
|
||||
const hasSkippedColumn = tableInfo.some((col) => col.name === "skipped");
|
||||
|
||||
@@ -52,18 +48,14 @@ export async function GET(request) {
|
||||
}
|
||||
} catch (schemaError) {
|
||||
console.error("Error ensuring database schema:", schemaError);
|
||||
// Continue execution - if this fails, the main query might still work
|
||||
// with existing columns
|
||||
}
|
||||
|
||||
// Get all clues with explicit ordering
|
||||
const clues = await query(`
|
||||
SELECT id, title, description, location
|
||||
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) => {
|
||||
let clueStatus;
|
||||
@@ -74,7 +66,6 @@ export async function GET(request) {
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(`Error querying status for clue ${clue.id}:`, error);
|
||||
// Fallback if query fails (e.g., skipped column still doesn't exist)
|
||||
clueStatus = await queryOne(
|
||||
"SELECT found_at FROM team_clues WHERE team_id = ? AND clue_id = ?",
|
||||
[token.teamId, clue.id]
|
||||
@@ -85,19 +76,17 @@ export async function GET(request) {
|
||||
...clue,
|
||||
found: !!clueStatus && !(clueStatus.skipped === 1),
|
||||
skipped: !!clueStatus && clueStatus.skipped === 1,
|
||||
available: false, // Will be determined after processing all clues
|
||||
available: false,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
// Determine which clues should be available
|
||||
let firstUnavailableFound = false;
|
||||
const finalClues = cluesWithStatus.map((clue, index, arr) => {
|
||||
if (clue.found || clue.skipped) {
|
||||
return { ...clue, available: true };
|
||||
}
|
||||
|
||||
// If we haven't found an unavailable clue yet, this one is available
|
||||
if (!firstUnavailableFound) {
|
||||
firstUnavailableFound = true;
|
||||
return { ...clue, available: true };
|
||||
|
||||
@@ -3,7 +3,6 @@ import { queryOne, run } from "@/lib/db";
|
||||
|
||||
export async function POST(request) {
|
||||
try {
|
||||
// Verify user authentication
|
||||
const token = await getToken({
|
||||
req: request,
|
||||
secret:
|
||||
@@ -15,20 +14,16 @@ export async function POST(request) {
|
||||
return Response.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Get user ID from token
|
||||
const userId = token.id;
|
||||
|
||||
// Get team name from request body
|
||||
const { name } = await request.json();
|
||||
|
||||
if (!name || !name.trim()) {
|
||||
return Response.json({ error: "Team name is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Generate a unique team code
|
||||
const code = generateTeamCode();
|
||||
|
||||
// Create teams table if it doesn't exist
|
||||
await run(`
|
||||
CREATE TABLE IF NOT EXISTS teams (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -38,7 +33,6 @@ export async function POST(request) {
|
||||
)
|
||||
`);
|
||||
|
||||
// Check if team name already exists
|
||||
const existingTeam = await queryOne("SELECT id FROM teams WHERE name = ?", [
|
||||
name,
|
||||
]);
|
||||
@@ -50,7 +44,6 @@ export async function POST(request) {
|
||||
);
|
||||
}
|
||||
|
||||
// Insert team
|
||||
const result = await run("INSERT INTO teams (name, code) VALUES (?, ?)", [
|
||||
name,
|
||||
code,
|
||||
@@ -61,10 +54,8 @@ export async function POST(request) {
|
||||
throw new Error("Failed to create team - no ID returned");
|
||||
}
|
||||
|
||||
// Update the user's team_id
|
||||
await run("UPDATE users SET team_id = ? WHERE id = ?", [teamId, userId]);
|
||||
|
||||
// Return success with team info
|
||||
return Response.json({
|
||||
success: true,
|
||||
teamId,
|
||||
|
||||
Reference in new issue
Block a user