paradigm-facebook-ia
Version:
Paradigm Facebook Instant Articles
66 lines (48 loc) • 1.58 kB
JavaScript
const {registerHook} = require('structure-dispatcher')
const CreateUpdateArticleJob = require('../jobs/create-update-article')
const DeleteArticleJob = require('../jobs/delete-article')
/*
* Hook for creating/updating/deleting facebook instant 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) {
try {
const applicationId = req.headers.applicationid
const organizationId = req.headers.organizationid
const document = response.pkg
const previouslyPublished = ['published', 'scheduled'].indexOf(document.previousStatus) > -1
const published = ['published', 'scheduled'].indexOf(document.status) > -1
if (previouslyPublished && !published) {
const job = new DeleteArticleJob({
organizationId,
applicationId,
logger: req.logger
})
job.queue({
documentId: document.id
})
} else if (published) {
const job = new CreateUpdateArticleJob({
organizationId,
applicationId,
logger: req.logger
})
job.queue({
documentId: document.id
})
}
} catch(e) {
req.logger.error('After Update Hook Error', e)
}
return response
}
registerHook({
when: 'after',
serviceName: 'documents',
actionNames: ['updateById'],
}, onAfterUpdate)