Skip to content

Commit

Permalink
feat: unlink accounts (#1878)
Browse files Browse the repository at this point in the history
* chore: add unlink account endpoint, add verification dates to profile endpoint

* refactor: unlink method
  • Loading branch information
1emu authored Jul 17, 2024
1 parent 05a963c commit 4d19830
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/entities/User/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,28 @@ export default class UserModel extends Model<UserAttributes> {
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`)
}
}
}
8 changes: 8 additions & 0 deletions src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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])
}
8 changes: 7 additions & 1 deletion src/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -233,4 +235,8 @@ export class UserService {
}
}
}

static async unlinkAccount(address: string, accountType: AccountType) {
return await UserModel.unlinkAccount(address, accountType)
}
}

0 comments on commit 4d19830

Please sign in to comment.