Merge pull request #670 from shocknet/clean-linking

db tx
This commit is contained in:
boufni95 2024-04-09 22:37:48 +02:00 committed by GitHub
commit d0470abf1a
2 changed files with 23 additions and 32 deletions

View file

@ -192,7 +192,7 @@ export default class {
const user = await this.storage.applicationStorage.GetApplicationUser(app, req.user_identifier) const user = await this.storage.applicationStorage.GetApplicationUser(app, req.user_identifier)
return this.paymentManager.GetLnurlPayInfoFromUser(user.user.user_id, app, req.base_url_override) return this.paymentManager.GetLnurlPayInfoFromUser(user.user.user_id, app, req.base_url_override)
} }
async RequestNsecLinkingToken(appId: string, req: Types.RequestNPubLinkingTokenRequest): Promise<Types.RequestNPubLinkingTokenResponse> { async RequestNPubLinkingToken(appId: string, req: Types.RequestNPubLinkingTokenRequest): Promise<Types.RequestNPubLinkingTokenResponse> {
const app = await this.storage.applicationStorage.GetApplication(appId); const app = await this.storage.applicationStorage.GetApplication(appId);
const user = await this.storage.applicationStorage.GetApplicationUser(app, req.user_identifier); const user = await this.storage.applicationStorage.GetApplicationUser(app, req.user_identifier);
if (Array.from(this.nPubLinkingTokens.values()).find(t => t.serialId === user.serial_id)) { if (Array.from(this.nPubLinkingTokens.values()).find(t => t.serialId === user.serial_id)) {
@ -208,17 +208,14 @@ export default class {
async LinkNpubThroughToken(appId: string, appUserId: string, req: Types.LinkNPubThroughTokenRequest): Promise<void> { async LinkNpubThroughToken(ctx: Types.UserContext, req: Types.LinkNPubThroughTokenRequest): Promise<void> {
const { app_id: appId, app_user_id: appUserId } = ctx
const app = await this.storage.applicationStorage.GetApplication(appId) const app = await this.storage.applicationStorage.GetApplication(appId)
const appUser = await this.storage.applicationStorage.GetApplicationUser(app, appUserId) const appUser = await this.storage.applicationStorage.GetApplicationUser(app, appUserId)
this.storage.txQueue.PushToQueue({
/* dbTx: true,
needs to be a db tx, otherwise gets foreign constraint error when deleting baseUser after exec: async tx => {
applicationUser, and unique constatin when giving targetted applicationUser the npub
*/
this.storage.DB.transaction(async tx => {
await this.storage.applicationStorage.RemoveApplicationUserAndBaseUser(appUser, tx); await this.storage.applicationStorage.RemoveApplicationUserAndBaseUser(appUser, tx);
const entry = this.nPubLinkingTokens.get(req.token) const entry = this.nPubLinkingTokens.get(req.token)
if (entry && entry.expiry > Date.now()) { if (entry && entry.expiry > Date.now()) {
const copy = { ...entry } const copy = { ...entry }
@ -231,9 +228,7 @@ export default class {
} else { } else {
throw new Error("Token invalid or expired") throw new Error("Token invalid or expired")
} }
})
} }
})
}
} }

View file

@ -205,7 +205,7 @@ export default (mainHandler: Main): Types.ServerMethods => {
user_identifier_CustomCheck: userIdentifier => userIdentifier !== '', user_identifier_CustomCheck: userIdentifier => userIdentifier !== '',
}) })
if (err != null) throw new Error(err.message) 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 }) => { LinkNPubThroughToken: async ({ ctx, req }) => {
const err = Types.LinkNPubThroughTokenRequestValidate(req, { const err = Types.LinkNPubThroughTokenRequestValidate(req, {
@ -213,11 +213,7 @@ export default (mainHandler: Main): Types.ServerMethods => {
token_CustomCheck: token => token !== '' token_CustomCheck: token => token !== ''
}) })
if (err != null) throw new Error(err.message) if (err != null) throw new Error(err.message)
return mainHandler.applicationManager.LinkNpubThroughToken(ctx, req)
return mainHandler.applicationManager.LinkNpubThroughToken(ctx.app_id, ctx.app_user_id, req)
} }
} }
} }