diff --git a/src/entities/User/model.ts b/src/entities/User/model.ts index fca2a1f93..4529bd8cb 100644 --- a/src/entities/User/model.ts +++ b/src/entities/User/model.ts @@ -99,4 +99,29 @@ 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) { + let query + + switch (accountType) { + case AccountType.Forum: + query = SQL` + UPDATE ${table(this)} + SET forum_id = NULL, forum_verification_date = NULL + WHERE address = ${address.toLowerCase()} + ` + break + case AccountType.Discord: + query = SQL` + UPDATE ${table(this)} + SET discord_id = NULL, discord_verification_date = NULL, is_discord_notifications_active = NULL + WHERE address = ${address.toLowerCase()} + ` + break + default: + throw new Error(`Unlinking account type ${accountType} is not supported`) + } + + return await this.namedQuery('unlink_account', query) + } } 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) + } }