0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/server/services/offers/service.js
Fabien O'Carroll 6220406716 Added support for Offers with Stripe Checkout
refs https://github.com/TryGhost/Team/issues/1090

This updates the Members & Offers modules to provide support for using
Offers in Stripe Checkout.

Members module now needs a handle to the Offers module repository, and
as such we have had to reorder the services boot order.
2021-10-06 17:24:39 +02:00

54 lines
1.7 KiB
JavaScript

const labs = require('../../../shared/labs');
const events = require('../../lib/common/events');
const DynamicRedirectManager = require('@tryghost/express-dynamic-redirects');
const OffersModule = require('@tryghost/members-offers');
const stripeService = require('../stripe');
const config = require('../../../shared/config');
const urlUtils = require('../../../shared/url-utils');
const models = require('../../models');
const redirectManager = new DynamicRedirectManager({
permanentMaxAge: config.get('caching:customRedirects:maxAge')
}, urlUtils);
module.exports = {
async init() {
const offersModule = OffersModule.create({
OfferModel: models.Offer,
redirectManager: redirectManager,
stripeAPIService: stripeService.api
});
this.api = offersModule.api;
this.repository = offersModule.repository;
if (labs.isSet('offers')) {
// handles setting up redirects
await offersModule.init();
}
// TODO: Delete after GA
let offersEnabled = labs.isSet('offers');
events.on('settings.labs.edited', async () => {
if (labs.isSet('offers') !== offersEnabled) {
offersEnabled = labs.isSet('offers');
if (offersEnabled) {
const offers = await this.api.listOffers();
for (const offer of offers) {
redirectManager.addRedirect(`/${offer.code}`, `/#/portal/offers/${offer.id}`, {permanent: false});
}
} else {
redirectManager.removeAllRedirects();
}
}
});
},
api: null,
middleware: redirectManager.handleRequest
};