From ea7a3e3a8f6b5132dee66d23caef7e141c242bed Mon Sep 17 00:00:00 2001 From: Xeovalyte Date: Tue, 6 Jun 2023 10:37:30 +0200 Subject: [PATCH] fix: Migrated from transactions to writes, closes #30 --- .../api/users/[id]/minecraft/index.delete.ts | 22 +++++++++++++++++++ .../index.get.ts} | 4 ++-- .../index.put.ts} | 22 +++++++------------ webv2/server/utils/models.ts | 2 +- 4 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 webv2/server/api/users/[id]/minecraft/index.delete.ts rename webv2/server/api/users/[id]/{minecraft.get.ts => minecraft/index.get.ts} (89%) rename webv2/server/api/users/[id]/{minecraft.put.ts => minecraft/index.put.ts} (77%) diff --git a/webv2/server/api/users/[id]/minecraft/index.delete.ts b/webv2/server/api/users/[id]/minecraft/index.delete.ts new file mode 100644 index 0000000..a10c0b8 --- /dev/null +++ b/webv2/server/api/users/[id]/minecraft/index.delete.ts @@ -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 '' +}) diff --git a/webv2/server/api/users/[id]/minecraft.get.ts b/webv2/server/api/users/[id]/minecraft/index.get.ts similarity index 89% rename from webv2/server/api/users/[id]/minecraft.get.ts rename to webv2/server/api/users/[id]/minecraft/index.get.ts index 98f4528..253b15b 100644 --- a/webv2/server/api/users/[id]/minecraft.get.ts +++ b/webv2/server/api/users/[id]/minecraft/index.get.ts @@ -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) diff --git a/webv2/server/api/users/[id]/minecraft.put.ts b/webv2/server/api/users/[id]/minecraft/index.put.ts similarity index 77% rename from webv2/server/api/users/[id]/minecraft.put.ts rename to webv2/server/api/users/[id]/minecraft/index.put.ts index 0c72968..1047ae8 100644 --- a/webv2/server/api/users/[id]/minecraft.put.ts +++ b/webv2/server/api/users/[id]/minecraft/index.put.ts @@ -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 diff --git a/webv2/server/utils/models.ts b/webv2/server/utils/models.ts index d64ae4e..636f854 100644 --- a/webv2/server/utils/models.ts +++ b/webv2/server/utils/models.ts @@ -19,7 +19,7 @@ const userSchema = new Schema({ teamInvites: [ Types.ObjectId ] -}) +}, { }) const whitelistSchema = new Schema({ uuid: { type: String, required: true, unique: true },