diff --git a/app/api/event2/createTeam/route.js b/app/api/event2/createTeam/route.js new file mode 100644 index 0000000..13f64f0 --- /dev/null +++ b/app/api/event2/createTeam/route.js @@ -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, + }); + } +} diff --git a/app/api/event2/deleteTeam/[teamId]/route.js b/app/api/event2/deleteTeam/[teamId]/route.js index 81176d4..b8b570c 100644 --- a/app/api/event2/deleteTeam/[teamId]/route.js +++ b/app/api/event2/deleteTeam/[teamId]/route.js @@ -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 }); - } } +} \ No newline at end of file diff --git a/app/api/event2/getTeamCode/route.js b/app/api/event2/getTeamCode/route.js index ab18d79..74c666a 100644 --- a/app/api/event2/getTeamCode/route.js +++ b/app/api/event2/getTeamCode/route.js @@ -1,21 +1,21 @@ import { connectMongoDB } from "@/lib/mongodb"; -import { TeamToken } from "@/models/event1TeamToken"; -import { Event2 } from "@/models/event2.model"; +import { NextResponse } from "next/server"; import { getTokenDetails } from "@/utils/authuser"; import { customAlphabet } from "nanoid"; import { getToken } from "next-auth/jwt"; -import { NextResponse } from "next/server"; +import { Event1 } from "@/models/event1.model"; +import { event1TeamToken } from "@/models/event1TeamToken"; -export async function POST(req, { params }) { +export async function GET(req, { params }) { try { await connectMongoDB(); const token = await getToken({req}) const auth = token ? token.accessTokenFromBackend : null let userId = await getTokenDetails(auth); - + console.log("rrrrrrfrrrrrr",userId); // console.log(userId); - const team = await Event2.findOne({ teamLeaderId: userId }); + const team = await Event1.findOne({ teamLeaderId: userId }); if (!team) { return NextResponse.json({ message: "Team Not found" }); } @@ -26,20 +26,20 @@ export async function POST(req, { params }) { 10 )(); // const teamCode = nanoid(10) - const newToken = await new TeamToken({ + const newToken = await new event1TeamToken({ teamId: team._id, token: teamCode, createdAt: new Date(), }).save(); - await TeamModel.findOneAndUpdate( + await Event1.findOneAndUpdate( { _id: team._id }, { $set: { teamCode: teamCode } } ); return NextResponse.json({ teamCode: teamCode, teamName: team.teamName }); } else { - const token = await TeamToken.findOne({ teamId: team._id }); + const token = await event1TeamToken.findOne({ teamId: team._id }); if (!token) { return NextResponse.json({ message: "Token not found" }); @@ -54,11 +54,11 @@ export async function POST(req, { params }) { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 10 )(); - await TeamToken.findOneAndUpdate( + await event1TeamToken.findOneAndUpdate( { teamId: team._id }, { $set: { token: newTeamCode, createdAt: currentTime } } ); - await Event2.findOneAndUpdate( + await Event1.findOneAndUpdate( { _id: team._id }, { $set: { teamCode: newTeamCode } } ); diff --git a/app/api/event2/getTeamData/route.js b/app/api/event2/getTeamData/route.js index 6396138..2b7c421 100644 --- a/app/api/event2/getTeamData/route.js +++ b/app/api/event2/getTeamData/route.js @@ -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, + }); + } } -} \ No newline at end of file diff --git a/app/api/event2/getTeamViaToken/route.js b/app/api/event2/getTeamViaToken/route.js index 4903c1c..32be7bb 100644 --- a/app/api/event2/getTeamViaToken/route.js +++ b/app/api/event2/getTeamViaToken/route.js @@ -1,22 +1,25 @@ import { connectMongoDB } from "@/lib/mongodb"; -import { Event2 } from "@/models/event2.model"; +import { Event1 } from "@/models/event1.model"; +import { event1TeamToken } from "@/models/event1TeamToken"; import { NextResponse } from "next/server"; export async function POST(req) { try { await connectMongoDB(); - const { code } = await req.json(); - console.log(code); - const team = await Event2.findOne({ teamCode: code }); + const { teamCode } = await req.json(); + console.log(teamCode); + const team = await event1TeamToken.findOne({ token: teamCode }); + if (!team) { - return NextResponse.json({ error: "Team not found" }); + return NextResponse.json({ error: "Token not found" }); } + const teamDetails = await Event1.findById(team.teamId); return NextResponse.json({ message: "Team Details sent. ", status: 200, - teamDetails: team, + teamDetails: teamDetails, }); } catch (error) { console.error("An error occurred:", error); diff --git a/app/api/event2/joinTeam/route.js b/app/api/event2/joinTeam/route.js new file mode 100644 index 0000000..515aba9 --- /dev/null +++ b/app/api/event2/joinTeam/route.js @@ -0,0 +1,90 @@ +import { connectMongoDB } from '@/lib/mongodb'; +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 }); + console.log("@@@@@@@@@@@",token) + const auth = token ? token.accessTokenFromBackend : null; + let userId = await getTokenDetails(auth); + + + const user = await Users.findById({ _id: userId }); + console.log(user); + + if (user.event1TeamId) { + return NextResponse.json({ + message: 'User is already a part of team', + }); + } + + console.log("$$$$$$$$$$$$$$$$$$") + + const { teamCode } = await req.json(); + console.log('==========',teamCode); + const team = await Event1.findOne({ teamCode: teamCode }); + //check if user is not a part of any team + if (!team) { + return NextResponse.json({ error: 'Team not found' }); + } + if (team.members.length === 4) { + return NextResponse.json({ error: 'Team is Full' }); + } + // console.log(team) + const Event1TeamToken = await event1TeamToken.findOne({ teamId: team._id }); + console.log("&&&&&&&",Event1TeamToken) + if (!Event1TeamToken) { + return res.status(404).json({ error: 'Token not found' }); + } + + console.log("*******") + // const currentTime = new Date(); + // const tokenCreationTime = token.createdAt; + + // const timeDifference = + // (currentTime - tokenCreationTime) / (1000 * 60); // Difference in minutes + //have to change this + + // if (timeDifference > 1000000000) { + // // Token expired, prompt for a new token + // return NextResponse.json({ + // error: 'Token expired. Ask leader to generate a new token.', + // }); + // } + if (teamCode !== Event1TeamToken.token) { + return NextResponse.json({ error: 'Incorrect token' }); + } + + console.log("workss") + await Users.findOneAndUpdate( + { _id: userId }, + { $set: { event1TeamId: team.id, event1TeamRole: 1 } } + ); + console.log("---------",team._id) + + await Event1.findOneAndUpdate( + { + _id: team._id, + }, + { + $push: { members: userId }, + } + ); + return NextResponse.json({ + message: 'You have joined the team!', + }); + } catch (error) { + console.error('An error occurred:', error); + return NextResponse.json({ + message: 'Error occurred ', + status: 500, + }); + } +} diff --git a/app/api/event2/leaveTeam/route.js b/app/api/event2/leaveTeam/route.js index 732df0d..296b787 100644 --- a/app/api/event2/leaveTeam/route.js +++ b/app/api/event2/leaveTeam/route.js @@ -1,57 +1,62 @@ -import { connectMongoDB } from "@/lib/mongodb"; -import { UsersDetails } from "@/models/Userdetails"; -import { Event2 } from "@/models/event2.model"; -import { NextResponse } from "next/server"; - -import { getToken } from "next-auth/jwt"; -import { getTokenDetails } from "../../../../utils/authuser"; - - -export async function POST(req){ - try{ +import { connectMongoDB } from '@/lib/mongodb'; +import { Event1 } from '@/models/event1.model'; +import { Users } from '@/models/user.model'; +import { NextResponse } from 'next/server'; + +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 + const token = await getToken({ req }); + const auth = token ? token.accessTokenFromBackend : null; let userId = await getTokenDetails(auth); - console.log(userId); - const user = await UsersDetails.findById(userId); + const user = await Users.findById(userId); - if (user.event1TeamRole != "1") { + console.log("*********", user); + + if (user.event1TeamRole !== 1) { return NextResponse.json({ - message: "Leader cant leave the team", + message: 'Leader cant leave the team', }); } if (!user.event1TeamId) { return NextResponse.json({ - message: "User is not part of any team", + message: 'User is not part of any team', }); } - const team = await Event2.findById(user.event1TeamId); + const team = await Event1.findById(user.event1TeamId); if (!team) { return NextResponse.json({ - message: "Team not found", + message: 'Team not found', }); } - team.members.pull(userId); - await team.save(); - - await UsersDetails.findByIdAndUpdate(userId, { $set: { teamId: null, teamRole:-1 } }); - - - - - return NextResponse.json({ message: "User has left the team successfully ", status: 200, teamDetails: team }); - + team.members.pull(userId); + await team.save(); -}catch(error) { - console.error("An error occurred:", error); - return NextResponse.json({ message: "Error occurred ", status: 500 }); + await Users.findByIdAndUpdate(userId, { + $set: { event1TeamId: null, event1TeamRole: -1 }, + }); + + return NextResponse.json({ + message: 'User has left the team successfully ', + status: 200, + teamDetails: team, + }); + + } catch (error) { + console.error('An error occurred:', error); + return NextResponse.json({ + message: 'Error occurred ', + status: 500, + }); + } } -} \ No newline at end of file diff --git a/app/api/event2/makeTeam/route.js b/app/api/event2/makeTeam/route.js deleted file mode 100644 index 1df6aaa..0000000 --- a/app/api/event2/makeTeam/route.js +++ /dev/null @@ -1,51 +0,0 @@ -import { connectMongoDB } from "@/lib/mongodb"; -import { UsersDetails } from "@/models/Userdetails"; -import { Event2 } from "@/models/event2.model"; -import { NextResponse } from "next/server"; - -import { getToken } from "next-auth/jwt"; -import { getTokenDetails } from "../../../../utils/authuser"; -import { generateTokens } from "../../login/generateTokensTeam/route"; - -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 UsersDetails.findById({ _id: userId }); - - console.log(user); - - const { teamName } = await req.json(); - const team = await Event2.findOne({ teamName: teamName }); - if (team) { - return NextResponse.json({ - message: "Team Already registered ", - status: 200, - }); - } - // console.log(teamName) - const newTeam = await new Event2({ - teamName: teamName, - teamLeaderId: userId, - members: [userId], - }).save(); - - await UsersDetails.findByIdAndUpdate( - { _id: userId }, - { $set: { teamId: newTeam._id } } - ); - - const { accessToken, refreshToken } = await generateTokens(newTeam); - //console.log(accessToken); - //console.log(refreshToken); - - 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 }); - } -} diff --git a/app/api/event2/removeMember/[teamId]/route.js b/app/api/event2/removeMember/[teamId]/route.js index 4249844..76c88a7 100644 --- a/app/api/event2/removeMember/[teamId]/route.js +++ b/app/api/event2/removeMember/[teamId]/route.js @@ -1,60 +1,70 @@ import { connectMongoDB } from "@/lib/mongodb"; -import { Event2 } from "@/models/event2.model"; -import { Users } from "@/models/user.model"; + + import { NextResponse } from "next/server"; +import { Users } from "@/models/user.model"; -import UserDetails from "@/components/userDetails"; import { getToken } from "next-auth/jwt"; import { getTokenDetails } from "../../../../../utils/authuser"; +import { Event1 } from "@/models/event1.model"; export async function POST(req, { params }) { try { + console.log("me idhr hu"); await connectMongoDB(); - + const token = await getToken({req}) const auth = token ? token.accessTokenFromBackend : null let userId = await getTokenDetails(auth); + const user1=await Users.findById(userId); + console.log("]]]]]]",user1); const teamId = params.teamId; - const team = await Event2.findById({ _id: teamId }); - console.log(team); + const team = await Event1.findById({ _id: teamId }); + console.log("______",team); if (!team) { - return res.status(401).json({ - message: "Invalid TeamId to remove", - }); + console.log("here1"); + return NextResponse.json({ message: "Invalid teamId to remove", status: 200 }); } - const userToRemove = await UserDetails.findById({ _id: userId }); + const userToRemove = await Users.findById({ _id: userId }); if (!userToRemove) { + console.log("here2"); return NextResponse.json({ message: "UserID is invalid", status: 200 }); } if (team.teamLeaderId.toString() !== userId) { + console.log("here3"); return NextResponse.json({ + message: "User doesn't belong to the team or user isn't a leader", status: 200, }); } if ( - userToRemove.teamId == null || - userToRemove.teamId.toString() !== teamId + userToRemove. + event1TeamId == null || + userToRemove. + event1TeamId.toString() !== teamId ) { - return res.status(401).json({ + console.log("here88") + return NextResponse.json({ message: "User to remove and TeamId didnt Match", }); } - + console.log("================") await Users.findOneAndUpdate( - { _id: req.body.userId }, - { teamId: null, teamRole: null } + { _id: userId }, + { event1TeamId: null, event1TeamRole + : -1 } ); //updating team - await Event2.findOneAndUpdate( - { _id: req.params.teamId }, + await Event1.findOneAndUpdate( + { _id: teamId }, { $pull: { members: req.body.userId } } ); diff --git a/app/api/login/route.js b/app/api/login/route.js deleted file mode 100644 index d75c46e..0000000 --- a/app/api/login/route.js +++ /dev/null @@ -1,42 +0,0 @@ -import { connectMongoDB } from "@/lib/mongodb"; -import { Users } from "@/models/user.js"; -import { getTokenDetails } from "@/utils/authuser.js"; -import { getToken } from "next-auth/jwt"; -import { headers } from 'next/headers'; -import { NextResponse } from "next/server"; - - -export async function POST(req){ - try{ - await connectMongoDB(); - const headersList = headers() - // const authorization = headersList.get('authorization') - const token = await getToken({req}) - //console.log('ff', token) - console.log(token.accessTokenFromBackend); - // const auth = req.headers.get("authorization").split(' ')[1]; - - let userId = await getTokenDetails(token.accessTokenFromBackend); - console.log(userId); - const user=await Users.findById(userId); - console.log(user); - - const {regNo,mobno}=await req.json(); - - await Users.findByIdAndUpdate(userId,{$set:{regNo:regNo,mobno:mobno}}) - // console.log(newUserDetail); - // const { accessToken, refreshToken } = await generateTokens(newUserDetail); - // console.log(accessToken); - // console.log(refreshToken); - - //console.log(accessToken); - - return NextResponse.json({ message: "User Details entered ", status: 200 }); - - - }catch(error) { - console.error("An error occurred:", error); - return NextResponse.json({ message: "Error occurred ", status: 500 }); - } - -} \ No newline at end of file diff --git a/components/userDetails.jsx b/components/userDetails.jsx index b81a9fe..ba802b3 100644 --- a/components/userDetails.jsx +++ b/components/userDetails.jsx @@ -22,7 +22,7 @@ export default function UserDetails() { console.log('button ') try { - const response = await fetch('http://localhost:3001/api/login', { + const response = await fetch('/api/userDetails', { method: 'POST', headers: { 'Content-Type': 'application/json'