feat: Added team leave
All checks were successful
Build and Deploy / Deploy Web (push) Successful in 1m3s
Build and Deploy / Deploy Discord Bot (push) Successful in 43s

This commit is contained in:
Xeovalyte 2023-06-06 18:22:36 +02:00
parent 034e203c1e
commit f9c13e5ada
7 changed files with 64 additions and 17 deletions

View File

@ -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"
}
}

View File

@ -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')
})

View File

@ -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 ''
})

View File

@ -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()

View File

@ -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<IUser>('User', userSchema)

View File

@ -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' })

View File

@ -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
}