mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Moved Offer event logic into the Offer Entity
We do not want our Repositories to implement business logic like deciding which events should be created. As that creates too tight of a coupling, and makes it harder to swap out repositories in future. Instead they should only be concerned with the storage, retrieval and hydration of Entities from the persistence engine.
This commit is contained in:
parent
af9be12a08
commit
a97383f2de
2 changed files with 19 additions and 14 deletions
|
@ -1,8 +1,6 @@
|
|||
const {flowRight} = require('lodash');
|
||||
const {mapKeyValues, mapQuery} = require('@tryghost/mongo-utils');
|
||||
const DomainEvents = require('@tryghost/domain-events');
|
||||
const OfferCodeChangeEvent = require('../domain/events/OfferCodeChangeEvent');
|
||||
const OfferCreatedEvent = require('../domain/events/OfferCreatedEvent');
|
||||
const Offer = require('../domain/models/Offer');
|
||||
const OfferStatus = require('../domain/models/OfferStatus');
|
||||
|
||||
|
@ -202,19 +200,13 @@ class OfferRepository {
|
|||
active: offer.status.equals(OfferStatus.create('active'))
|
||||
};
|
||||
|
||||
if (offer.codeChanged) {
|
||||
const event = OfferCodeChangeEvent.create({
|
||||
offerId: offer.id,
|
||||
previousCode: offer.oldCode,
|
||||
currentCode: offer.code
|
||||
});
|
||||
DomainEvents.dispatch(event);
|
||||
}
|
||||
|
||||
if (offer.isNew) {
|
||||
await this.OfferModel.add(data, options);
|
||||
const event = OfferCreatedEvent.create({offer});
|
||||
} else {
|
||||
await this.OfferModel.edit(data, {...options, id: data.id});
|
||||
}
|
||||
|
||||
for (const event of offer.events) {
|
||||
if (options.transacting) {
|
||||
// Only dispatch the event after the transaction has finished
|
||||
// Because else the offer won't be committed to the database yet
|
||||
|
@ -224,8 +216,6 @@ class OfferRepository {
|
|||
} else {
|
||||
DomainEvents.dispatch(event);
|
||||
}
|
||||
} else {
|
||||
await this.OfferModel.edit(data, {...options, id: data.id});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,8 @@ const OfferType = require('./OfferType');
|
|||
const OfferDuration = require('./OfferDuration');
|
||||
const OfferCurrency = require('./OfferCurrency');
|
||||
const OfferStatus = require('./OfferStatus');
|
||||
const OfferCreatedEvent = require('../events/OfferCreatedEvent');
|
||||
const OfferCodeChangeEvent = require('../events/OfferCodeChangeEvent');
|
||||
|
||||
/**
|
||||
* @typedef {object} OfferProps
|
||||
|
@ -100,6 +102,7 @@ class OfferTier {
|
|||
}
|
||||
|
||||
class Offer {
|
||||
events = [];
|
||||
get id() {
|
||||
return this.props.id.toHexString();
|
||||
}
|
||||
|
@ -195,6 +198,13 @@ class Offer {
|
|||
message: `Offer 'code' must be unique. Please change and try again.`
|
||||
});
|
||||
}
|
||||
|
||||
this.events.push(OfferCodeChangeEvent.create({
|
||||
offerId: this.id,
|
||||
previousCode: this.props.code,
|
||||
currentCode: code
|
||||
}));
|
||||
|
||||
this.changed.code = this.props.code;
|
||||
this.props.code = code;
|
||||
}
|
||||
|
@ -232,6 +242,11 @@ class Offer {
|
|||
/** @type OfferCode */
|
||||
code: null
|
||||
};
|
||||
if (options.isNew) {
|
||||
this.events.push(OfferCreatedEvent.create({
|
||||
offer: this
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue