import { queryOne } from "../../../../lib/db"; import { getServerSession } from "next-auth/next"; import { authOptions } from "../../auth/[...nextauth]/route"; export async function POST(request) { const session = await getServerSession(authOptions); if (!session) { return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401, }); } try { const { teamCode } = await request.json(); if (!teamCode) { return new Response(JSON.stringify({ error: "Team code is required" }), { status: 400, }); } const team = await queryOne("SELECT id FROM teams WHERE code = ?", [ teamCode, ]); if (!team) { return new Response(JSON.stringify({ error: "Invalid team code" }), { status: 404, }); } await queryOne("UPDATE users SET team_id = ? WHERE id = ?", [ team.id, session.user.id, ]); return new Response(JSON.stringify({ teamId: team.id }), { status: 200, }); } catch (error) { console.error("Join team error:", error); return new Response(JSON.stringify({ error: "Failed to join team" }), { status: 500, }); } }