fixed skip clue implimentation

This commit is contained in:
bobbert committed 2025-03-18 22:25:07 +00:00
1 parent fccc2a4edc
commit 2f9a6c732c
18 files changed
+524 -151

No files matched your search

+22 -22
View File
@@ -1,10 +1,9 @@
import { getToken } from "next-auth/jwt";
import { query, queryOne, run } from "@/lib/db";
import { nanoid } from "nanoid";
import { queryOne, run } from "@/lib/db";
export async function POST(request) {
try {
// Get user token from session
// Verify user authentication
const token = await getToken({
req: request,
secret:
@@ -13,30 +12,23 @@ export async function POST(request) {
});
if (!token) {
return Response.json({ error: "Invalid session" }, { status: 401 });
return Response.json({ error: "Unauthorized" }, { status: 401 });
}
// Extract user ID from the token
// Get user ID from token
const userId = token.id;
if (!userId) {
return Response.json(
{ error: "Invalid user information in session" },
{ status: 401 }
);
}
// Parse request body
const body = await request.json();
const { name } = body;
// Get team name from request body
const { name } = await request.json();
if (!name || name.trim() === "") {
if (!name || !name.trim()) {
return Response.json({ error: "Team name is required" }, { status: 400 });
}
// Generate a unique team code
const code = nanoid(6).toUpperCase();
const code = generateTeamCode();
// Create the team - updated schema to match actual database (no created_by column)
// Create teams table if it doesn't exist
await run(`
CREATE TABLE IF NOT EXISTS teams (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -50,6 +42,7 @@ export async function POST(request) {
const existingTeam = await queryOne("SELECT id FROM teams WHERE name = ?", [
name,
]);
if (existingTeam) {
return Response.json(
{ error: "Team name already exists" },
@@ -57,7 +50,7 @@ export async function POST(request) {
);
}
// Insert team without created_by field
// Insert team
const result = await run("INSERT INTO teams (name, code) VALUES (?, ?)", [
name,
code,
@@ -71,19 +64,26 @@ export async function POST(request) {
// Update the user's team_id
await run("UPDATE users SET team_id = ? WHERE id = ?", [teamId, userId]);
// Return a more complete response with all needed data
// Return success with team info
return Response.json({
success: true,
teamId,
teamName: name,
code,
message: "Team created successfully",
});
} catch (error) {
console.error("Create team error:", error);
console.error("Error creating team:", error);
return Response.json(
{ error: "Failed to create team: " + error.message },
{ status: 500 }
);
}
}
function generateTeamCode() {
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
let result = "";
for (let i = 0; i < 6; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}