mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-24 06:35:49 +03:00
504fb1bfa1
no-issue This adds the concept of "Value Objects" to an Offers properties, allowing us to move validation out and ensure that an Offer will only ever have valid properties, without having to duplicate checks - or leave them to the persistent layer. This means we can fail early, as well as write unit tests for all of our validation.
62 lines
2.0 KiB
JavaScript
62 lines
2.0 KiB
JavaScript
const DomainEvents = require('@tryghost/domain-events');
|
|
const OfferCodeChangeEvent = require('./lib/events/OfferCodeChange');
|
|
const OfferRepository = require('./lib/OfferRepository');
|
|
const OffersAPI = require('./lib/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.previousCodes) {
|
|
for (const previousCode of event.data.previousCodes) {
|
|
this.redirectManager.removeRedirect(`/${previousCode.value}`);
|
|
}
|
|
}
|
|
this.redirectManager.addRedirect(
|
|
`/${event.data.currentCode.value}`,
|
|
`/#/portal/offers/${event.data.offerId}`,
|
|
{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 {import('@tryghost/members-stripe-service')} deps.stripeAPIService
|
|
* @param {any} deps.OfferModel
|
|
*
|
|
* @returns {OffersModule}
|
|
*/
|
|
static create(deps) {
|
|
const repository = new OfferRepository(deps.OfferModel, deps.stripeAPIService);
|
|
const offersAPI = new OffersAPI(repository);
|
|
return new OffersModule(offersAPI, deps.redirectManager, repository);
|
|
}
|
|
}
|
|
|
|
module.exports = OffersModule;
|