2023-05-02 23:43:47 +03:00
|
|
|
/* 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');
|
2023-09-01 08:37:51 +03:00
|
|
|
const Offer = require('./domain/models/Offer');
|
2023-05-02 23:43:47 +03:00
|
|
|
const OffersAPI = require('./application/OffersAPI');
|
|
|
|
|
|
|
|
class OffersModule {
|
|
|
|
/**
|
|
|
|
* @param {OffersAPI} offersAPI
|
|
|
|
* @param {import('@tryghost/express-dynamic-redirects')} redirectManager
|
2023-09-01 08:44:45 +03:00
|
|
|
* @param {any} repository
|
2023-05-02 23:43:47 +03:00
|
|
|
*/
|
|
|
|
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
|
2023-09-01 08:44:45 +03:00
|
|
|
* @param {any} deps.repository
|
2023-05-02 23:43:47 +03:00
|
|
|
*
|
|
|
|
* @returns {OffersModule}
|
|
|
|
*/
|
|
|
|
static create(deps) {
|
2023-09-01 08:44:45 +03:00
|
|
|
const offersAPI = new OffersAPI(deps.repository);
|
|
|
|
return new OffersModule(offersAPI, deps.redirectManager, deps.repository);
|
2023-05-02 23:43:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static events = {
|
|
|
|
OfferCreatedEvent,
|
|
|
|
OfferCodeChangeEvent
|
|
|
|
};
|
|
|
|
|
2023-09-01 08:37:51 +03:00
|
|
|
static Offer = Offer;
|
|
|
|
|
2023-05-02 23:43:47 +03:00
|
|
|
static OffersAPI = OffersAPI;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = OffersModule;
|