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.
This commit is contained in:
Fabien O'Carroll 2021-08-26 18:05:50 +02:00
parent dc79f04989
commit dd4d6aeae5

View File

@ -49,6 +49,14 @@ class ProductRepository {
* @returns {Promise<ProductModel>}
*/
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);
}