-
Notifications
You must be signed in to change notification settings - Fork 2
Teams leave #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SivamshIndukuri
wants to merge
15
commits into
dev
Choose a base branch
from
teams-leave
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Teams leave #136
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
aa7b964
team leave end point
SivamshIndukuri 45e8dbe
test cases and updated leave logic
SivamshIndukuri e1245ae
add one more case
SivamshIndukuri 78e0bc6
fixed to include disband endpoint
SivamshIndukuri 2f9e603
fixed serverless.ts
SivamshIndukuri 1f5b2b0
Merge branch 'dev' into teams-leave
SivamshIndukuri e4f7ba8
serverless.ts prettier
SivamshIndukuri a60009d
minor fixes
SivamshIndukuri c41193a
typescript error fix
SivamshIndukuri 79ad216
typescript error fix
SivamshIndukuri 05eaa6b
name change
SivamshIndukuri b2c44ac
prettier
SivamshIndukuri 6b4f24c
fixed lead logic
SivamshIndukuri 7318757
valid team lead check
SivamshIndukuri b2d6008
valid team lead check
SivamshIndukuri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import type { ValidatedEventAPIGatewayProxyEvent } from '@libs/api-gateway'; | ||
| import { middyfy } from '@libs/lambda'; | ||
| import schema from './schema'; | ||
| import { UserDocument, TeamDocument } from '../../../types'; | ||
| import { MongoDB, validateToken, disbandTeam } from '../../../util'; //change to actual disband | ||
| import * as path from 'path'; | ||
| import * as dotenv from 'dotenv'; | ||
|
|
||
| //import fetch from 'node-fetch'; | ||
|
|
||
| dotenv.config({ path: path.resolve(process.cwd(), '.env') }); | ||
|
|
||
| const teamLeave: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (event) => { | ||
| try { | ||
| const { auth_token: authToken, auth_email: authEmail, team_id: teamId } = event.body; | ||
|
|
||
| // 1. Validate auth token | ||
| const tokenValid = validateToken(authToken, process.env.JWT_SECRET, authEmail); | ||
| if (!tokenValid) { | ||
| return { | ||
| statusCode: 401, | ||
| body: JSON.stringify({ statusCode: 401, message: 'Unauthorized' }), | ||
| }; | ||
| } | ||
|
|
||
| // 2. connect to mongoDB | ||
| const db = MongoDB.getInstance(process.env.MONGO_URI); | ||
| await db.connect(); | ||
| const users = db.getCollection<UserDocument>('users'); | ||
| const teams = db.getCollection<TeamDocument>('teams'); | ||
|
|
||
| // 3. check if user exisits | ||
| const authUser = await users.findOne({ email: authEmail }); | ||
| 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: teamId }); | ||
| 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' }), | ||
| }; | ||
| } | ||
|
|
||
| // 6. Check is teamlead is real | ||
| const teamAuth = await users.findOne({ email: team.leader_email }); | ||
| if (!teamAuth) { | ||
| return { | ||
| statusCode: 400, | ||
| body: JSON.stringify({ statusCode: 400, message: 'Invalid team lead' }), | ||
| }; | ||
| } | ||
|
|
||
| // 7. No team members | ||
| if (team.members.length + 1 == 0) { | ||
| return { | ||
| statusCode: 400, | ||
| body: JSON.stringify({ statusCode: 400, message: 'Empty team member list' }), | ||
| }; | ||
| } | ||
|
|
||
| // 8. Check if user is in team | ||
| if (!team.members.includes(authEmail) && team.leader_email !== authEmail) { | ||
| return { | ||
| statusCode: 400, | ||
| body: JSON.stringify({ statusCode: 400, message: 'User not in team' }), | ||
| }; | ||
| } | ||
|
|
||
| //grabs team info object | ||
| const teamInfo = authUser.team_info; | ||
|
|
||
| // 9. Check if user is team lead | ||
| if (teamInfo.role == 'leader') return await disbandTeam(authToken, authEmail, teamId); | ||
|
|
||
| // 10. Remove user from team | ||
| await teams.updateOne( | ||
| { team_id: teamId, members: authUser.email }, | ||
| { | ||
| $pull: { | ||
| members: authUser.email, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| // 11. clear team_info and set confirmed_team | ||
|
|
||
| authUser.confirmed_team = false; | ||
| authUser.team_info = { | ||
| team_id: '', | ||
| role: null, | ||
| pending_invites: [], | ||
| }; | ||
|
|
||
| // 12. update the MongoDB user | ||
| await users.updateOne( | ||
| { email: authEmail }, | ||
| { | ||
| $set: { | ||
| confirmed_team: authUser.confirmed_team, | ||
| team_info: authUser.team_info, | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| return { | ||
| statusCode: 200, | ||
| body: JSON.stringify({ message: 'Successfully left team' }), | ||
| }; | ||
| } 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); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| 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, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 existscheck 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.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.