Refactored Members controller bulkDestroy (#13221)

no-issue

This moves the logic out of the controller and into the members-api
member repository. Removing complexity from the controllers and
out into services is desirable to reduce code in the Ghost codebase
and move logic into modules which can be tested easier.
This commit is contained in:
Fabien 'egg' O'Carroll 2021-08-12 15:26:25 +01:00 committed by GitHub
parent cf14b5f433
commit 235597a5e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 37 deletions

View File

@ -379,36 +379,7 @@ module.exports = {
method: 'destroy' method: 'destroy'
}, },
async query(frame) { async query(frame) {
const {all, filter, search} = frame.options; const bulkDestroyResult = await membersService.api.members.bulkDestroy(frame.options);
if (!filter && !search && (!all || all !== true)) {
throw new errors.IncorrectUsageError({
message: 'DELETE /members/ must be used with a filter or ?all=true'
});
}
const knexOptions = _.pick(frame.options, ['transacting']);
const filterOptions = Object.assign({}, knexOptions);
if (all !== true) {
if (filter) {
filterOptions.filter = filter;
}
if (search) {
filterOptions.search = search;
}
}
// fetch ids of all matching members
const memberRows = await models.Member
.getFilteredCollectionQuery(filterOptions)
.select('members.id')
.distinct();
const memberIds = memberRows.map(row => row.id);
const bulkDestroyResult = await models.Member.bulkDestroy(memberIds);
// shaped to match the importer response // shaped to match the importer response
return { return {

View File

@ -72,7 +72,7 @@
"@tryghost/limit-service": "0.6.1", "@tryghost/limit-service": "0.6.1",
"@tryghost/logging": "0.1.5", "@tryghost/logging": "0.1.5",
"@tryghost/magic-link": "1.0.7", "@tryghost/magic-link": "1.0.7",
"@tryghost/members-api": "1.23.1", "@tryghost/members-api": "1.24.0",
"@tryghost/members-csv": "1.1.2", "@tryghost/members-csv": "1.1.2",
"@tryghost/members-importer": "0.1.2", "@tryghost/members-importer": "0.1.2",
"@tryghost/members-ssr": "1.0.8", "@tryghost/members-ssr": "1.0.8",

View File

@ -898,7 +898,7 @@
jsonwebtoken "^8.5.1" jsonwebtoken "^8.5.1"
lodash "^4.17.15" lodash "^4.17.15"
"@tryghost/magic-link@^1.0.7": "@tryghost/magic-link@^1.0.8":
version "1.0.8" version "1.0.8"
resolved "https://registry.yarnpkg.com/@tryghost/magic-link/-/magic-link-1.0.8.tgz#a5ef3e77562715b11537d73af4f5d3d56b1cbb41" resolved "https://registry.yarnpkg.com/@tryghost/magic-link/-/magic-link-1.0.8.tgz#a5ef3e77562715b11537d73af4f5d3d56b1cbb41"
integrity sha512-TKRnCTbRbhkkCMlwE55nMruZRiH82byLlNlQ4Uw9MzQAMJn2m5M9LDshtX/eUbOHvSESBkMF5G2UdcgvsG19Rw== integrity sha512-TKRnCTbRbhkkCMlwE55nMruZRiH82byLlNlQ4Uw9MzQAMJn2m5M9LDshtX/eUbOHvSESBkMF5G2UdcgvsG19Rw==
@ -907,15 +907,15 @@
jsonwebtoken "^8.5.1" jsonwebtoken "^8.5.1"
lodash "^4.17.15" lodash "^4.17.15"
"@tryghost/members-api@1.23.1": "@tryghost/members-api@1.24.0":
version "1.23.1" version "1.24.0"
resolved "https://registry.yarnpkg.com/@tryghost/members-api/-/members-api-1.23.1.tgz#2ea436d7ed3d99a1d55218b360b0f698fa064bb4" resolved "https://registry.yarnpkg.com/@tryghost/members-api/-/members-api-1.24.0.tgz#993a43332f342f9887ca1c933487cf3362a39972"
integrity sha512-uRapjVQBd9Rzb0yt8nLf2jGFcRBcOZFclLbf5H6NUgpC9Y3N2/uVSdtm9EXViq6bMEAlK+ewnTIp4LAQAwk5mw== integrity sha512-UTLuCNRCGZhMDG2YRgBHd0LGsjqDy09UUuzohbCxKVnsVTLBoYyCV/Pwqypwu4enf4fJvJxVWHVeqSv7sRNXkQ==
dependencies: dependencies:
"@tryghost/debug" "^0.1.2" "@tryghost/debug" "^0.1.2"
"@tryghost/errors" "^0.2.9" "@tryghost/errors" "^0.2.9"
"@tryghost/ignition-errors" "^0.1.2" "@tryghost/ignition-errors" "^0.1.2"
"@tryghost/magic-link" "^1.0.7" "@tryghost/magic-link" "^1.0.8"
"@tryghost/tpl" "^0.1.2" "@tryghost/tpl" "^0.1.2"
"@types/jsonwebtoken" "^8.5.1" "@types/jsonwebtoken" "^8.5.1"
bluebird "^3.5.4" bluebird "^3.5.4"