mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-24 06:35:49 +03:00
Added and emitted events for Offer Created
refs https://github.com/TryGhost/Team/issues/1166 Since we removed the creation of coupons from the Offers module, we must emit events so that the Payments module can handle creating Coupons when Offers are created. We also export the events from the module so that they can be listened to by the Payments module. We also export other internals of the module so that the types can be used.
This commit is contained in:
parent
efe5164eff
commit
ff2da8a417
@ -1,5 +1,6 @@
|
||||
const DomainEvents = require('@tryghost/domain-events');
|
||||
const OfferCodeChangeEvent = require('./lib/domain/events/OfferCodeChange');
|
||||
const OfferCreatedEvent = require('./lib/domain/events/OfferCreated');
|
||||
const OfferRepository = require('./lib/application/OfferRepository');
|
||||
const OffersAPI = require('./lib/application/OffersAPI');
|
||||
|
||||
@ -30,6 +31,14 @@ class OffersModule {
|
||||
);
|
||||
});
|
||||
|
||||
DomainEvents.subscribe(OfferCreatedEvent, (event) => {
|
||||
this.redirectManager.addRedirect(
|
||||
`/${event.data.offer.code.value}`,
|
||||
`/#/portal/offers/${event.data.offer.id}`,
|
||||
{permanent: false}
|
||||
);
|
||||
});
|
||||
|
||||
const offers = await this.repository.getAll();
|
||||
|
||||
for (const offer of offers) {
|
||||
@ -54,6 +63,15 @@ class OffersModule {
|
||||
const offersAPI = new OffersAPI(repository);
|
||||
return new OffersModule(offersAPI, deps.redirectManager, repository);
|
||||
}
|
||||
|
||||
static events = {
|
||||
OfferCreatedEvent,
|
||||
OfferCodeChangeEvent
|
||||
};
|
||||
|
||||
static OfferRepository = OfferRepository;
|
||||
|
||||
static OffersAPI = OffersAPI;
|
||||
}
|
||||
|
||||
module.exports = OffersModule;
|
||||
|
@ -2,6 +2,7 @@ const {flowRight} = require('lodash');
|
||||
const {mapKeyValues, mapQuery} = require('@nexes/mongo-utils');
|
||||
const DomainEvents = require('@tryghost/domain-events');
|
||||
const OfferCodeChangeEvent = require('../domain/events/OfferCodeChange');
|
||||
const OfferCreatedEvent = require('../domain/events/OfferCreated');
|
||||
const Offer = require('../domain/models/Offer');
|
||||
const OfferStatus = require('../domain/models/OfferStatus');
|
||||
|
||||
@ -179,7 +180,7 @@ class OfferRepository {
|
||||
active: offer.status.equals(OfferStatus.create('active'))
|
||||
};
|
||||
|
||||
if (offer.codeChanged || offer.isNew) {
|
||||
if (offer.codeChanged) {
|
||||
const event = OfferCodeChangeEvent.create({
|
||||
offerId: offer.id,
|
||||
previousCode: offer.oldCode,
|
||||
@ -189,7 +190,9 @@ class OfferRepository {
|
||||
}
|
||||
|
||||
if (offer.isNew) {
|
||||
const event = OfferCreatedEvent.create({offer});
|
||||
await this.OfferModel.add(data, options);
|
||||
DomainEvents.dispatch(event);
|
||||
} else {
|
||||
await this.OfferModel.edit(data, {...options, id: data.id});
|
||||
}
|
||||
|
27
ghost/offers/lib/domain/events/OfferCreated.js
Normal file
27
ghost/offers/lib/domain/events/OfferCreated.js
Normal file
@ -0,0 +1,27 @@
|
||||
/** @typedef {import('../models/Offer')} Offer */
|
||||
|
||||
/**
|
||||
* @typedef {object} OfferCreatedEventData
|
||||
* @prop {Offer} offer
|
||||
*/
|
||||
|
||||
class OfferCreatedEvent {
|
||||
/**
|
||||
* @param {OfferCreatedEventData} data
|
||||
* @param {Date} timestamp
|
||||
*/
|
||||
constructor(data, timestamp) {
|
||||
this.data = data;
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {OfferCreatedEventData} data
|
||||
* @param {Date} [timestamp]
|
||||
*/
|
||||
static create(data, timestamp) {
|
||||
return new OfferCreatedEvent(data, timestamp || new Date);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = OfferCreatedEvent;
|
Loading…
Reference in New Issue
Block a user