2019-09-26 08:07:52 +03:00
|
|
|
const ghostBookshelf = require('./base');
|
|
|
|
|
|
|
|
const MemberStripeCustomer = ghostBookshelf.Model.extend({
|
2020-07-23 19:21:10 +03:00
|
|
|
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');
|
|
|
|
}
|
2019-10-08 09:10:20 +03:00
|
|
|
}, {
|
|
|
|
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);
|
2020-07-23 19:21:10 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
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);
|
2019-10-08 09:10:20 +03:00
|
|
|
}
|
2019-09-26 08:07:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
MemberStripeCustomer: ghostBookshelf.model('MemberStripeCustomer', MemberStripeCustomer)
|
|
|
|
};
|