mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-18 16:01:40 +03:00
66970e5002
refs https://github.com/TryGhost/Team/issues/1726 - updates offer setup to allow new `trial` as discount type, was prev only `fixed` and `percent` - updates offer setup to allow `amount` as free trial days value - updates offer setup to allow `trial` as discount duration value for trial offers, was prev only `once`/`forever`/`repeating`
60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
const ValueObject = require('./shared/ValueObject');
|
|
const InvalidOfferDuration = require('../errors').InvalidOfferDuration;
|
|
|
|
/**
|
|
* @typedef {object} BasicDuration
|
|
* @prop {'once'|'forever'|'trial'} type
|
|
*/
|
|
|
|
/**
|
|
* @typedef {object} RepeatingDuration
|
|
* @prop {'repeating'} type
|
|
* @prop {number} months
|
|
*/
|
|
|
|
/**
|
|
* @extends ValueObject<BasicDuration|RepeatingDuration>
|
|
*/
|
|
class OfferDuration extends ValueObject {
|
|
/**
|
|
* @param {unknown} type
|
|
* @param {unknown} months
|
|
*/
|
|
static create(type, months) {
|
|
if (!type || typeof type !== 'string') {
|
|
throw new InvalidOfferDuration({
|
|
message: 'Offer `duration` must be a string.'
|
|
});
|
|
}
|
|
if (type !== 'once' && type !== 'repeating' && type !== 'forever' && type !== 'trial') {
|
|
throw new InvalidOfferDuration({
|
|
message: 'Offer `duration` must be one of "once", "repeating", "forever" or "trial.'
|
|
});
|
|
}
|
|
if (type !== 'repeating') {
|
|
return new OfferDuration({type});
|
|
}
|
|
if (typeof months !== 'number') {
|
|
throw new InvalidOfferDuration({
|
|
message: 'Offer `duration` must have include `duration_in_months` when "repeating".'
|
|
});
|
|
}
|
|
if (!Number.isInteger(months)) {
|
|
throw new InvalidOfferDuration({
|
|
message: 'Offer `duration_in_months` must be an integer greater than 0.'
|
|
});
|
|
}
|
|
if (months < 1) {
|
|
throw new InvalidOfferDuration({
|
|
message: 'Offer `duration_in_months` must be an integer greater than 0.'
|
|
});
|
|
}
|
|
return new OfferDuration({type, months});
|
|
}
|
|
|
|
static InvalidOfferDuration = InvalidOfferDuration;
|
|
}
|
|
|
|
module.exports = OfferDuration;
|
|
|