2019-10-07 08:03:24 +03:00
|
|
|
const ghostBookshelf = require('./base');
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (defaultSerializedObject.stripePrice) {
|
|
|
|
serialized.price = {
|
|
|
|
id: defaultSerializedObject.stripePrice.stripe_price_id,
|
|
|
|
nickname: defaultSerializedObject.stripePrice.nickname,
|
|
|
|
amount: defaultSerializedObject.stripePrice.amount,
|
|
|
|
interval: defaultSerializedObject.stripePrice.interval,
|
|
|
|
currency: String.prototype.toUpperCase.call(defaultSerializedObject.stripePrice.currency)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (defaultSerializedObject.stripePrice.stripeProduct) {
|
|
|
|
serialized.price.product = {
|
|
|
|
id: defaultSerializedObject.stripePrice.stripeProduct.stripe_product_id,
|
|
|
|
name: defaultSerializedObject.stripePrice.stripeProduct.name,
|
|
|
|
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)
|
|
|
|
};
|