Ghost/core/server/models/stripe-customer-subscription.js
Nazar Gargol 95044e3ba0 Added error handling for failed member imports
no issue

- When bulk insert fails there is no transactional logic to revert
related records form being inserted. Also, previously there were no
attempts to "retry" the insert.
- To avoid complex retry logic, an iterative one-by-one insert retry
approach was taken. If this becomes a bottleneck in the future, the
retry algorithm could be improved.
- To avoid a lot of code duplication refactored model's `bulkAdd` & `bulkDestroy`
methods to use 'bulk-operations' module.
- Updated error handling and logging for bulk delete operations. It's very
unlikely for error to happen here,  but still need to make sure there is
a proper logging in place to trace back the failure.
- Added debug logs. This should improve debugging experience and
performance measurements.
- Added handling for unrecognized errors. Handling inspired by current unrecognized
error handling by ghost importer -10e5d5f3d4/core/server/data/importer/importers/data/base.js (L148-L154)
2020-08-26 17:11:35 +12:00

62 lines
2.4 KiB
JavaScript

const ghostBookshelf = require('./base');
const CURRENCY_SYMBOLS = {
usd: '$',
aud: '$',
cad: '$',
gbp: '£',
eur: '€',
inr: '₹'
};
const StripeCustomerSubscription = ghostBookshelf.Model.extend({
tableName: 'members_stripe_customers_subscriptions',
customer() {
return this.belongsTo('MemberStripeCustomer', 'customer_id', 'customer_id');
},
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
};
}
}, {
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);
}
});
module.exports = {
StripeCustomerSubscription: ghostBookshelf.model('StripeCustomerSubscription', StripeCustomerSubscription)
};