Skip to content

Commit

Permalink
restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratyush committed Mar 15, 2024
1 parent dc566ee commit 17ed443
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 249 deletions.
52 changes: 52 additions & 0 deletions app/api/event2/createTeam/route.js
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,
});
}
}
118 changes: 66 additions & 52 deletions app/api/event2/deleteTeam/[teamId]/route.js
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 });
}
}
}
22 changes: 11 additions & 11 deletions app/api/event2/getTeamCode/route.js
Original file line number Diff line number Diff line change
@@ -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" });
}
Expand All @@ -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" });
Expand All @@ -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 } }
);
Expand Down
65 changes: 31 additions & 34 deletions app/api/event2/getTeamData/route.js
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,
});
}
}
}
15 changes: 9 additions & 6 deletions app/api/event2/getTeamViaToken/route.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Loading

0 comments on commit 17ed443

Please sign in to comment.