From 34c0a7f6afaf09a8e020c43b012b609221bd546a Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Fri, 15 Oct 2021 11:18:11 +0200 Subject: [PATCH] Added redemptionCount to Offer refs https://github.com/TryGhost/Team/issues/1132 This is a read-only property for tracking how many times an Offer has been redeemed --- ghost/offers/lib/domain/models/Offer.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ghost/offers/lib/domain/models/Offer.js b/ghost/offers/lib/domain/models/Offer.js index 998c7aecdf..72dc9cd4a5 100644 --- a/ghost/offers/lib/domain/models/Offer.js +++ b/ghost/offers/lib/domain/models/Offer.js @@ -27,6 +27,7 @@ const OfferStatus = require('./OfferStatus'); * @prop {string} [stripe_coupon_id] * @prop {OfferStatus} status * @prop {OfferTier} tier + * @prop {number} redemptionCount */ /** @@ -44,6 +45,7 @@ const OfferStatus = require('./OfferStatus'); * @prop {string} currency * @prop {string} [stripe_coupon_id] * @prop {string} status + * @prop {number} redemptionCount * @prop {TierProps|OfferTier} tier */ @@ -124,6 +126,10 @@ class Offer { return this.props.status; } + get redemptionCount() { + return this.props.redemptionCount; + } + set status(value) { this.props.status = value; } @@ -261,6 +267,15 @@ class Offer { const duration = OfferDuration.create(data.duration, data.duration_in_months); const status = OfferStatus.create(data.status || 'active'); + if (isNew && data.redemptionCount !== undefined) { + // TODO correct error + throw new errors.InvalidOfferCode({ + message: 'An Offer cannot be created with redemptionCount' + }); + } + + const redemptionCount = data.redemptionCount || 0; + if (cadence.value === 'year' && duration.value.type === 'repeating') { throw new errors.InvalidOfferDuration({ message: 'Offer `duration` must be "once" or "forever" for the "yearly" cadence.' @@ -319,6 +334,7 @@ class Offer { currency, tier, stripe_coupon_id: couponId, + redemptionCount, status }, {isNew}); }