mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-01 05:50:35 +03:00
127c61d4c6
As per our architecture guidelines we want to keep bookshelf implementations of Repositories in Ghost core, so that all the bookshelf code is kept together, and the packages implementing business logic with entities and services require less dependencies to test. This separation should also help us inadvertently add business logic to repository implementations by having a more "physical" boundary between them.
79 lines
2.3 KiB
JavaScript
79 lines
2.3 KiB
JavaScript
/* eslint-disable max-lines */
|
|
// @TODO: Reduce file length and remove the line above
|
|
|
|
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 OffersAPI = require('./application/OffersAPI');
|
|
|
|
class OffersModule {
|
|
/**
|
|
* @param {OffersAPI} offersAPI
|
|
* @param {import('@tryghost/express-dynamic-redirects')} redirectManager
|
|
* @param {any} 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.repository
|
|
*
|
|
* @returns {OffersModule}
|
|
*/
|
|
static create(deps) {
|
|
const offersAPI = new OffersAPI(deps.repository);
|
|
return new OffersModule(offersAPI, deps.redirectManager, deps.repository);
|
|
}
|
|
|
|
static events = {
|
|
OfferCreatedEvent,
|
|
OfferCodeChangeEvent
|
|
};
|
|
|
|
static Offer = Offer;
|
|
|
|
static OffersAPI = OffersAPI;
|
|
}
|
|
|
|
module.exports = OffersModule;
|