From f3a5a3b63470f1d021f43feca8d137ef39fd5c62 Mon Sep 17 00:00:00 2001 From: Smriti Doneria Date: Fri, 15 Mar 2024 18:19:52 +0530 Subject: [PATCH] delete team --- app/api/event1/deleteTeam/[teamId]/route.js | 13 ++--- app/api/event1/getTeamData/route.js | 1 + app/api/event1/getTeamViaToken/route.js | 3 +- app/api/event1/leaveTeam/route.js | 11 +++-- app/api/event1/removeMember/[teamId]/route.js | 47 ++++++++++++------- app/events/event1/leaderDash/page.jsx | 9 ++-- app/events/event1/memberDash/page.jsx | 2 +- utils/authuser.js | 2 +- 8 files changed, 54 insertions(+), 34 deletions(-) diff --git a/app/api/event1/deleteTeam/[teamId]/route.js b/app/api/event1/deleteTeam/[teamId]/route.js index 9c3ce08..b8b570c 100644 --- a/app/api/event1/deleteTeam/[teamId]/route.js +++ b/app/api/event1/deleteTeam/[teamId]/route.js @@ -1,6 +1,7 @@ import { connectMongoDB } from "@/lib/mongodb"; -import { TeamModel } from "@/models/TeamDetails"; -import { TeamToken } from "@/models/event1TeamToken"; +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"; @@ -19,7 +20,7 @@ export async function POST(req,{params}){ const teamId=params.teamId; - const team = await TeamModel.findById({ _id: teamId }); + const team = await Event1.findById({ _id: teamId }); console.log(team); if (!team) { @@ -38,17 +39,17 @@ export async function POST(req,{params}){ return NextResponse.json({ message: "Team Size more than 1", status: 200 }); } - await TeamModel.findOneAndDelete({ + await Event1.findOneAndDelete({ _id: teamId, }); - await TeamToken.findOneAndDelete({ + await event1TeamToken.findOneAndDelete({ teamId: teamId, }); await Users.findByIdAndUpdate( { _id: userId }, - { teamId: null, teamRole: null } + { event1TeamId: null, event1TeamRole: null } ); diff --git a/app/api/event1/getTeamData/route.js b/app/api/event1/getTeamData/route.js index 9a901e7..ca136be 100644 --- a/app/api/event1/getTeamData/route.js +++ b/app/api/event1/getTeamData/route.js @@ -24,6 +24,7 @@ export async function GET(req) { const teamId = user.event1TeamId; const team = await Event1.findById(teamId).populate('members'); + console.log("^^^^^^",team); if (!team) { return NextResponse.json({ message: 'Team is not there ' }); } diff --git a/app/api/event1/getTeamViaToken/route.js b/app/api/event1/getTeamViaToken/route.js index 65747c8..32be7bb 100644 --- a/app/api/event1/getTeamViaToken/route.js +++ b/app/api/event1/getTeamViaToken/route.js @@ -10,8 +10,9 @@ export async function POST(req) { 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); diff --git a/app/api/event1/leaveTeam/route.js b/app/api/event1/leaveTeam/route.js index 75fdc98..fecdd50 100644 --- a/app/api/event1/leaveTeam/route.js +++ b/app/api/event1/leaveTeam/route.js @@ -1,14 +1,15 @@ import { connectMongoDB } from "@/lib/mongodb"; -import { TeamModel } from "@/models/TeamDetails"; 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"; export async function POST(req){ try{ + console.log("yha aa rha h bhiiiiiiii") await connectMongoDB(); const token = await getToken({req}) @@ -17,6 +18,7 @@ export async function POST(req){ console.log(userId); const user = await Users.findById(userId); + console.log("*********",user); if (user.event1TeamRole != "1") { return NextResponse.json({ @@ -30,7 +32,9 @@ export async function POST(req){ }); } - const team = await TeamModel.findById(user.event1TeamId); + const team = await Event1.findById(user.event1TeamId); + + console.log("teamId++++",team) if (!team) { return NextResponse.json({ message: "Team not found", @@ -40,7 +44,8 @@ export async function POST(req){ team.members.pull(userId); await team.save(); - await Users.findByIdAndUpdate(userId, { $set: { teamId: null, teamRole:-1 } }); + await Users.findByIdAndUpdate(userId, { $set: { event1TeamId: null, event1TeamRole:-1 } }); + console.log("teamId++++@@@@@@@@@") diff --git a/app/api/event1/removeMember/[teamId]/route.js b/app/api/event1/removeMember/[teamId]/route.js index caddf85..76c88a7 100644 --- a/app/api/event1/removeMember/[teamId]/route.js +++ b/app/api/event1/removeMember/[teamId]/route.js @@ -1,59 +1,70 @@ import { connectMongoDB } from "@/lib/mongodb"; -import { TeamModel } from "@/models/TeamDetails"; -import { UsersDetails } from "@/models/Userdetails"; + + 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 TeamModel.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", }); } - - await UsersDetails.findOneAndUpdate( - { _id: req.body.userId }, - { teamId: null, teamRole: -1 } + console.log("================") + await Users.findOneAndUpdate( + { _id: userId }, + { event1TeamId: null, event1TeamRole + : -1 } ); //updating team - await TeamModel.findOneAndUpdate( - { _id: req.params.teamId }, + await Event1.findOneAndUpdate( + { _id: teamId }, { $pull: { members: req.body.userId } } ); diff --git a/app/events/event1/leaderDash/page.jsx b/app/events/event1/leaderDash/page.jsx index 46aec57..a6ba51d 100644 --- a/app/events/event1/leaderDash/page.jsx +++ b/app/events/event1/leaderDash/page.jsx @@ -110,9 +110,10 @@ export default function LeaderDashboard() { setPopUpForDelete(!popUpForDelete); } function removeMember(id) { + console.log("||||||",teamId); setRemove((prev) => !prev); setIsLoading(true); - fetch('/team/remove/' + teamId, { + fetch(`/api/event1/removeMember/${teamId}`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -125,7 +126,7 @@ export default function LeaderDashboard() { }) .then((res) => res.json()) .then((data) => { - location.reload(); + //location.reload(); setIsLoading(false); }) .then(() => { @@ -141,7 +142,7 @@ export default function LeaderDashboard() { } setIsLoading(true); setDeleted(!deleted); - fetch('/team/deleteTeam/' + teamId, { + fetch(`/api/event1/deleteTeam/${teamId}` , { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -152,7 +153,7 @@ export default function LeaderDashboard() { .then((res) => res.json()) .then((data) => {}) .then(() => { - router.push('/makeTeam'); + router.push('/events/event1/makeTeam'); toast.success('Team Deleted.'); setIsLoading(false); }) diff --git a/app/events/event1/memberDash/page.jsx b/app/events/event1/memberDash/page.jsx index 565ea64..fe13e52 100644 --- a/app/events/event1/memberDash/page.jsx +++ b/app/events/event1/memberDash/page.jsx @@ -97,7 +97,7 @@ const TeamPage = () => { const leaveTeam = () => { setIsLoading(true); - fetch( '/api/event1/leaveTeam/'+teamId, { + fetch( '/api/event1/leaveTeam', { content: "application/json", method: "POST", headers: { diff --git a/utils/authuser.js b/utils/authuser.js index ed80514..9c1392f 100644 --- a/utils/authuser.js +++ b/utils/authuser.js @@ -18,7 +18,7 @@ export async function getTokenDetails(token) { console.log("+++++++",userId) const user = await Users.findById(userId); - + console.log("\\\\",user); return userId;