2019-10-07 08:03:24 +03:00
|
|
|
const ghostBookshelf = require('./base');
|
|
|
|
|
2020-08-12 16:17:44 +03:00
|
|
|
const CURRENCY_SYMBOLS = {
|
|
|
|
usd: '$',
|
|
|
|
aud: '$',
|
|
|
|
cad: '$',
|
|
|
|
gbp: '£',
|
|
|
|
eur: '€',
|
|
|
|
inr: '₹'
|
|
|
|
};
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
serialize(options) {
|
|
|
|
const defaultSerializedObject = ghostBookshelf.Model.prototype.serialize.call(this, options);
|
|
|
|
|
|
|
|
return {
|
|
|
|
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,
|
|
|
|
currency: String.prototype.toUpperCase.call(defaultSerializedObject.plan_currency),
|
|
|
|
currency_symbol: CURRENCY_SYMBOLS[String.prototype.toLowerCase.call(defaultSerializedObject.plan_currency)]
|
|
|
|
},
|
|
|
|
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,
|
|
|
|
current_period_end: defaultSerializedObject.current_period_end
|
|
|
|
};
|
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)
|
|
|
|
};
|