mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-23 22:11:09 +03:00
Updated Offer to only change code once
no-issue This simplifies the handling of updating redirects for a code, and doesn't affect our application layer because we never have the need to change a code twice. In future this should be replaced with events at the domain level - so that we do not have to track changed properties and instead a redirect service can listen to events, which would be dispatched on a successful save by the repository.
This commit is contained in:
parent
1f703920c9
commit
1312943f5b
@ -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}`,
|
||||
|
@ -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);
|
||||
|
@ -3,7 +3,7 @@
|
||||
/**
|
||||
* @typedef {object} OfferCodeChangeEventData
|
||||
* @prop {string} offerId
|
||||
* @prop {OfferCode[]} previousCodes
|
||||
* @prop {OfferCode} previousCode
|
||||
* @prop {OfferCode} currentCode
|
||||
*/
|
||||
|
||||
|
@ -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
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user