Skip to content
Open
131 changes: 131 additions & 0 deletions src/functions/teams/leave/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import type { ValidatedEventAPIGatewayProxyEvent } from '@libs/api-gateway';
import { middyfy } from '@libs/lambda';
import schema from './schema';
import { MongoDB, validateToken, UserDoc, TeamDoc, teamsDisband } from '../../../util'; //change to actual disband
Copy link

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment indicates this is temporary code that needs to be replaced. Consider using a proper TODO comment format like // TODO: Replace teamsDisband with actual implementation.

Suggested change
import { MongoDB, validateToken, UserDoc, TeamDoc, teamsDisband } from '../../../util'; //change to actual disband
import { MongoDB, validateToken, UserDoc, TeamDoc, teamsDisband } from '../../../util'; // TODO: Replace teamsDisband with actual implementation

Copilot uses AI. Check for mistakes.
import * as path from 'path';
import * as dotenv from 'dotenv';

//import fetch from 'node-fetch';

dotenv.config({ path: path.resolve(process.cwd(), '.env') });


Check failure on line 12 in src/functions/teams/leave/handler.ts

View workflow job for this annotation

GitHub Actions / eslint

More than 1 blank line not allowed
const teamLeave: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (event) => {
try{

const {auth_token, auth_email, team_id} = event.body;

Check failure on line 16 in src/functions/teams/leave/handler.ts

View workflow job for this annotation

GitHub Actions / eslint

Variable name `team_id` must match one of the following formats: camelCase, UPPER_CASE, PascalCase

Check failure on line 16 in src/functions/teams/leave/handler.ts

View workflow job for this annotation

GitHub Actions / eslint

Variable name `auth_email` must match one of the following formats: camelCase, UPPER_CASE, PascalCase

Check failure on line 16 in src/functions/teams/leave/handler.ts

View workflow job for this annotation

GitHub Actions / eslint

Variable name `auth_token` must match one of the following formats: camelCase, UPPER_CASE, PascalCase

// 1. Validate auth token
const tokenValid = validateToken(auth_token, process.env.JWT_SECRET, auth_email);
if(!tokenValid){
return{
statusCode:401,
body: JSON.stringify({statusCode:401, message: 'Unauthorized'})
}
}

// 2. connect to mongoDB
const db = MongoDB.getInstance(process.env.MONGO_URI);

Check failure on line 28 in src/functions/teams/leave/handler.ts

View workflow job for this annotation

GitHub Actions / eslint

Multiple spaces found before 'MongoDB'
await db.connect();
const users = db.getCollection<UserDoc>('users');
const teams = db.getCollection<TeamDoc>('teams');

// 3. check if user exisits
const authUser = await users.findOne({email:auth_email})
if(!authUser){
return{
statusCode: 404,
body: JSON.stringify({statusCode:404, message: 'Auth user not found'})
}
}

// 4. check if team exisits
const team = await teams.findOne({team_id:team_id})
if(!team){
return{
statusCode:404,
body: JSON.stringify({statusCode:404, message: 'Team not found'})
}
}

// 5. Check if team is disbanded
if(team.status == "Disbanded"){
return{
statusCode:400,
body: JSON.stringify({statusCode:400, message: "Team already disbanded"})
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do a validation check on leader email. Simple if exists check will be enough. The reason why we want to do this is because we may do a leader_email check later on. To be fair, a team's state should never be in this spot (where a team has no leader) but just to be safe.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want this check to be done if the leader wants to leave the team or always when running this endpoint

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either or is fine. I think they are functionally equal so I'll leave it to your best judgement.

// 6. No team members
if(team.members.length == 0){
return{
statusCode: 400,
body: JSON.stringify({statusCode:400, message: 'Empty team member list'})
}
}

// 7. Check if user is in team
if (!team.members.includes(auth_email)) {
return{
statusCode: 400,
body: JSON.stringify({statusCode:400, message: 'User not in team'})
}
}

//grabs team info object
const teamInfo = authUser.team_info

// 8. Check if user is team lead
if(teamInfo.role == "leader"){

Check failure on line 79 in src/functions/teams/leave/handler.ts

View workflow job for this annotation

GitHub Actions / eslint

Unnecessary { after 'if' condition
return await teamsDisband(auth_token, auth_email, team_id); //mocked function replace with right funtion name
Copy link

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a typo in the comment: 'funtion' should be 'function'.

Suggested change
return await teamsDisband(auth_token, auth_email, team_id); //mocked function replace with right funtion name
return await teamsDisband(auth_token, auth_email, team_id); //mocked function replace with right function name

Copilot uses AI. Check for mistakes.
}


Check failure on line 83 in src/functions/teams/leave/handler.ts

View workflow job for this annotation

GitHub Actions / eslint

More than 1 blank line not allowed
// 9. Remove user from team
await teams.updateOne(
{ team_id: team_id, members: authUser.email},
{
$pull:{
members: authUser.email
}
}
)


Check failure on line 94 in src/functions/teams/leave/handler.ts

View workflow job for this annotation

GitHub Actions / eslint

More than 1 blank line not allowed
// 10. clear team_info and set confirmed_team

authUser.confirmed_team = false
authUser.team_info = {
team_id: "",
role: "",
pending_invites: []
};

// 11. update the mondoDB user
Copy link

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a typo in the comment: 'mondoDB' should be 'MongoDB'.

Suggested change
// 11. update the mondoDB user
// 11. update the MongoDB user

Copilot uses AI. Check for mistakes.
await users.updateOne(
{ email: auth_email},
{
$set:{
confirmed_team: authUser.confirmed_team,
team_info: authUser.team_info
}
}
)

return {
statusCode:200,
body: JSON.stringify({"message": "Successfully left team"})
}

}

Check failure on line 120 in src/functions/teams/leave/handler.ts

View workflow job for this annotation

GitHub Actions / eslint

Closing curly brace does not appear on the same line as the subsequent block

catch (error){
console.error('Error deleting team member:', error);
return {
statusCode: 500,
body: JSON.stringify({ statusCode: 500, message: 'Internal server error' }),
};
}
}

export const main = middyfy(teamLeave);
22 changes: 22 additions & 0 deletions src/functions/teams/leave/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { handlerPath } from '@libs/handler-resolver';
import schema from './schema';

export default {
handler: `${handlerPath(__dirname)}/handler.main`,
events: [
{
http: {
method: 'post',
path: 'teams/leave',
cors: true,
request: {
schemas: {
'application/json': schema,
},
},
},
},
],
};


Check failure on line 22 in src/functions/teams/leave/index.ts

View workflow job for this annotation

GitHub Actions / eslint

Too many blank lines at the end of file. Max of 1 allowed
9 changes: 9 additions & 0 deletions src/functions/teams/leave/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
type: 'object',
properties: {
auth_token: { type: 'string' },
auth_email: { type: 'string', format: 'email' },
team_id: {type:"string"}
},
required: ['auth_token', 'auth_email', 'team_id'],
} as const;
Loading
Loading