2021-10-05 17:24:47 +03:00
|
|
|
const DomainEvents = require('@tryghost/domain-events');
|
|
|
|
const OfferCodeChangeEvent = require('./lib/events/OfferCodeChange');
|
2021-10-05 12:15:57 +03:00
|
|
|
const OfferRepository = require('./lib/OfferRepository');
|
|
|
|
const OffersAPI = require('./lib/OffersAPI');
|
|
|
|
|
|
|
|
class OffersModule {
|
|
|
|
/**
|
|
|
|
* @param {OffersAPI} offersAPI
|
2021-10-05 17:24:47 +03:00
|
|
|
* @param {import('@tryghost/express-dynamic-redirects')} redirectManager
|
2021-10-06 15:58:55 +03:00
|
|
|
* @param {OfferRepository} repository
|
2021-10-05 12:15:57 +03:00
|
|
|
*/
|
2021-10-06 15:58:55 +03:00
|
|
|
constructor(offersAPI, redirectManager, repository) {
|
2021-10-05 12:15:57 +03:00
|
|
|
this.api = offersAPI;
|
2021-10-06 15:58:55 +03:00
|
|
|
this.repository = repository;
|
2021-10-05 17:24:47 +03:00
|
|
|
this.redirectManager = redirectManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async init() {
|
|
|
|
DomainEvents.subscribe(OfferCodeChangeEvent, (event) => {
|
|
|
|
if (event.data.previousCodes) {
|
|
|
|
for (const previousCode of event.data.previousCodes) {
|
2021-10-07 17:42:56 +03:00
|
|
|
this.redirectManager.removeRedirect(`/${previousCode.value}`);
|
2021-10-05 17:24:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.redirectManager.addRedirect(
|
2021-10-07 17:42:56 +03:00
|
|
|
`/${event.data.currentCode.value}`,
|
2021-10-05 17:24:47 +03:00
|
|
|
`/#/portal/offers/${event.data.offerId}`,
|
|
|
|
{permanent: false}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-10-07 17:42:56 +03:00
|
|
|
const offers = await this.repository.getAll();
|
2021-10-05 17:24:47 +03:00
|
|
|
|
|
|
|
for (const offer of offers) {
|
|
|
|
this.redirectManager.addRedirect(
|
2021-10-07 17:42:56 +03:00
|
|
|
`/${offer.code.value}`,
|
2021-10-05 17:24:47 +03:00
|
|
|
`/#/portal/offers/${offer.id}`,
|
|
|
|
{permanent: false}
|
|
|
|
);
|
|
|
|
}
|
2021-10-05 12:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} deps
|
|
|
|
* @param {import('@tryghost/express-dynamic-redirects')} deps.redirectManager
|
|
|
|
* @param {import('@tryghost/members-stripe-service')} deps.stripeAPIService
|
|
|
|
* @param {any} deps.OfferModel
|
|
|
|
*
|
|
|
|
* @returns {OffersModule}
|
|
|
|
*/
|
|
|
|
static create(deps) {
|
2021-10-05 17:24:47 +03:00
|
|
|
const repository = new OfferRepository(deps.OfferModel, deps.stripeAPIService);
|
2021-10-05 12:15:57 +03:00
|
|
|
const offersAPI = new OffersAPI(repository);
|
2021-10-06 15:58:55 +03:00
|
|
|
return new OffersModule(offersAPI, deps.redirectManager, repository);
|
2021-10-05 12:15:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = OffersModule;
|