From 3366bd1254700551a308421addee5166956ffdc6 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Tue, 8 Oct 2019 13:10:20 +0700 Subject: [PATCH] Added upsert method to stripe models no-issue This is kind of copied from the session model, but simplified This will allow much easier integration with members-api --- core/server/models/member-stripe-customer.js | 11 +++++++++++ core/server/models/stripe-customer-subscription.js | 11 +++++++++++ 2 files changed, 22 insertions(+) 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 = {