diff --git a/core/server/models/index.js b/core/server/models/index.js index 4e724a38fe..1f98de87df 100644 --- a/core/server/models/index.js +++ b/core/server/models/index.js @@ -29,6 +29,8 @@ const models = [ 'mobiledoc-revision', 'member', 'product', + 'stripe-product', + 'stripe-price', 'member-subscribe-event', 'member-paid-subscription-event', 'member-login-event', diff --git a/core/server/models/stripe-price.js b/core/server/models/stripe-price.js new file mode 100644 index 0000000000..0350123ae8 --- /dev/null +++ b/core/server/models/stripe-price.js @@ -0,0 +1,18 @@ +const ghostBookshelf = require('./base'); + +const StripePrice = ghostBookshelf.Model.extend({ + tableName: 'stripe_prices', + + stripeProduct() { + return this.belongsTo('StripeProduct', 'stripe_product_id', 'stripe_product_id'); + } +}); + +const StripePrices = ghostBookshelf.Collection.extend({ + model: StripePrice +}); + +module.exports = { + StripePrice: ghostBookshelf.model('StripePrice', StripePrice), + StripePrices: ghostBookshelf.collection('StripePrices', StripePrices) +}; diff --git a/core/server/models/stripe-product.js b/core/server/models/stripe-product.js new file mode 100644 index 0000000000..aed7629a10 --- /dev/null +++ b/core/server/models/stripe-product.js @@ -0,0 +1,57 @@ +const ghostBookshelf = require('./base'); + +const StripeProduct = ghostBookshelf.Model.extend({ + tableName: 'stripe_products', + + product() { + return this.belongsTo('Product', 'product_id', 'id'); + } + +}, { + async upsert(data, unfilteredOptions = {}) { + const stripeProductId = data.stripe_product_id; + const model = await this.findOne({stripe_product_id: stripeProductId}, unfilteredOptions); + if (model) { + return this.edit(data, Object.assign({}, unfilteredOptions, { + id: model.id + })); + } + return this.add(data, unfilteredOptions); + }, + + add(data, unfilteredOptions = {}) { + if (!unfilteredOptions.transacting) { + return ghostBookshelf.transaction((transacting) => { + return this.add(data, Object.assign({transacting}, unfilteredOptions)); + }); + } + return ghostBookshelf.Model.add.call(this, data, unfilteredOptions); + }, + + edit(data, unfilteredOptions = {}) { + if (!unfilteredOptions.transacting) { + return ghostBookshelf.transaction((transacting) => { + return this.edit(data, Object.assign({transacting}, unfilteredOptions)); + }); + } + return ghostBookshelf.Model.edit.call(this, data, unfilteredOptions); + }, + + destroy(unfilteredOptions = {}) { + if (!unfilteredOptions.transacting) { + return ghostBookshelf.transaction((transacting) => { + return this.destroy(Object.assign({transacting}, unfilteredOptions)); + }); + } + return ghostBookshelf.Model.destroy.call(this, unfilteredOptions); + } +}); + +const StripeProducts = ghostBookshelf.Collection.extend({ + model: StripeProduct +}); + +module.exports = { + StripeProduct: ghostBookshelf.model('StripeProduct', StripeProduct), + StripeProducts: ghostBookshelf.collection('StripeProducts', StripeProducts) +};