mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Added concept of OfferStatus to domain model
refs https://github.com/TryGhost/Team/issues/1131 This allows us to model the behaviour of archived & active offers, as well as allowing us to set their status on the model.
This commit is contained in:
parent
d148108ae6
commit
1f936357d9
3 changed files with 40 additions and 2 deletions
|
@ -22,6 +22,7 @@ class InvalidOfferTierName extends InvalidPropError {}
|
|||
class InvalidOfferCadence extends InvalidPropError {}
|
||||
class InvalidOfferDuration extends InvalidPropError {}
|
||||
class InvalidOfferCoupon extends InvalidPropError {}
|
||||
class InvalidOfferStatus extends InvalidPropError {}
|
||||
|
||||
module.exports = {
|
||||
InvalidOfferName,
|
||||
|
@ -34,5 +35,6 @@ module.exports = {
|
|||
InvalidOfferCadence,
|
||||
InvalidOfferDuration,
|
||||
InvalidOfferTierName,
|
||||
InvalidOfferCoupon
|
||||
InvalidOfferCoupon,
|
||||
InvalidOfferStatus
|
||||
};
|
||||
|
|
|
@ -10,6 +10,7 @@ const OfferCadence = require('./OfferCadence');
|
|||
const OfferType = require('./OfferType');
|
||||
const OfferDuration = require('./OfferDuration');
|
||||
const OfferCurrency = require('./OfferCurrency');
|
||||
const OfferStatus = require('./OfferStatus');
|
||||
|
||||
/**
|
||||
* @typedef {object} OfferProps
|
||||
|
@ -24,6 +25,7 @@ const OfferCurrency = require('./OfferCurrency');
|
|||
* @prop {OfferDuration} duration
|
||||
* @prop {OfferCurrency} [currency]
|
||||
* @prop {string} [stripe_coupon_id]
|
||||
* @prop {OfferStatus} status
|
||||
* @prop {OfferTier} tier
|
||||
*/
|
||||
|
||||
|
@ -41,6 +43,7 @@ const OfferCurrency = require('./OfferCurrency');
|
|||
* @prop {number} duration_in_months
|
||||
* @prop {string} currency
|
||||
* @prop {string} [stripe_coupon_id]
|
||||
* @prop {string} status
|
||||
* @prop {TierProps|OfferTier} tier
|
||||
*/
|
||||
|
||||
|
@ -117,6 +120,14 @@ class Offer {
|
|||
return this.props.duration;
|
||||
}
|
||||
|
||||
get status() {
|
||||
return this.props.status;
|
||||
}
|
||||
|
||||
set status(value) {
|
||||
this.props.status = value;
|
||||
}
|
||||
|
||||
get oldCode() {
|
||||
return this.changed.code;
|
||||
}
|
||||
|
@ -248,6 +259,7 @@ class Offer {
|
|||
const type = OfferType.create(data.type);
|
||||
const cadence = OfferCadence.create(data.cadence);
|
||||
const duration = OfferDuration.create(data.duration, data.duration_in_months);
|
||||
const status = OfferStatus.create(data.status);
|
||||
|
||||
if (cadence.value === 'year' && duration.value.type === 'repeating') {
|
||||
throw new errors.InvalidOfferDuration({
|
||||
|
@ -306,7 +318,8 @@ class Offer {
|
|||
duration,
|
||||
currency,
|
||||
tier,
|
||||
stripe_coupon_id: couponId
|
||||
stripe_coupon_id: couponId,
|
||||
status
|
||||
}, {isNew});
|
||||
}
|
||||
}
|
||||
|
|
23
ghost/offers/lib/domain/models/OfferStatus.js
Normal file
23
ghost/offers/lib/domain/models/OfferStatus.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
const ValueObject = require('./shared/ValueObject');
|
||||
const InvalidOfferStatus = require('../errors').InvalidOfferStatus;
|
||||
|
||||
/** @extends ValueObject<'active'|'archived'> */
|
||||
class OfferStatus extends ValueObject {
|
||||
/** @param {unknown} status */
|
||||
static create(status) {
|
||||
if (typeof status !== 'string') {
|
||||
throw new InvalidOfferStatus({
|
||||
message: 'Offer `status` must be a string.'
|
||||
});
|
||||
}
|
||||
|
||||
if (status !== 'active' && status !== 'archived') {
|
||||
throw new InvalidOfferStatus({
|
||||
message: 'Offer `status` must be either "active" or "archived".'
|
||||
});
|
||||
}
|
||||
return new OfferStatus(status);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = OfferStatus;
|
Loading…
Add table
Reference in a new issue