From 4d19830a64ad3ac8ac7c47f6d84ba25aea922cc8 Mon Sep 17 00:00:00 2001 From: lemu Date: Wed, 17 Jul 2024 11:34:59 -0300 Subject: [PATCH] feat: unlink accounts (#1878) * chore: add unlink account endpoint, add verification dates to profile endpoint * refactor: unlink method --- src/entities/User/model.ts | 24 ++++++++++++++++++++++++ src/routes/user.ts | 8 ++++++++ src/services/user.ts | 8 +++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/entities/User/model.ts b/src/entities/User/model.ts index fca2a1f93..e20a2ee77 100644 --- a/src/entities/User/model.ts +++ b/src/entities/User/model.ts @@ -99,4 +99,28 @@ export default class UserModel extends Model { const result = await this.namedQuery('get_user_validated', query) return result[0]?.total_no_null_columns === String(accounts.size) } + + static async unlinkAccount(address: string, accountType: AccountType) { + const query = UserModel.getUnlinkQuery(address, accountType) + return await this.namedQuery('unlink_account', query) + } + + private static getUnlinkQuery(address: string, accountType: AccountType) { + switch (accountType) { + case AccountType.Forum: + return SQL` + UPDATE ${table(this)} + SET forum_id = NULL, forum_verification_date = NULL + WHERE address = ${address.toLowerCase()} + ` + case AccountType.Discord: + return SQL` + UPDATE ${table(this)} + SET discord_id = NULL, discord_verification_date = NULL, is_discord_notifications_active = NULL + WHERE address = ${address.toLowerCase()} + ` + default: + throw new Error(`Unlinking account type ${accountType} is not supported`) + } + } } diff --git a/src/routes/user.ts b/src/routes/user.ts index 410e01ae3..1b2e897cb 100644 --- a/src/routes/user.ts +++ b/src/routes/user.ts @@ -17,6 +17,7 @@ export default routes((route) => { route.get('/user/discord-linked', withAuth, handleAPI(isDiscordLinked)) route.get('/user/:address/is-validated', handleAPI(isValidated)) route.get('/user/:address', handleAPI(getProfile)) + route.post('/user/unlink', withAuth, handleAPI(unlinkAccount)) }) async function getValidationMessage(req: WithAuth) { @@ -65,3 +66,10 @@ async function getProfile(req: Request) { const address = validateAddress(req.params.address) return await UserService.getProfile(address) } + +async function unlinkAccount(req: WithAuth) { + const address = req.auth! + const { accountType } = req.body + const accounts = validateAccountTypes(accountType) + return await UserService.unlinkAccount(address, accounts[0]) +} diff --git a/src/services/user.ts b/src/services/user.ts index 2e26db4cb..14d452b8c 100644 --- a/src/services/user.ts +++ b/src/services/user.ts @@ -219,11 +219,13 @@ export class UserService { const emptyProfile: UserAttributes = { address } return emptyProfile } - const { forum_id } = user + const { forum_id, forum_verification_date, discord_verification_date } = user return { forum_id, forum_username: forum_id ? (await DiscourseService.getUserById(forum_id))?.username : null, + forum_verification_date, + discord_verification_date, } } catch (error: unknown) { if (error instanceof Error) { @@ -233,4 +235,8 @@ export class UserService { } } } + + static async unlinkAccount(address: string, accountType: AccountType) { + return await UserModel.unlinkAccount(address, accountType) + } }