fix: Migrated from transactions to writes, closes #30
All checks were successful
Build and Deploy / Deploy Web (push) Successful in 1m2s
Build and Deploy / Deploy Discord Bot (push) Successful in 42s

This commit is contained in:
Xeovalyte 2023-06-06 10:37:30 +02:00
parent 3eb4453d71
commit ea7a3e3a8f
4 changed files with 33 additions and 17 deletions

View File

@ -0,0 +1,22 @@
export default defineEventHandler(async (event) => {
const userId: string = event.context.params ? event.context.params.id : '@me'
const user = await getUser(userId, event)
if (user.$isEmpty('minecraft') || !user.minecraft) {
throw createError({ statusCode: 400, statusMessage: 'Minecraft has not been linked' })
}
try {
await WhitelistModel.deleteOne({ uuid: user.minecraft.uuid })
user.minecraft = undefined
await user.save()
} catch (e) {
console.error('Failed to update documents', e)
throw createError('Failed to update documents')
}
return ''
})

View File

@ -3,7 +3,7 @@ export default defineEventHandler(async (event) => {
const user = await getUser(userId, event)
if (!user.minecraft) {
if (user.$isEmpty('minecraft') || !user.minecraft) {
throw createError({ statusCode: 400, statusMessage: 'Minecraft has not been linked' })
}
@ -12,7 +12,7 @@ export default defineEventHandler(async (event) => {
try {
user.minecraft = { uuid: user.minecraft.uuid, username: minecraftProfile.name }
user.save()
await user.save()
} catch (e) {
console.error('Failed to update document', e)

View File

@ -1,9 +1,13 @@
import mongoose from 'mongoose'
export default defineEventHandler(async (event) => {
const { code } = await readBody(event)
const userId: string = event.context.params ? event.context.params.id : '@me'
const user = await getUser(userId, event)
if (!user.$isEmpty('minecraft')) {
throw createError({ statusCode: 400, statusMessage: 'Minecraft already linked' })
}
let whitelistDoc
try {
whitelistDoc = await WhitelistModel.findOne({ code })
@ -18,27 +22,17 @@ export default defineEventHandler(async (event) => {
}
const minecraftProfile: any = await $fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${whitelistDoc.uuid}`)
const user = await getUser(userId, event)
const session = await mongoose.startSession()
session.startTransaction()
try {
whitelistDoc.connected = true
user.minecraft = { uuid: whitelistDoc.uuid, username: minecraftProfile.name }
whitelistDoc.save({ session })
user.save({ session })
await session.commitTransaction()
await whitelistDoc.save()
await user.save()
} catch (e) {
console.error('Failed to update documents', e)
await session.abortTransaction()
throw createError('Failed to update documents')
} finally {
session.endSession()
}
return user.minecraft

View File

@ -19,7 +19,7 @@ const userSchema = new Schema({
teamInvites: [
Types.ObjectId
]
})
}, { })
const whitelistSchema = new Schema({
uuid: { type: String, required: true, unique: true },