Improved price data access and setting for Tier

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

We want to be able to easily get the price for a Tier based on the cadence for
use when creating Stripe Prices. We also want to be able to set the pricing data
to `null` for free Tiers, rather than erroring.
This commit is contained in:
Fabien "egg" O'Carroll 2022-10-21 15:03:31 +07:00
parent 68f1df545c
commit 253c30c37a

View File

@ -115,6 +115,21 @@ module.exports = class Tier {
this.#currency = validateCurrency(value, this.#type); this.#currency = validateCurrency(value, this.#type);
} }
/**
* @param {'month'|'year'} cadence
*/
getPrice(cadence) {
if (cadence === 'month') {
return this.monthlyPrice;
}
if (cadence === 'year') {
return this.yearlyPrice;
}
throw new ValidationError({
message: 'Invalid cadence'
});
}
/** @type {number|null} */ /** @type {number|null} */
#monthlyPrice; #monthlyPrice;
get monthlyPrice() { get monthlyPrice() {
@ -134,7 +149,7 @@ module.exports = class Tier {
} }
updatePricing({currency, monthlyPrice, yearlyPrice}) { updatePricing({currency, monthlyPrice, yearlyPrice}) {
if (this.#type !== 'paid') { if (this.#type !== 'paid' && (currency || monthlyPrice || yearlyPrice)) {
throw new ValidationError({ throw new ValidationError({
message: 'Cannot set pricing for free tiers' message: 'Cannot set pricing for free tiers'
}); });