mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-24 06:35:49 +03:00
ff2da8a417
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.
78 lines
2.4 KiB
JavaScript
78 lines
2.4 KiB
JavaScript
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');
|
|
|
|
class OffersModule {
|
|
/**
|
|
* @param {OffersAPI} offersAPI
|
|
* @param {import('@tryghost/express-dynamic-redirects')} redirectManager
|
|
* @param {OfferRepository} repository
|
|
*/
|
|
constructor(offersAPI, redirectManager, repository) {
|
|
this.api = offersAPI;
|
|
this.repository = repository;
|
|
this.redirectManager = redirectManager;
|
|
}
|
|
|
|
/**
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async init() {
|
|
DomainEvents.subscribe(OfferCodeChangeEvent, (event) => {
|
|
if (event.data.previousCode) {
|
|
this.redirectManager.removeRedirect(`/${event.data.previousCode.value}`);
|
|
}
|
|
this.redirectManager.addRedirect(
|
|
`/${event.data.currentCode.value}`,
|
|
`/#/portal/offers/${event.data.offerId}`,
|
|
{permanent: false}
|
|
);
|
|
});
|
|
|
|
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) {
|
|
this.redirectManager.addRedirect(
|
|
`/${offer.code.value}`,
|
|
`/#/portal/offers/${offer.id}`,
|
|
{permanent: false}
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param {object} deps
|
|
* @param {import('@tryghost/express-dynamic-redirects')} deps.redirectManager
|
|
* @param {any} deps.OfferModel
|
|
* @param {any} deps.OfferRedemptionModel
|
|
*
|
|
* @returns {OffersModule}
|
|
*/
|
|
static create(deps) {
|
|
const repository = new OfferRepository(deps.OfferModel, deps.OfferRedemptionModel);
|
|
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;
|