diff --git a/core/server/models/member-stripe-customer.js b/core/server/models/member-stripe-customer.js index d5580fcb11..6a4807d0a6 100644 --- a/core/server/models/member-stripe-customer.js +++ b/core/server/models/member-stripe-customer.js @@ -2,6 +2,17 @@ const ghostBookshelf = require('./base'); const MemberStripeCustomer = ghostBookshelf.Model.extend({ tableName: 'members_stripe_customers' +}, { + async upsert(data, unfilteredOptions) { + const customerId = data.customer_id; + const model = await this.findOne({customer_id: customerId}, unfilteredOptions); + if (model) { + return this.edit(data, Object.assign({}, unfilteredOptions, { + id: model.id + })); + } + return this.add(data, unfilteredOptions); + } }); module.exports = { diff --git a/core/server/models/stripe-customer-subscription.js b/core/server/models/stripe-customer-subscription.js index 6ae199b2e2..5f9c51881d 100644 --- a/core/server/models/stripe-customer-subscription.js +++ b/core/server/models/stripe-customer-subscription.js @@ -2,6 +2,17 @@ const ghostBookshelf = require('./base'); const StripeCustomerSubscription = ghostBookshelf.Model.extend({ tableName: 'stripe_customers_subscriptions' +}, { + 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 = {