UNPKG

paradigm-core

Version:
53 lines (39 loc) 1.22 kB
const { Dispatcher: {registerHook}, ApplicationsPlugin: {ApplicationModel}, } = require('structure-core') const purgeQueue = require('../queues/purge') /** * Hook for purging channel data. Simply queues a purge job. * * @param {Object} req - express request object * @param {Object} response - the response from the dispatcher, which cointains * the data from the previous controller method. * @returns {Object} response - the response to be returned to the user */ async function purgeChannel(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 channel = response.pkg purgeQueue.add({ applicationId, channelSlug: channel.slug, hostname: application.host, protocol: application.protocol }) } catch(e) { req.logger.error('Failed to purge channel', e) } return response } registerHook({ when: 'after', serviceName: 'channels', actionNames: ['create', 'updateById'], }, purgeChannel)