feat: Added team joining
This commit is contained in:
parent
70b0b86a23
commit
d1f08e3dfa
27
webv2/server/api/teams/[id]/members/index.post.ts
Normal file
27
webv2/server/api/teams/[id]/members/index.post.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const { userId } = await readBody(event)
|
||||||
|
const teamId: string = event.context.params ? event.context.params.id : '@current'
|
||||||
|
|
||||||
|
if (teamId === '@current') {
|
||||||
|
throw createError({ statusCode: 400, statusMessage: 'Cannot join your own team' })
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await getUser(userId, event)
|
||||||
|
|
||||||
|
if (user.team) {
|
||||||
|
throw createError({ statusCode: 400, statusMessage: 'User already is in a team' })
|
||||||
|
}
|
||||||
|
|
||||||
|
const team = await TeamModel.findById(teamId).populate('members')
|
||||||
|
|
||||||
|
if (!team) {
|
||||||
|
throw createError({ statusCode: 500, statusMessage: 'Team does not exist' })
|
||||||
|
}
|
||||||
|
|
||||||
|
team.members.push(userId)
|
||||||
|
user.team = team._id
|
||||||
|
|
||||||
|
team.save()
|
||||||
|
|
||||||
|
return team
|
||||||
|
})
|
@ -3,6 +3,10 @@ export default defineEventHandler(async (event) => {
|
|||||||
|
|
||||||
const userId: string = event.context.params ? event.context.params.id : '@me'
|
const userId: string = event.context.params ? event.context.params.id : '@me'
|
||||||
|
|
||||||
|
if (userId === '@me') {
|
||||||
|
throw createError({ statusCode: 400, statusMessage: 'Cannot invite current user to a team' })
|
||||||
|
}
|
||||||
|
|
||||||
const user = await getUser(userId, event)
|
const user = await getUser(userId, event)
|
||||||
|
|
||||||
const { teamInvites } = await UserModel.findByIdAndUpdate(user._id, {
|
const { teamInvites } = await UserModel.findByIdAndUpdate(user._id, {
|
||||||
|
Loading…
Reference in New Issue
Block a user