Skip to content

Commit

Permalink
chore: add unlink account endpoint, add verification dates to profile…
Browse files Browse the repository at this point in the history
… endpoint
  • Loading branch information
1emu committed Jul 16, 2024
1 parent 05a963c commit cd02352
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/entities/User/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,29 @@ 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) {
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)
}
}
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 cd02352

Please sign in to comment.