attempt to write resolver functions

This commit is contained in:
Satindar Dhillon 2022-06-22 20:00:37 -07:00
parent 02411347a7
commit da76ae7aab
2 changed files with 50 additions and 0 deletions

View file

@ -327,6 +327,17 @@ class UserModel extends DataModel<UserData, CreateSet, UpdateSet> {
}
return this.kx.transaction((tx) => this.updateProfile(userId, set, tx))
}
@logMethod
deleteUser(userId: string, tx: Knex.Transaction): boolean {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result = super.delete(userId, tx) as any
if (result.error) {
return false
} else {
return true
}
}
}
export default UserModel

View file

@ -1,4 +1,8 @@
import {
DeleteAccountSuccess,
DeleteAccountError,
DeleteAccountErrorCode,
MutationDeleteAccountArgs,
GoogleSignupResult,
LoginErrorCode,
LoginResult,
@ -355,3 +359,38 @@ export const signupResolver: ResolverFn<
return { errorCodes: [SignupErrorCode.Unknown] }
}
}
export const deleteAccountResolver = authorized<
DeleteAccountSuccess,
DeleteAccountError,
MutationDeleteAccountArgs
>(async (_, { userID }, { models, claims, log }) => {
const user = await models.user.get(userID)
if (!user || user.id !== claims.uid) {
return {
errorCodes: [DeleteAccountErrorCode.Unauthorized],
}
}
const deleteUserResult = await authTrx((tx) =>
models.user.deleteUser(userID as string, tx)
)
if (!deleteUserResult) {
return {
errorCodes: [DeleteAccountErrorCode.Forbidden],
}
}
log.info('Deleting a user account', {
userID,
labels: {
source: 'resolver',
resolver: 'deleteAccountResolver',
uid: claims.uid,
},
})
return { userID }
})