0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-18 02:21:47 -05:00

Stored price and currency data on Tiers when creating & editing

refs https://github.com/TryGhost/Team/issues/2029

This will allow us to start decoupling the Stripe side of things once we've got
the core data stored. We've also add some integrity checks on the incoming
monthly_price and yearly_price to ensure they are the same currency.
This commit is contained in:
Fabien "egg" O'Carroll 2022-10-14 11:52:51 +07:00
parent 8afc6777c0
commit 370ded5c77

View file

@ -1,4 +1,4 @@
const {UpdateCollisionError, NotFoundError, MethodNotAllowedError, ValidationError} = require('@tryghost/errors');
const {UpdateCollisionError, NotFoundError, MethodNotAllowedError, ValidationError, BadRequestError} = require('@tryghost/errors');
const tpl = require('@tryghost/tpl');
const messages = {
@ -173,6 +173,12 @@ class ProductRepository {
validatePrice(data.monthly_price);
}
if (data.yearly_price && data.monthly_price && data.yearly_price.currency !== data.monthly_price.currency) {
throw new BadRequestError({
message: 'The monthly and yearly price must use the same currency'
});
}
if (data.stripe_prices) {
data.stripe_prices.forEach(validatePrice);
}
@ -187,6 +193,16 @@ class ProductRepository {
welcome_page_url: data.welcome_page_url
};
if (data.monthly_price) {
productData.monthly_price = data.monthly_price.amount;
productData.currency = data.monthly_price.currency;
}
if (data.yearly_price) {
productData.yearly_price = data.yearly_price.amount;
productData.currency = data.yearly_price.currency;
}
if (Reflect.has(data, 'trial_days')) {
productData.trial_days = data.trial_days;
}
@ -335,6 +351,12 @@ class ProductRepository {
data.stripe_prices.forEach(validatePrice);
}
if (data.yearly_price && data.monthly_price && data.yearly_price.currency !== data.monthly_price.currency) {
throw new BadRequestError({
message: 'The monthly and yearly price must use the same currency'
});
}
const productId = data.id || options.id;
const existingProduct = await this._Product.findOne({id: productId}, options);
@ -347,6 +369,16 @@ class ProductRepository {
welcome_page_url: data.welcome_page_url
};
if (data.monthly_price) {
productData.monthly_price = data.monthly_price.amount;
productData.currency = data.monthly_price.currency;
}
if (data.yearly_price) {
productData.yearly_price = data.yearly_price.amount;
productData.currency = data.yearly_price.currency;
}
if (Reflect.has(data, 'active')) {
productData.active = data.active;
}