mirror of
https://github.com/mudabbir-ahmad/peworkshop.git
synced 2026-10-07 19:50:20 +00:00
fixed a bit
This commit is contained in:
1 parent
45b0f98971
commit
a0fb2b9970
9 files changed
+314
-177
No files matched your search
@@ -0,0 +1,46 @@
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user