From f9c13e5ada40c590c587cf0e93cea1c23d2df50b Mon Sep 17 00:00:00 2001 From: Xeovalyte Date: Tue, 6 Jun 2023 18:22:36 +0200 Subject: [PATCH] feat: Added team leave --- webv2/.eslintrc | 3 +- webv2/server/api/teams/[id]/index.get.ts | 11 +++++-- .../api/teams/[id]/members/[userId].delete.ts | 30 +++++++++++++++++++ webv2/server/api/teams/index.post.ts | 15 +++++++--- webv2/server/utils/models.ts | 8 +++-- webv2/server/utils/user.ts | 2 +- webv2/types/global.d.ts | 12 ++++---- 7 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 webv2/server/api/teams/[id]/members/[userId].delete.ts diff --git a/webv2/.eslintrc b/webv2/.eslintrc index be01fa5..c7ff188 100644 --- a/webv2/.eslintrc +++ b/webv2/.eslintrc @@ -13,6 +13,7 @@ "vue/multi-word-component-names": "off", "vue/singleline-html-element-content-newline": "off", "no-undef": "off", - "no-console": "off" + "no-console": "off", + "space-before-function-paren": "off" } } diff --git a/webv2/server/api/teams/[id]/index.get.ts b/webv2/server/api/teams/[id]/index.get.ts index ff73666..a60b1c5 100644 --- a/webv2/server/api/teams/[id]/index.get.ts +++ b/webv2/server/api/teams/[id]/index.get.ts @@ -1,3 +1,10 @@ -export default defineEventHandler((event) => { - const userId: string = event.context.params ? event.context.params.id : '@me' +export default defineEventHandler(async (event) => { + let teamId: string = event.context.params ? event.context.params.id : '@current' + + if (teamId === '@current') { + const user = await getUser('@me', event) + teamId = user.team._id.toString() + } + + return await TeamModel.findById(teamId).populate('members') }) diff --git a/webv2/server/api/teams/[id]/members/[userId].delete.ts b/webv2/server/api/teams/[id]/members/[userId].delete.ts new file mode 100644 index 0000000..f79e22b --- /dev/null +++ b/webv2/server/api/teams/[id]/members/[userId].delete.ts @@ -0,0 +1,30 @@ +export default defineEventHandler(async (event) => { + let teamId: string = event.context.params ? event.context.params.id : '@current' + const userId: string = event.context.params ? event.context.params.userId : '@me' + + const user = await getUser(userId, event) + + if (!user.team) { + throw createError({ statusCode: 400, statusMessage: 'User must be in a team' }) + } + + if (teamId === '@current') { + teamId = user.team._id.toString() + } + + const team = await TeamModel.findOneAndUpdate({ _id: user.team._id }, { + $pull: { + members: user._id + } + }, { returnDocument: 'after' }) + + if (team && team.members.length < 1) { + team.deleteOne() + } + + user.team = undefined + + await user.save() + + return '' +}) diff --git a/webv2/server/api/teams/index.post.ts b/webv2/server/api/teams/index.post.ts index f0d51dd..3977d06 100644 --- a/webv2/server/api/teams/index.post.ts +++ b/webv2/server/api/teams/index.post.ts @@ -1,16 +1,23 @@ export default defineEventHandler(async (event) => { - const { name, color } = await readBody(event) + const { name, color, userId } = await readBody(event) - const user = await getUser('@me', event) + const user = await getUser(userId ?? '@me', event) + + if (user.team) { + throw createError({ statusCode: 400, statusMessage: 'User already in a team' }) + } const team = new TeamModel({ name, - color + color, + members: [ + user._id + ] }) await team.save() - user.teamId = team._id + user.team = team._id await user.save() diff --git a/webv2/server/utils/models.ts b/webv2/server/utils/models.ts index 9b483c6..38ca8a9 100644 --- a/webv2/server/utils/models.ts +++ b/webv2/server/utils/models.ts @@ -3,7 +3,7 @@ import { Schema, Types, model } from 'mongoose' const userSchema = new Schema({ username: { type: String, required: false }, usernameType: { type: String, required: true, default: 'discord' }, - teamId: { type: Types.ObjectId, ref: 'Team', required: false }, + team: { type: Types.ObjectId, ref: 'Team', required: false }, discord: { id: { type: String, required: true, unique: true }, username: { type: String, required: true } @@ -31,9 +31,11 @@ const whitelistSchema = new Schema({ const teamSchema = new Schema({ name: { type: String, required: true, minLength: 3, maxLength: 16, unique: true, match: /^[a-zA-Z0-9]+$/ }, color: { type: String, required: true, match: /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/ }, - amount: { type: Number, required: true, default: 1 }, textChannelId: { type: String, required: false }, - voiceChannelId: { type: String, required: false } + voiceChannelId: { type: String, required: false }, + members: [ + { type: Types.ObjectId, ref: 'User' } + ] }) export const UserModel = model('User', userSchema) diff --git a/webv2/server/utils/user.ts b/webv2/server/utils/user.ts index 660f686..8819dc5 100644 --- a/webv2/server/utils/user.ts +++ b/webv2/server/utils/user.ts @@ -33,7 +33,7 @@ export const getUser = async (userId: string, event: any) => { } try { - const user = await UserModel.findById(userId) + const user = await UserModel.findById(userId).populate('team') if (!user) { throw createError({ statusCode: 400, statusMessage: 'No user was found' }) diff --git a/webv2/types/global.d.ts b/webv2/types/global.d.ts index b75bf2d..8a57b2e 100644 --- a/webv2/types/global.d.ts +++ b/webv2/types/global.d.ts @@ -1,10 +1,10 @@ -import { Document } from 'mongoose' +import { Document, Types } from 'mongoose' export {} declare global { interface IUser extends Document { - _id: string, + _id: Types.ObjectId, username: string, usernameType: string, discord: { @@ -21,22 +21,22 @@ declare global { admin: boolean, moderator: boolean, }, - teamId: string, + team?: ITeam, accessToken?: string } interface IWhitelist { - _id: string, + _id: types.ObjectId, uuid: string, connected: boolean, code: string } interface ITeam { - _id: string, + _id: types.ObjectId, name: string, color: string, - amount: number, + members: string[], textChannelId: string, voiceChannelId: string }