Skip to content

Commit

Permalink
Merge pull request #30 from smritidoneria/main
Browse files Browse the repository at this point in the history
del-fox
  • Loading branch information
pratyush3124 authored Mar 15, 2024
2 parents 3238a22 + 87d7a3c commit e9e22b7
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 32 deletions.
13 changes: 7 additions & 6 deletions app/api/event1/deleteTeam/[teamId]/route.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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) {
Expand All @@ -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 }
);


Expand Down
1 change: 1 addition & 0 deletions app/api/event1/getTeamData/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' });
}
Expand Down
3 changes: 2 additions & 1 deletion app/api/event1/getTeamViaToken/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion app/api/event1/leaveTeam/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NextResponse } from 'next/server';

import { getToken } from 'next-auth/jwt';
import { getTokenDetails } from '../../../../utils/authuser';

///
export async function POST(req) {
try {
await connectMongoDB();
Expand All @@ -16,6 +16,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({
Expand Down
47 changes: 29 additions & 18 deletions app/api/event1/removeMember/[teamId]/route.js
Original file line number Diff line number Diff line change
@@ -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 } }
);

Expand Down
9 changes: 5 additions & 4 deletions app/events/event1/leaderDash/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -125,7 +126,7 @@ export default function LeaderDashboard() {
})
.then((res) => res.json())
.then((data) => {
location.reload();
//location.reload();
setIsLoading(false);
})
.then(() => {
Expand All @@ -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',
Expand All @@ -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);
})
Expand Down
2 changes: 1 addition & 1 deletion app/events/event1/memberDash/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const TeamPage = () => {
console.log('data', data)
if(data.error == false) {
setIsLoading(true);
router.push('/makeTeam')
router.push('/events/event/makeTeam')
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion utils/authuser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function getTokenDetails(token) {
console.log("+++++++",userId)

const user = await Users.findById(userId);

console.log("\\\\",user);

return userId;

Expand Down

0 comments on commit e9e22b7

Please sign in to comment.