fix: Migrated from transactions to writes, closes #30
This commit is contained in:
parent
3eb4453d71
commit
ea7a3e3a8f
22
webv2/server/api/users/[id]/minecraft/index.delete.ts
Normal file
22
webv2/server/api/users/[id]/minecraft/index.delete.ts
Normal 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 ''
|
||||
})
|
@ -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)
|
||||
|
@ -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
|
@ -19,7 +19,7 @@ const userSchema = new Schema({
|
||||
teamInvites: [
|
||||
Types.ObjectId
|
||||
]
|
||||
})
|
||||
}, { })
|
||||
|
||||
const whitelistSchema = new Schema({
|
||||
uuid: { type: String, required: true, unique: true },
|
||||
|
Loading…
Reference in New Issue
Block a user