UNPKG

paradigm-core

Version:
58 lines (42 loc) 1.32 kB
const { Dispatcher: {registerHook}, ApplicationsPlugin: {ApplicationModel}, } = require('structure-core') const purgeUrlQueue = require('../queues/purge-url') /** * Hook for purging url after a create or delete. 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 purgeUrl(req, response) { const logger = req.logger || console try { const applicationId = req.headers.applicationid const organizationId = req.headers.organizationid const applicationModel = new ApplicationModel({ logger, organizationId }) logger.debug('Hook:documents--purge-url start') const application = await applicationModel.getById(applicationId) const id = req.params.id purgeUrlQueue.add({ organizationId, application, hostname: application.host, id, protocol: application.protocol }) } catch(e) { logger.debug('Hook:documents--purge-url failed', e) } return response } registerHook({ when: 'after', serviceName: 'documents', actionNames: ['deleteById', 'purgeById'], }, purgeUrl)