diff --git a/ghost/offers/index.js b/ghost/offers/index.js index 1b4e30f5c0..706bc6142f 100644 --- a/ghost/offers/index.js +++ b/ghost/offers/index.js @@ -20,10 +20,8 @@ class OffersModule { */ async init() { DomainEvents.subscribe(OfferCodeChangeEvent, (event) => { - if (event.data.previousCodes) { - for (const previousCode of event.data.previousCodes) { - this.redirectManager.removeRedirect(`/${previousCode.value}`); - } + if (event.data.previousCode) { + this.redirectManager.removeRedirect(`/${event.data.previousCode.value}`); } this.redirectManager.addRedirect( `/${event.data.currentCode.value}`, diff --git a/ghost/offers/lib/application/OfferRepository.js b/ghost/offers/lib/application/OfferRepository.js index ad54d346ee..1d0975d9f6 100644 --- a/ghost/offers/lib/application/OfferRepository.js +++ b/ghost/offers/lib/application/OfferRepository.js @@ -131,7 +131,7 @@ class OfferRepository { if (offer.codeChanged || offer.isNew) { const event = OfferCodeChangeEvent.create({ offerId: offer.id, - previousCodes: offer.isNew ? null : offer.oldCodes, + previousCode: offer.oldCode, currentCode: offer.code }); DomainEvents.dispatch(event); diff --git a/ghost/offers/lib/domain/events/OfferCodeChange.js b/ghost/offers/lib/domain/events/OfferCodeChange.js index b4d9bbc917..b76a9c498c 100644 --- a/ghost/offers/lib/domain/events/OfferCodeChange.js +++ b/ghost/offers/lib/domain/events/OfferCodeChange.js @@ -3,7 +3,7 @@ /** * @typedef {object} OfferCodeChangeEventData * @prop {string} offerId - * @prop {OfferCode[]} previousCodes + * @prop {OfferCode} previousCode * @prop {OfferCode} currentCode */ diff --git a/ghost/offers/lib/domain/models/Offer.js b/ghost/offers/lib/domain/models/Offer.js index fabcd9a926..c6f2e1b7c4 100644 --- a/ghost/offers/lib/domain/models/Offer.js +++ b/ghost/offers/lib/domain/models/Offer.js @@ -117,12 +117,12 @@ class Offer { return this.props.duration; } - get oldCodes() { + get oldCode() { return this.changed.code; } get codeChanged() { - return this.changed.code.length > 0; + return this.changed.code !== null; } get displayTitle() { @@ -174,12 +174,17 @@ class Offer { if (code.equals(this.props.code)) { return; } + if (this.changed.code) { + throw new errors.InvalidOfferCode({ + message: 'Offer `code` cannot be updated more than once.' + }); + } if (!await uniqueChecker.isUniqueCode(code)) { throw new errors.InvalidOfferCode({ message: 'Offer `code` must be unique.' }); } - this.changed.code.push(this.props.code); + this.changed.code = this.props.code; this.props.code = code; } @@ -197,7 +202,6 @@ class Offer { message: 'Offer `name` must be unique.' }); } - this.changed.name.push(this.props.name); this.props.name = name; } @@ -214,8 +218,8 @@ class Offer { this.options = options; /** @private */ this.changed = { - code: [], - name: [] + /** @type OfferCode */ + code: null }; }