-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { getServerSession } from "next-auth"; | ||
import { authOptions } from "../../../../lib/authOptions"; | ||
import { TeamModel } from "../../../../model/Team"; | ||
import dbConnect from "../../../../lib/dbConnect"; | ||
|
||
export async function POST(request) { | ||
const session = await getServerSession(authOptions); | ||
|
||
if (!session || !session.user) { | ||
return new Response( | ||
JSON.stringify({ success: false, message: "Unauthorized" }), | ||
{ status: 401 } | ||
); | ||
} | ||
|
||
const userId = session.user._id; // Extract user ID from the session | ||
const { teamId, playerId } = await request.json(); | ||
|
||
if (!teamId || !playerId) { | ||
return new Response( | ||
JSON.stringify({ success: false, message: "Team ID and Player ID are required" }), | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
await dbConnect(); | ||
|
||
try { | ||
const team = await TeamModel.findById(teamId); | ||
|
||
if (!team) { | ||
return new Response( | ||
JSON.stringify({ success: false, message: "Team not found" }), | ||
{ status: 404 } | ||
); | ||
} | ||
|
||
// Check if the authenticated user is a member of the team | ||
const isPlayerInTeam = team.players.some((player) => player.toString() === userId); | ||
if (!isPlayerInTeam) { | ||
return new Response( | ||
JSON.stringify({ success: false, message: "Forbidden: You are not a member of this team" }), | ||
{ status: 403 } | ||
); | ||
} | ||
|
||
// Add the new player to the team | ||
if (!team.players.includes(playerId)) { | ||
team.players.push(playerId); | ||
await team.save(); | ||
} | ||
|
||
return new Response( | ||
JSON.stringify({ success: true, message: "Player added successfully" }), | ||
{ status: 200 } | ||
); | ||
} catch (error) { | ||
console.error("Error adding player:", error); | ||
return new Response( | ||
JSON.stringify({ success: false, message: "Server error" }), | ||
{ status: 500 } | ||
); | ||
} | ||
} |