0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

🐛 Fixed Member model removing labels when unset

closes https://github.com/TryGhost/Ghost/issues/12600

The bookshelf-relations plugin which we use will **remove** all
relations when they are set to an empty array, but will leave them alone
if it's set to undefined.

Our logic to deduplicate uppercase & lowercase version of the same label
was in advertently always setting the labels to an array, but when the
model was saved without passing the labels, this array would be empty.

Here we've added a check which will skip all label handling, if there
are no labels set.
This commit is contained in:
Fabien O'Carroll 2021-02-05 12:01:21 +00:00 committed by Daniel Lockyer
parent 59c51f3862
commit 33910db1ec
No known key found for this signature in database
GPG key ID: FFBC6FA2A6F6ABC1
2 changed files with 6 additions and 1 deletions

View file

@ -185,7 +185,7 @@ module.exports = {
permissions: true,
async query(frame) {
try {
frame.options.withRelated = ['stripeSubscriptions'];
frame.options.withRelated = ['stripeSubscriptions', 'labels'];
const member = await membersService.api.members.update(frame.data.members[0], frame.options);
const hasCompedSubscription = !!member.related('stripeSubscriptions').find(sub => sub.get('plan_nickname') === 'Complimentary' && sub.get('status') === 'active');

View file

@ -109,6 +109,11 @@ const Member = ghostBookshelf.Model.extend({
let labelsToSave = [];
let ops = [];
if (_.isUndefined(this.get('labels'))) {
this.unset('labels');
return;
}
// CASE: detect lowercase/uppercase label slugs
if (!_.isUndefined(this.get('labels')) && !_.isNull(this.get('labels'))) {
labelsToSave = [];