UNPKG

paradigm-core

Version:
53 lines (39 loc) 1.23 kB
const { Dispatcher: {registerHook}, ApplicationsPlugin: {ApplicationModel} } = require('structure-core') const purgeUrlQueue = require('../queues/purge-url') /** * Hook for purging url after create/update/delete. Simply queues a purge job. * * @param {Object} req - express request object * @param {Object} response - the response from the dispatcher, which contains * the data from the previous controller method. * @returns {Object} response - the response to be returned to the user */ async function purgeUrl(req, response) { try { const applicationId = req.headers.applicationid const organizationId = req.headers.organizationid const applicationModel = new ApplicationModel({ logger: req.logger, organizationId }) const application = await applicationModel.getById(applicationId) const page = response.pkg purgeUrlQueue.add({ organizationId, application, hostname: application.host, pageSlug: page.slug, }) } catch(e) { req.logger.error('Failed to purge static page', e) } return response } registerHook({ when: 'after', serviceName: 'pages', actionNames: ['updateById', 'deleteById', 'purgeById'], }, purgeUrl)