From fcd37afdb070b5c4685d3b81c4c9b4ea83267a06 Mon Sep 17 00:00:00 2001 From: NGPixel Date: Sun, 15 Mar 2020 12:06:45 -0400 Subject: [PATCH] fix: drop userKeys on user delete --- server/graph/resolvers/user.js | 2 +- server/models/users.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/server/graph/resolvers/user.js b/server/graph/resolvers/user.js index 47ce1bc7..865d701e 100644 --- a/server/graph/resolvers/user.js +++ b/server/graph/resolvers/user.js @@ -45,7 +45,7 @@ module.exports = { if (args.id <= 2) { throw new WIKI.Error.UserDeleteProtected() } - await WIKI.models.users.query().deleteById(args.id) + await WIKI.models.users.deleteUser(args.id) return { responseResult: graphHelper.generateSuccess('User deleted successfully') } diff --git a/server/models/users.js b/server/models/users.js index 426efd07..82de4496 100644 --- a/server/models/users.js +++ b/server/models/users.js @@ -596,6 +596,21 @@ module.exports = class User extends Model { } } + /** + * Delete a User + * + * @param {*} id User ID + */ + static async deleteUser (id) { + const usr = await WIKI.models.users.query().findById(id) + if (usr) { + await WIKI.models.userKeys.query().delete().where('userId', id) + await WIKI.models.users.query().deleteById(id) + } else { + throw new WIKI.Error.UserNotFound() + } + } + /** * Register a new user (client-side registration) *