Skip to content

Commit

Permalink
joinTeam backend
Browse files Browse the repository at this point in the history
  • Loading branch information
pratyush3124 committed Mar 14, 2024
1 parent 5b9a095 commit 1eab616
Showing 1 changed file with 45 additions and 39 deletions.
84 changes: 45 additions & 39 deletions app/api/event1/joinTeam/route.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,83 @@
import { connectMongoDB } from "@/lib/mongodb";
import { TeamModel } from "@/models/TeamDetails";
import { TeamToken } 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";
import { connectMongoDB } from '@/lib/mongodb';
import { Event1 } from '@/models/TeamDetails';
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{

export async function POST(req, { params }) {
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({ _id: userId});
console.log(user)

const user = await Users.findById({ _id: userId });
console.log(user);

if (user.event1TeamId) {
return NextResponse.json({ message: "User is already a part of team" });
return NextResponse.json({
message: 'User is already a part of team',
});
}
const {code} = await req.json();

const { code } = await req.json();
console.log(code);
const team = await TeamModel.findOne({ teamCode: code });
const team = await Event1.findOne({ teamCode: code });
//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 teamToken = await TeamToken.findOne({ teamId: team._id });
const Event1TeamToken = await event1TeamToken.findOne({ teamId: team._id });

if (!teamToken) {
if (!Event1TeamToken) {
return res.status(404).json({ error: 'Token not found' });
}



const currentTime = new Date();
const tokenCreationTime = token.createdAt;

const timeDifference = (currentTime - tokenCreationTime) / (1000 * 60); // Difference in minutes
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.' });
return NextResponse.json({
error: 'Token expired. Ask leader to generate a new token.',
});
}
if (code !== token.token) {
return NextResponse.json({ error: 'Incorrect token' });
}

await Users.findOneAndUpdate({ _id: userId }, { $set: { teamId: team.id, teamRole: 1 } });
await Users.findOneAndUpdate(
{ _id: userId },
{ $set: { teamId: team.id, teamRole: 1 } }
);

await TeamModel.findOneAndUpdate(
await Event1.findOneAndUpdate(
{
_id: team._id,
_id: team._id,
},
{
$push: { members: userId },
$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 });
);
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,
});
}
}
}

0 comments on commit 1eab616

Please sign in to comment.