From 0bea1588322be93faf085acf8a4918981b092af7 Mon Sep 17 00:00:00 2001 From: Thibaut Patel Date: Tue, 12 Jan 2021 16:02:34 +0000 Subject: [PATCH] Moved the service logic out of the users controller refs #12537 --- core/server/api/canary/users.js | 31 ++------------------------- core/server/services/users.js | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 29 deletions(-) create mode 100644 core/server/services/users.js diff --git a/core/server/api/canary/users.js b/core/server/api/canary/users.js index ba65451e63..f82e52d489 100644 --- a/core/server/api/canary/users.js +++ b/core/server/api/canary/users.js @@ -1,10 +1,9 @@ -const path = require('path'); const Promise = require('bluebird'); const {i18n} = require('../../lib/common'); const errors = require('@tryghost/errors'); -const dbBackup = require('../../data/db/backup'); const models = require('../../models'); const permissionsService = require('../../services/permissions'); +const {destroyUser} = require('../../services/users'); const ALLOWED_INCLUDES = ['count.posts', 'permissions', 'roles', 'roles.permissions']; const UNSAFE_ATTRS = ['status', 'roles']; @@ -148,33 +147,7 @@ module.exports = { }, permissions: true, async query(frame) { - const backupPath = await dbBackup.backup(); - const parsedFileName = path.parse(backupPath); - const filename = `${parsedFileName.name}${parsedFileName.ext}`; - - return models.Base.transaction((t) => { - frame.options.transacting = t; - - return models.Post.destroyByAuthor(frame.options) - .then(() => { - return models.ApiKey.destroy({ - ...frame.options, - require: true, - destroyBy: { - user_id: frame.options.id - } - }).catch((err) => { - if (err instanceof models.ApiKey.NotFoundError) { - return; //Do nothing here as it's ok - } - throw err; - }); - }) - .then(() => { - return models.User.destroy(Object.assign({status: 'all'}, frame.options)); - }) - .then(() => filename); - }).catch((err) => { + return destroyUser(frame.options).catch((err) => { return Promise.reject(new errors.NoPermissionError({ err: err })); diff --git a/core/server/services/users.js b/core/server/services/users.js new file mode 100644 index 0000000000..21be5e3993 --- /dev/null +++ b/core/server/services/users.js @@ -0,0 +1,37 @@ +const path = require('path'); +const dbBackup = require('../data/db/backup'); +const models = require('../models'); + +async function destroyUser(frameOptions) { + const backupPath = await dbBackup.backup(); + const parsedFileName = path.parse(backupPath); + const filename = `${parsedFileName.name}${parsedFileName.ext}`; + + return models.Base.transaction((t) => { + frameOptions.transacting = t; + + return models.Post.destroyByAuthor(frameOptions) + .then(() => { + return models.ApiKey.destroy({ + ...frameOptions, + require: true, + destroyBy: { + user_id: frameOptions.id + } + }).catch((err) => { + if (err instanceof models.ApiKey.NotFoundError) { + return; //Do nothing here as it's ok + } + throw err; + }); + }) + .then(() => { + return models.User.destroy(Object.assign({status: 'all'}, frameOptions)); + }) + .then(() => filename); + }); +} + +module.exports = { + destroyUser +};