From 139cf1d3c96b607b775edf02c397ac2c30637460 Mon Sep 17 00:00:00 2001 From: hatim boufnichel Date: Tue, 9 Apr 2024 22:37:13 +0200 Subject: [PATCH] db tx --- src/services/main/applicationManager.ts | 47 +++++++++++-------------- src/services/serverMethods/index.ts | 8 ++--- 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/services/main/applicationManager.ts b/src/services/main/applicationManager.ts index ddcd68ef..0d8d6621 100644 --- a/src/services/main/applicationManager.ts +++ b/src/services/main/applicationManager.ts @@ -192,7 +192,7 @@ export default class { const user = await this.storage.applicationStorage.GetApplicationUser(app, req.user_identifier) return this.paymentManager.GetLnurlPayInfoFromUser(user.user.user_id, app, req.base_url_override) } - async RequestNsecLinkingToken(appId: string, req: Types.RequestNPubLinkingTokenRequest): Promise { + async RequestNPubLinkingToken(appId: string, req: Types.RequestNPubLinkingTokenRequest): Promise { const app = await this.storage.applicationStorage.GetApplication(appId); const user = await this.storage.applicationStorage.GetApplicationUser(app, req.user_identifier); if (Array.from(this.nPubLinkingTokens.values()).find(t => t.serialId === user.serial_id)) { @@ -208,32 +208,27 @@ export default class { - async LinkNpubThroughToken(appId: string, appUserId: string, req: Types.LinkNPubThroughTokenRequest): Promise { + async LinkNpubThroughToken(ctx: Types.UserContext, req: Types.LinkNPubThroughTokenRequest): Promise { + const { app_id: appId, app_user_id: appUserId } = ctx const app = await this.storage.applicationStorage.GetApplication(appId) const appUser = await this.storage.applicationStorage.GetApplicationUser(app, appUserId) - - /* - needs to be a db tx, otherwise gets foreign constraint error when deleting baseUser after - applicationUser, and unique constatin when giving targetted applicationUser the npub - */ - this.storage.DB.transaction(async tx => { - await this.storage.applicationStorage.RemoveApplicationUserAndBaseUser(appUser, tx); - - const entry = this.nPubLinkingTokens.get(req.token) - if (entry && entry.expiry > Date.now()) { - const copy = { ...entry } - const deleted = this.nPubLinkingTokens.delete(req.token) - if (deleted) { - await this.storage.applicationStorage.AddNPubToApplicationUser(copy.serialId, req.nostr_pub, tx) - } else { - throw new Error("An uknown error occured") - } - } else { - throw new Error("Token invalid or expired") - } - }) - + this.storage.txQueue.PushToQueue({ + dbTx: true, + exec: async tx => { + await this.storage.applicationStorage.RemoveApplicationUserAndBaseUser(appUser, tx); + const entry = this.nPubLinkingTokens.get(req.token) + if (entry && entry.expiry > Date.now()) { + const copy = { ...entry } + const deleted = this.nPubLinkingTokens.delete(req.token) + if (deleted) { + await this.storage.applicationStorage.AddNPubToApplicationUser(copy.serialId, req.nostr_pub, tx) + } else { + throw new Error("An uknown error occured") + } + } else { + throw new Error("Token invalid or expired") + } + } + }) } - - } \ No newline at end of file diff --git a/src/services/serverMethods/index.ts b/src/services/serverMethods/index.ts index 90d18362..f51d9296 100644 --- a/src/services/serverMethods/index.ts +++ b/src/services/serverMethods/index.ts @@ -205,7 +205,7 @@ export default (mainHandler: Main): Types.ServerMethods => { user_identifier_CustomCheck: userIdentifier => userIdentifier !== '', }) if (err != null) throw new Error(err.message) - return mainHandler.applicationManager.RequestNsecLinkingToken(ctx.app_id, req) + return mainHandler.applicationManager.RequestNPubLinkingToken(ctx.app_id, req) }, LinkNPubThroughToken: async ({ ctx, req }) => { const err = Types.LinkNPubThroughTokenRequestValidate(req, { @@ -213,11 +213,7 @@ export default (mainHandler: Main): Types.ServerMethods => { token_CustomCheck: token => token !== '' }) if (err != null) throw new Error(err.message) - - - - return mainHandler.applicationManager.LinkNpubThroughToken(ctx.app_id, ctx.app_user_id, req) - + return mainHandler.applicationManager.LinkNpubThroughToken(ctx, req) } } } \ No newline at end of file