feat: Added applyUsername function

This commit is contained in:
Xeovalyte 2023-06-06 13:49:27 +02:00
parent 89027590ca
commit e28d2278cb
8 changed files with 25 additions and 7 deletions

View File

@ -4,7 +4,7 @@ export default defineNuxtRouteMiddleware(async (to) => {
}
try {
const user = await $fetch('/api/users/@me')
const user: IUser = await $fetch('/api/users/@me')
if (!user._id) {
throw createError({ statusCode: 500, statusMessage: 'No user was found' })

View File

@ -1,5 +1,8 @@
export default defineEventHandler(async (event) => {
const userId: string = event.context.params ? event.context.params.id : '@me'
return await getUser(userId, event)
const user = await getUser(userId, event)
await applyUsername(user)
return user
})

View File

@ -11,7 +11,7 @@ export default defineEventHandler(async (event) => {
await WhitelistModel.deleteOne({ uuid: user.minecraft.uuid })
user.minecraft = undefined
await user.save()
await applyUsername(user)
} catch (e) {
console.error('Failed to update documents', e)

View File

@ -12,7 +12,7 @@ export default defineEventHandler(async (event) => {
try {
user.minecraft = { uuid: user.minecraft.uuid, username: minecraftProfile.name }
await user.save()
await applyUsername(user)
} catch (e) {
console.error('Failed to update document', e)

View File

@ -28,7 +28,7 @@ export default defineEventHandler(async (event) => {
user.minecraft = { uuid: whitelistDoc.uuid, username: minecraftProfile.name }
await whitelistDoc.save()
await user.save()
await applyUsername(user)
} catch (e) {
console.error('Failed to update documents', e)

View File

@ -1,7 +1,7 @@
import { Schema, Types, model } from 'mongoose'
const userSchema = new Schema({
username: { type: String, required: true },
username: { type: String, required: false },
usernameType: { type: String, required: true, default: 'discord' },
discord: {
id: { type: String, required: true, unique: true },

View File

@ -46,3 +46,15 @@ export const getUser = async (userId: string, event: any) => {
throw createError({ statusCode: 500, statusMessage: 'Failed to get user' })
}
}
export const applyUsername = async (user: IUser) => {
if (user.usernameType === 'discord' || user.$isEmpty('minecraft') || !user.minecraft) {
user.username = user.discord.username
await user.save()
} else {
user.username = user.minecraft.username
await user.save()
}
}

View File

@ -1,9 +1,12 @@
import { Document } from 'mongoose'
export {}
declare global {
interface IUser {
interface IUser extends Document {
_id: string,
username: string,
usernameType: string,
discord: {
id: string,
username: string,