UNPKG

prism-ad-campaigns

Version:
60 lines (43 loc) 1.44 kB
const {registerHook} = require('structure-dispatcher') const CreateAdCampaignJob = require('../jobs/create-ad-campaign') /* * Hook for creating/updating/deleting apple news articles based on the update * of a document * * @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 */ function onAfterUpdate(req, response) { return new Promise(async (resolve) => { const applicationId = req.headers.applicationid const organizationId = req.headers.organizationid const document = response.pkg const logger = req.logger || console try { const previouslyPublished = ['published', 'scheduled'].indexOf(document.previousStatus) > -1 const published = ['published', 'scheduled'].indexOf(document.status) > -1 if (!previouslyPublished && published) { const job = new CreateAdCampaignJob({ organizationId, applicationId, logger }) job.queue({ organizationId, applicationId, document, }) } } catch(e) { logger.error('After Update Hook Error', e) } resolve(response) }) } registerHook({ when: 'after', serviceName: 'documents', actionNames: ['updateById'], }, onAfterUpdate)