prism-ad-campaigns
Version:
Prism Ad Campaigns
59 lines (42 loc) • 1.31 kB
JavaScript
const {registerHook} = require('structure-dispatcher')
const CreateAdCampaignJob = require('../jobs/create-ad-campaign')
/**
* Hook for creating/updating an facebook instant article when a document has
* been published
*
* @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 onAfterCreate(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 published = ['published', 'scheduled'].indexOf(document.status) > -1
if (published) {
const job = new CreateAdCampaignJob({
organizationId,
applicationId,
logger
})
job.queue({
organizationId,
applicationId,
document,
})
}
} catch(e) {
logger.error('After Create Hook Error', e)
}
resolve(response)
})
}
registerHook({
when: 'after',
serviceName: 'documents',
actionNames: ['create'],
}, onAfterCreate)