2019-10-07 08:03:24 +03:00
|
|
|
const ghostBookshelf = require('./base');
|
2021-05-06 19:17:34 +03:00
|
|
|
const _ = require('lodash');
|
2019-10-07 08:03:24 +03:00
|
|
|
|
|
|
|
const StripeCustomerSubscription = ghostBookshelf.Model.extend({
|
2020-07-23 19:21:10 +03:00
|
|
|
tableName: 'members_stripe_customers_subscriptions',
|
|
|
|
|
|
|
|
customer() {
|
|
|
|
return this.belongsTo('MemberStripeCustomer', 'customer_id', 'customer_id');
|
2020-08-12 16:17:44 +03:00
|
|
|
},
|
|
|
|
|
2021-04-26 19:14:34 +03:00
|
|
|
stripePrice() {
|
|
|
|
return this.hasOne('StripePrice', 'stripe_price_id', 'stripe_price_id');
|
|
|
|
},
|
|
|
|
|
2020-08-12 16:17:44 +03:00
|
|
|
serialize(options) {
|
|
|
|
const defaultSerializedObject = ghostBookshelf.Model.prototype.serialize.call(this, options);
|
|
|
|
|
2021-04-26 19:14:34 +03:00
|
|
|
const serialized = {
|
2020-08-12 16:17:44 +03:00
|
|
|
id: defaultSerializedObject.subscription_id,
|
|
|
|
customer: {
|
|
|
|
id: defaultSerializedObject.customer_id,
|
|
|
|
// TODO? The customer is not fetched by default so these sometimes won't exist
|
|
|
|
name: defaultSerializedObject.customer ? defaultSerializedObject.customer.name : null,
|
|
|
|
email: defaultSerializedObject.customer ? defaultSerializedObject.customer.email : null
|
|
|
|
},
|
|
|
|
plan: {
|
|
|
|
id: defaultSerializedObject.plan_id,
|
|
|
|
nickname: defaultSerializedObject.plan_nickname,
|
|
|
|
amount: defaultSerializedObject.plan_amount,
|
|
|
|
interval: defaultSerializedObject.plan_interval,
|
2021-02-25 12:49:07 +03:00
|
|
|
currency: String.prototype.toUpperCase.call(defaultSerializedObject.plan_currency)
|
2020-08-12 16:17:44 +03:00
|
|
|
},
|
|
|
|
status: defaultSerializedObject.status,
|
|
|
|
start_date: defaultSerializedObject.start_date,
|
|
|
|
default_payment_card_last4: defaultSerializedObject.default_payment_card_last4,
|
|
|
|
cancel_at_period_end: defaultSerializedObject.cancel_at_period_end,
|
2020-11-24 00:19:27 +03:00
|
|
|
cancellation_reason: defaultSerializedObject.cancellation_reason,
|
2020-08-12 16:17:44 +03:00
|
|
|
current_period_end: defaultSerializedObject.current_period_end
|
|
|
|
};
|
2021-04-26 19:14:34 +03:00
|
|
|
|
2021-05-06 19:17:34 +03:00
|
|
|
if (!_.isEmpty(defaultSerializedObject.stripePrice)) {
|
2021-04-26 19:14:34 +03:00
|
|
|
serialized.price = {
|
2021-05-07 12:43:23 +03:00
|
|
|
id: defaultSerializedObject.stripePrice.stripe_price_id,
|
|
|
|
price_id: defaultSerializedObject.stripePrice.id,
|
2021-04-26 19:14:34 +03:00
|
|
|
nickname: defaultSerializedObject.stripePrice.nickname,
|
|
|
|
amount: defaultSerializedObject.stripePrice.amount,
|
|
|
|
interval: defaultSerializedObject.stripePrice.interval,
|
2021-05-04 18:32:20 +03:00
|
|
|
type: defaultSerializedObject.stripePrice.type,
|
2021-04-26 19:14:34 +03:00
|
|
|
currency: String.prototype.toUpperCase.call(defaultSerializedObject.stripePrice.currency)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (defaultSerializedObject.stripePrice.stripeProduct) {
|
2021-05-04 18:32:20 +03:00
|
|
|
const productData = defaultSerializedObject.stripePrice.stripeProduct.product || {};
|
2021-04-26 19:14:34 +03:00
|
|
|
serialized.price.product = {
|
|
|
|
id: defaultSerializedObject.stripePrice.stripeProduct.stripe_product_id,
|
2021-05-04 18:32:20 +03:00
|
|
|
name: productData.name,
|
2021-04-26 19:14:34 +03:00
|
|
|
product_id: defaultSerializedObject.stripePrice.stripeProduct.product_id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return serialized;
|
2020-07-23 19:21:10 +03:00
|
|
|
}
|
2020-08-12 16:17:44 +03:00
|
|
|
|
2019-10-08 09:10:20 +03:00
|
|
|
}, {
|
|
|
|
async upsert(data, unfilteredOptions) {
|
|
|
|
const subscriptionId = unfilteredOptions.subscription_id;
|
|
|
|
const model = await this.findOne({subscription_id: subscriptionId}, unfilteredOptions);
|
|
|
|
if (model) {
|
|
|
|
return this.edit(data, Object.assign({}, unfilteredOptions, {
|
|
|
|
id: model.id
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return this.add(data, unfilteredOptions);
|
|
|
|
}
|
2019-10-07 08:03:24 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
StripeCustomerSubscription: ghostBookshelf.model('StripeCustomerSubscription', StripeCustomerSubscription)
|
|
|
|
};
|