From dd4d6aeae5e34a85ef053217faddbc1f9195095e Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 26 Aug 2021 18:05:50 +0200 Subject: [PATCH] Wrapped product repo methods in transactions refs https://github.com/TryGhost/Team/issues/982 This ensures that we will not commit any rows to the database if something is to go wrong with a Stripe API request. --- ghost/members-api/lib/repositories/product.js | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/ghost/members-api/lib/repositories/product.js b/ghost/members-api/lib/repositories/product.js index 82d179b1b5..fc35b4ba6b 100644 --- a/ghost/members-api/lib/repositories/product.js +++ b/ghost/members-api/lib/repositories/product.js @@ -49,6 +49,14 @@ class ProductRepository { * @returns {Promise} */ async get(data, options) { + if (!options.transacting) { + return this._Product.transaction((transacting) => { + return this.get(data, { + ...options, + transacting + }); + }); + } if ('stripe_product_id' in data) { const stripeProduct = await this._StripeProduct.findOne({ stripe_product_id: data.stripe_product_id @@ -115,6 +123,15 @@ class ProductRepository { }); } + if (!options.transacting) { + return this._Product.transaction((transacting) => { + return this.create(data, { + ...options, + transacting + }); + }); + } + const productData = { name: data.name, description: data.description, @@ -241,6 +258,15 @@ class ProductRepository { }); } + if (!options.transacting) { + return this._Product.transaction((transacting) => { + return this.update(data, { + ...options, + transacting + }); + }); + } + const productData = { name: data.name, description: data.description, @@ -464,6 +490,14 @@ class ProductRepository { * @returns {Promise<{data: ProductModel[], meta: object}>} **/ async list(options) { + if (!options.transacting) { + return this._Product.transaction((transacting) => { + return this.list({ + ...options, + transacting + }); + }); + } return this._Product.findPage(options); }