From d1f08e3dfac1da68f7d9d0084db60c08e2e35b83 Mon Sep 17 00:00:00 2001 From: Xeovalyte Date: Wed, 7 Jun 2023 22:29:18 +0200 Subject: [PATCH] feat: Added team joining --- .../api/teams/[id]/members/index.post.ts | 27 +++++++++++++++++++ .../server/api/users/[id]/teamInvites.post.ts | 4 +++ 2 files changed, 31 insertions(+) create mode 100644 webv2/server/api/teams/[id]/members/index.post.ts diff --git a/webv2/server/api/teams/[id]/members/index.post.ts b/webv2/server/api/teams/[id]/members/index.post.ts new file mode 100644 index 0000000..a861293 --- /dev/null +++ b/webv2/server/api/teams/[id]/members/index.post.ts @@ -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 +}) diff --git a/webv2/server/api/users/[id]/teamInvites.post.ts b/webv2/server/api/users/[id]/teamInvites.post.ts index a79392c..805199f 100644 --- a/webv2/server/api/users/[id]/teamInvites.post.ts +++ b/webv2/server/api/users/[id]/teamInvites.post.ts @@ -3,6 +3,10 @@ export default defineEventHandler(async (event) => { 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 { teamInvites } = await UserModel.findByIdAndUpdate(user._id, {