feat: Added applyUsername function
This commit is contained in:
parent
89027590ca
commit
e28d2278cb
@ -4,7 +4,7 @@ export default defineNuxtRouteMiddleware(async (to) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const user = await $fetch('/api/users/@me')
|
const user: IUser = await $fetch('/api/users/@me')
|
||||||
|
|
||||||
if (!user._id) {
|
if (!user._id) {
|
||||||
throw createError({ statusCode: 500, statusMessage: 'No user was found' })
|
throw createError({ statusCode: 500, statusMessage: 'No user was found' })
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
export default defineEventHandler(async (event) => {
|
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'
|
||||||
|
|
||||||
return await getUser(userId, event)
|
const user = await getUser(userId, event)
|
||||||
|
await applyUsername(user)
|
||||||
|
|
||||||
|
return user
|
||||||
})
|
})
|
||||||
|
@ -11,7 +11,7 @@ export default defineEventHandler(async (event) => {
|
|||||||
await WhitelistModel.deleteOne({ uuid: user.minecraft.uuid })
|
await WhitelistModel.deleteOne({ uuid: user.minecraft.uuid })
|
||||||
user.minecraft = undefined
|
user.minecraft = undefined
|
||||||
|
|
||||||
await user.save()
|
await applyUsername(user)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to update documents', e)
|
console.error('Failed to update documents', e)
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ export default defineEventHandler(async (event) => {
|
|||||||
try {
|
try {
|
||||||
user.minecraft = { uuid: user.minecraft.uuid, username: minecraftProfile.name }
|
user.minecraft = { uuid: user.minecraft.uuid, username: minecraftProfile.name }
|
||||||
|
|
||||||
await user.save()
|
await applyUsername(user)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to update document', e)
|
console.error('Failed to update document', e)
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ export default defineEventHandler(async (event) => {
|
|||||||
user.minecraft = { uuid: whitelistDoc.uuid, username: minecraftProfile.name }
|
user.minecraft = { uuid: whitelistDoc.uuid, username: minecraftProfile.name }
|
||||||
|
|
||||||
await whitelistDoc.save()
|
await whitelistDoc.save()
|
||||||
await user.save()
|
await applyUsername(user)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to update documents', e)
|
console.error('Failed to update documents', e)
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { Schema, Types, model } from 'mongoose'
|
import { Schema, Types, model } from 'mongoose'
|
||||||
|
|
||||||
const userSchema = new Schema({
|
const userSchema = new Schema({
|
||||||
username: { type: String, required: true },
|
username: { type: String, required: false },
|
||||||
usernameType: { type: String, required: true, default: 'discord' },
|
usernameType: { type: String, required: true, default: 'discord' },
|
||||||
discord: {
|
discord: {
|
||||||
id: { type: String, required: true, unique: true },
|
id: { type: String, required: true, unique: true },
|
||||||
|
@ -46,3 +46,15 @@ export const getUser = async (userId: string, event: any) => {
|
|||||||
throw createError({ statusCode: 500, statusMessage: 'Failed to get user' })
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
5
webv2/types/global.d.ts
vendored
5
webv2/types/global.d.ts
vendored
@ -1,9 +1,12 @@
|
|||||||
|
import { Document } from 'mongoose'
|
||||||
|
|
||||||
export {}
|
export {}
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface IUser {
|
interface IUser extends Document {
|
||||||
_id: string,
|
_id: string,
|
||||||
username: string,
|
username: string,
|
||||||
|
usernameType: string,
|
||||||
discord: {
|
discord: {
|
||||||
id: string,
|
id: string,
|
||||||
username: string,
|
username: string,
|
||||||
|
Loading…
Reference in New Issue
Block a user