mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
5144a0e09c
no-issue Up until now we have left orphaned rows in members_stripe_* tables when a member is deleted, this updates the destroy method so that we cascade and remove any MemberStripeCustomer and StripeCustomerSubscription models related to the Member. This also adds regression tests for the new functionality as well as to confirm the existing functionality of cascading to the members_labels join table This adds the relations of Subscription->Customer & Customer->Member
61 lines
2 KiB
JavaScript
61 lines
2 KiB
JavaScript
const ghostBookshelf = require('./base');
|
|
|
|
const MemberStripeCustomer = ghostBookshelf.Model.extend({
|
|
tableName: 'members_stripe_customers',
|
|
|
|
relationships: ['subscriptions'],
|
|
|
|
relationshipBelongsTo: {
|
|
subscriptions: 'members_stripe_customers_subscriptions'
|
|
},
|
|
|
|
subscriptions() {
|
|
return this.hasMany('StripeCustomerSubscription', 'customer_id', 'customer_id');
|
|
},
|
|
|
|
member() {
|
|
return this.belongsTo('Member', 'member_id', 'id');
|
|
}
|
|
}, {
|
|
async upsert(data, unfilteredOptions) {
|
|
const customerId = data.customer_id;
|
|
const model = await this.findOne({customer_id: customerId}, unfilteredOptions);
|
|
if (model) {
|
|
return this.edit(data, Object.assign({}, unfilteredOptions, {
|
|
id: model.id
|
|
}));
|
|
}
|
|
return this.add(data, unfilteredOptions);
|
|
},
|
|
|
|
add(data, unfilteredOptions) {
|
|
if (!unfilteredOptions.transacting) {
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
return this.add(data, Object.assign({transacting}, unfilteredOptions));
|
|
});
|
|
}
|
|
return ghostBookshelf.Model.add.call(this, data, unfilteredOptions);
|
|
},
|
|
|
|
edit(data, unfilteredOptions) {
|
|
if (!unfilteredOptions.transacting) {
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
return this.edit(data, Object.assign({transacting}, unfilteredOptions));
|
|
});
|
|
}
|
|
return ghostBookshelf.Model.edit.call(this, data, unfilteredOptions);
|
|
},
|
|
|
|
destroy(unfilteredOptions) {
|
|
if (!unfilteredOptions.transacting) {
|
|
return ghostBookshelf.transaction((transacting) => {
|
|
return this.destroy(Object.assign({transacting}, unfilteredOptions));
|
|
});
|
|
}
|
|
return ghostBookshelf.Model.destroy.call(this, unfilteredOptions);
|
|
}
|
|
});
|
|
|
|
module.exports = {
|
|
MemberStripeCustomer: ghostBookshelf.model('MemberStripeCustomer', MemberStripeCustomer)
|
|
};
|