fixed a bit

This commit is contained in:
bobbert committed 2025-03-12 01:46:45 +00:00
1 parent c62413eafa
commit 4ed0480d08
10 files changed
+138 -149

No files matched your search

+2 -14
View File
@@ -1,4 +1,4 @@
import { queryOne, run } from "../../../../lib/db";
import { queryOne, run } from "@/lib/db";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../../auth/[...nextauth]/route";
import { nanoid } from "nanoid";
@@ -7,9 +7,6 @@ export async function POST(request) {
try {
const session = await getServerSession(authOptions);
// Debugging: Log the session
console.log("Session:", session);
if (!session) {
return new Response(
JSON.stringify({ success: false, error: "Unauthorised" }),
@@ -17,7 +14,6 @@ export async function POST(request) {
);
}
// Ensure the user is not an admin
if (session.user.role === "admin") {
return new Response(
JSON.stringify({ success: false, error: "Admins cannot create teams" }),
@@ -34,27 +30,19 @@ export async function POST(request) {
);
}
// Generate a unique team code (6 characters)
const teamCode = nanoid(6).toUpperCase();
// Insert the new team
const result = await run("INSERT INTO teams (name, code) VALUES (?, ?)", [
name,
teamCode,
]);
await run("INSERT INTO teams (name, code) VALUES (?, ?)", [name, teamCode]);
// Fetch the newly created team ID
const newTeam = await queryOne("SELECT id FROM teams WHERE code = ?", [
teamCode,
]);
// Update the user's team_id
await run("UPDATE users SET team_id = ? WHERE id = ?", [
newTeam.id,
userId,
]);
// Update the session with the new teamId
session.user.teamId = newTeam.id;
return new Response(JSON.stringify({ success: true, teamId: newTeam.id }), {