Skip to content

Commit

Permalink
aass
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratyush committed Mar 15, 2024
1 parent f7648a9 commit 49ccef5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
61 changes: 31 additions & 30 deletions app/api/event1/leaveTeam/route.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,58 @@
import { connectMongoDB } from "@/lib/mongodb";
import { TeamModel } from "@/models/TeamDetails";
import { Users } from "@/models/user.model";
import { NextResponse } from "next/server";
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";
import { getToken } from 'next-auth/jwt';
import { getTokenDetails } from '../../../../utils/authuser';


export async function POST(req){
try{
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 Users.findById(userId);

if (user.event1TeamRole != "1") {
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 TeamModel.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 Users.findByIdAndUpdate(userId, { $set: { teamId: null, teamRole:-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 });
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,
});
}
}
}
3 changes: 2 additions & 1 deletion app/events/event1/memberDash/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -107,6 +107,7 @@ const TeamPage = () => {
},
}).then(data=>data.json())
.then(data=>{
console.log('data', data)
if(data.error == false) {
setIsLoading(true);
router.push('/makeTeam')
Expand Down

0 comments on commit 49ccef5

Please sign in to comment.