0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Added bulkDestroy method to Member Repository

no-issue

The logic for bulk destroy is currently incorrectly inside of the
members api controller in Ghost core. Moving it out to here allows us to
simplify the controller to rely on the service, rather than implement
the logic.
This commit is contained in:
Fabien O'Carroll 2021-08-12 13:45:01 +02:00
parent daa3bb405d
commit 6bb3407725

View file

@ -7,7 +7,8 @@ const messages = {
moreThanOneProduct: 'A member cannot have more than one Product',
existingSubscriptions: 'Cannot modify Products for a Member with active Subscriptions',
subscriptionNotFound: 'Could not find Subscription {id}',
productNotFound: 'Could not find Product {id}'
productNotFound: 'Could not find Product {id}',
bulkActionRequiresFilter: 'Cannot perform {action} without a filter or all=true'
};
module.exports = class MemberRepository {
@ -289,6 +290,40 @@ module.exports = class MemberRepository {
}, options);
}
async bulkDestroy(options) {
const {all, filter, search} = options;
if (!filter && !search && (!all || all !== true)) {
throw new errors.IncorrectUsageError({
message: tpl(messages.bulkActionRequiresFilter, {action: 'bulk delete'})
});
}
const filterOptions = {};
if (options.transacting) {
filterOptions.transacting = options.transacting;
}
if (all !== true) {
if (filter) {
filterOptions.filter = filter;
}
if (filter) {
filterOptions.search = search;
}
}
const memberRows = await this._Member.getFilteredCollectionQuery(filterOptions)
.select('members.id')
.distinct();
const memberIds = memberRows.map(row => row.id);
return this._Member.bulkDestroy(memberIds);
}
async upsertCustomer(data) {
return await this._StripeCustomer.upsert({
customer_id: data.customer_id,