2019-10-07 12:03:24 +07:00
|
|
|
const ghostBookshelf = require('./base');
|
2021-05-06 21:47:34 +05:30
|
|
|
const _ = require('lodash');
|
2019-10-07 12:03:24 +07:00
|
|
|
|
|
|
|
const StripeCustomerSubscription = ghostBookshelf.Model.extend({
|
2020-07-23 18:21:10 +02:00
|
|
|
tableName: 'members_stripe_customers_subscriptions',
|
|
|
|
|
|
|
|
customer() {
|
|
|
|
return this.belongsTo('MemberStripeCustomer', 'customer_id', 'customer_id');
|
2020-08-12 14:17:44 +01:00
|
|
|
},
|
|
|
|
|
2021-04-26 17:14:34 +01:00
|
|
|
stripePrice() {
|
|
|
|
return this.hasOne('StripePrice', 'stripe_price_id', 'stripe_price_id');
|
|
|
|
},
|
|
|
|
|
2020-08-12 14:17:44 +01:00
|
|
|
serialize(options) {
|
|
|
|
const defaultSerializedObject = ghostBookshelf.Model.prototype.serialize.call(this, options);
|
|
|
|
|
2021-04-26 17:14:34 +01:00
|
|
|
const serialized = {
|
2020-08-12 14:17:44 +01: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 09:49:07 +00:00
|
|
|
currency: String.prototype.toUpperCase.call(defaultSerializedObject.plan_currency)
|
2020-08-12 14:17:44 +01: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-23 21:19:27 +00:00
|
|
|
cancellation_reason: defaultSerializedObject.cancellation_reason,
|
2020-08-12 14:17:44 +01:00
|
|
|
current_period_end: defaultSerializedObject.current_period_end
|
|
|
|
};
|
2021-04-26 17:14:34 +01:00
|
|
|
|
2021-05-06 21:47:34 +05:30
|
|
|
if (!_.isEmpty(defaultSerializedObject.stripePrice)) {
|
2021-04-26 17:14:34 +01:00
|
|
|
serialized.price = {
|
2021-05-07 15:13:23 +05:30
|
|
|
id: defaultSerializedObject.stripePrice.stripe_price_id,
|
|
|
|
price_id: defaultSerializedObject.stripePrice.id,
|
2021-04-26 17:14:34 +01:00
|
|
|
nickname: defaultSerializedObject.stripePrice.nickname,
|
|
|
|
amount: defaultSerializedObject.stripePrice.amount,
|
|
|
|
interval: defaultSerializedObject.stripePrice.interval,
|
2021-05-04 21:02:20 +05:30
|
|
|
type: defaultSerializedObject.stripePrice.type,
|
2021-04-26 17:14:34 +01:00
|
|
|
currency: String.prototype.toUpperCase.call(defaultSerializedObject.stripePrice.currency)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (defaultSerializedObject.stripePrice.stripeProduct) {
|
2021-05-04 21:02:20 +05:30
|
|
|
const productData = defaultSerializedObject.stripePrice.stripeProduct.product || {};
|
2021-04-26 17:14:34 +01:00
|
|
|
serialized.price.product = {
|
|
|
|
id: defaultSerializedObject.stripePrice.stripeProduct.stripe_product_id,
|
2021-05-04 21:02:20 +05:30
|
|
|
name: productData.name,
|
2021-04-26 17:14:34 +01:00
|
|
|
product_id: defaultSerializedObject.stripePrice.stripeProduct.product_id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return serialized;
|
2020-07-23 18:21:10 +02:00
|
|
|
}
|
2020-08-12 14:17:44 +01:00
|
|
|
|
2019-10-08 13:10:20 +07: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 12:03:24 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
StripeCustomerSubscription: ghostBookshelf.model('StripeCustomerSubscription', StripeCustomerSubscription)
|
|
|
|
};
|