-
Notifications
You must be signed in to change notification settings - Fork 4
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
Pratyush
committed
Mar 15, 2024
1 parent
dc566ee
commit 17ed443
Showing
11 changed files
with
327 additions
and
249 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,52 @@ | ||
import { connectMongoDB } from '@/lib/mongodb'; | ||
import { Users } from '@/models/user.model'; | ||
import { NextResponse } from 'next/server'; | ||
|
||
import { Event1 } from '@/models/event1.model'; | ||
import { getToken } from 'next-auth/jwt'; | ||
import { getTokenDetails } from '../../../../utils/authuser'; | ||
|
||
export async function POST(req) { | ||
try { | ||
await connectMongoDB(); | ||
|
||
const token = await getToken({ req }); | ||
const auth = token ? token.accessTokenFromBackend : null; | ||
let userId = await getTokenDetails(auth); | ||
|
||
const user = await Users.findById({ _id: userId }); | ||
|
||
console.log(user); | ||
|
||
const { teamName } = await req.json(); | ||
const team = await Event1.findOne({ teamName: teamName }); | ||
if (team) { | ||
return NextResponse.json({ | ||
message: 'Team Already registered ', | ||
status: 200, | ||
}); | ||
} | ||
|
||
const newTeam = await new Event1({ | ||
teamName: teamName, | ||
teamLeaderId: userId, | ||
members: [userId], | ||
}).save(); | ||
|
||
await Users.findByIdAndUpdate( | ||
{ _id: userId }, | ||
{ $set: { event1TeamId: newTeam._id, event1TeamRole: 0 } } | ||
); | ||
|
||
return NextResponse.json({ | ||
message: 'Team Details entered ', | ||
status: 200, | ||
}); | ||
} catch (error) { | ||
console.error('An error occurred:', error); | ||
return NextResponse.json({ | ||
message: 'Error occurred ', | ||
status: 500, | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,64 +1,78 @@ | ||
import { connectMongoDB } from "@/lib/mongodb"; | ||
import { TeamToken } from "@/models/event1TeamToken"; | ||
import { Event2 } from "@/models/event2.model"; | ||
import { Event1 } from "@/models/event1.model"; | ||
|
||
import { event1TeamToken } from "@/models/event1TeamToken"; | ||
import { Users } from "@/models/user.model"; | ||
import { getTokenDetails } from "@/utils/authuser"; | ||
import { getToken } from "next-auth/jwt"; | ||
import { NextResponse } from "next/server"; | ||
|
||
export async function POST(req, { params }) { | ||
try { | ||
await connectMongoDB(); | ||
|
||
const token = await getToken({req}) | ||
const auth = token ? token.accessTokenFromBackend : null | ||
let userId = await getTokenDetails(auth); | ||
|
||
const teamId = params.teamId; | ||
const team = await Event2.findById({ _id: teamId }); | ||
console.log(team); | ||
|
||
if (!team) { | ||
return res.status(401).json({ | ||
message: "Invalid TeamId", | ||
}); | ||
} | ||
|
||
if (team.teamLeaderId.toString() !== userId) { | ||
return NextResponse.json({ | ||
message: "User do not belong to the team or the user is not a leader", | ||
status: 200, | ||
}); | ||
} | ||
|
||
if (team.members.length !== 1) { | ||
return NextResponse.json({ | ||
message: "Team Size more than 1", | ||
status: 200, | ||
}); | ||
} | ||
|
||
await Event2.findOneAndDelete({ | ||
_id: teamId, | ||
}); | ||
|
||
await TeamToken.findOneAndDelete({ | ||
teamId: teamId, | ||
}); | ||
|
||
await Users.findByIdAndUpdate( | ||
{ _id: userId }, | ||
{ teamId: null, teamRole: null } | ||
); | ||
|
||
/* | ||
const deletedTeam = await Event2.deleteOne({teamName:teamName}); | ||
export async function POST(req,{params}){ | ||
|
||
try{ | ||
|
||
await connectMongoDB(); | ||
const headers = req.headers; | ||
|
||
const token = await getToken({req}) | ||
const auth = token ? token.accessTokenFromBackend : null | ||
let userId = await getTokenDetails(auth); | ||
|
||
|
||
const teamId=params.teamId; | ||
const team = await Event1.findById({ _id: teamId }); | ||
console.log(team); | ||
|
||
if (!team) { | ||
return res.status(401).json({ | ||
message: "Invalid TeamId" | ||
}) | ||
|
||
} | ||
|
||
|
||
if (team.teamLeaderId.toString() !== userId) { | ||
return NextResponse.json({ message: "User do not belong to the team or the user is not a leader", status: 200 }); | ||
} | ||
|
||
if (team.members.length !== 1) { | ||
return NextResponse.json({ message: "Team Size more than 1", status: 200 }); | ||
} | ||
|
||
await Event1.findOneAndDelete({ | ||
_id: teamId, | ||
}); | ||
|
||
await event1TeamToken.findOneAndDelete({ | ||
teamId: teamId, | ||
}); | ||
|
||
await Users.findByIdAndUpdate( | ||
{ _id: userId }, | ||
{ event1TeamId: null, event1TeamRole: null } | ||
); | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* | ||
const deletedTeam = await TeamModel.deleteOne({teamName:teamName}); | ||
console.log(deletedTeam) | ||
*/ | ||
|
||
return NextResponse.json({ message: "Team deleted", status: 200 }); | ||
} catch (error) { | ||
|
||
|
||
|
||
|
||
}catch(error) { | ||
console.error("An error occurred:", error); | ||
return NextResponse.json({ message: "Error occurred ", status: 500 }); | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,46 +1,43 @@ | ||
import { connectMongoDB } from "@/lib/mongodb"; | ||
import { Event2 } from "@/models/event2.model"; | ||
import { Users } from "@/models/user.model"; | ||
import { NextResponse } from "next/server"; | ||
import { connectMongoDB } from '@/lib/mongodb'; | ||
import { Users } from '@/models/user.model'; | ||
import { NextResponse } from 'next/server'; | ||
|
||
import { getToken } from "next-auth/jwt"; | ||
import { getTokenDetails } from "../../../../utils/authuser"; | ||
import { Event1 } from '@/models/event1.model'; | ||
import { getToken } from 'next-auth/jwt'; | ||
import { getTokenDetails } from '../../../../utils/authuser'; | ||
|
||
|
||
export async function POST(req){ | ||
try{ | ||
export async function GET(req) { | ||
try { | ||
await connectMongoDB(); | ||
const headers = req.headers; | ||
|
||
|
||
|
||
|
||
const token = await getToken({req}) | ||
const auth = token ? token.accessTokenFromBackend : null | ||
const token = await getToken({ req }); | ||
const auth = token ? token.accessTokenFromBackend : null; | ||
let userId = await getTokenDetails(auth); | ||
console.log(userId) | ||
|
||
const user = await Users.findById({ _id: userId}); | ||
if(!user){ | ||
return NextResponse.json({ message: "User Not found"}); | ||
|
||
const user = await Users.findById({ _id: userId }); | ||
|
||
if (!user) { | ||
return NextResponse.json({ message: 'User Not found' }); | ||
} | ||
|
||
console.log(user); | ||
|
||
const teamId = user.event1TeamId; | ||
const team = await Event2.findById(teamId); | ||
const team = await Event1.findById(teamId).populate('members'); | ||
console.log("^^^^^^",team); | ||
if (!team) { | ||
return NextResponse.json({ message: "Team is not there " }); | ||
return NextResponse.json({ message: 'Team is not there' }, {status:201}); | ||
} | ||
|
||
|
||
return NextResponse.json({ message: "Team Details sent. ", status: 200, teamDetails: team }); | ||
|
||
|
||
|
||
|
||
}catch(error) { | ||
console.error("An error occurred:", error); | ||
return NextResponse.json({ message: "Error occurred ", status: 500 }); | ||
|
||
return NextResponse.json({ | ||
message: 'Team Details sent. ', | ||
status: 200, | ||
teamDetails: team, | ||
}); | ||
} catch (error) { | ||
console.error('An error occurred:', error); | ||
return NextResponse.json({ | ||
message: 'Error occurred ', | ||
status: 500, | ||
}); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.