const got = require('got'); const config = require('./config/config'); const _ = require('lodash'); const handleError = require('./error-handler'); const business = {}; business.doTheJob = function (docObject, cb) { return cb(); }; business.finalJob = function (docObjects, cb) { const finalJobLogs = { errLogs: [], processLogs: [], }; const unpaywallWsUrl = config.unpaywallWsUrl; const unpaywallRequestBody = []; let docIndex = 0; for (const { doi } of docObjects) { if (doi !== undefined && doi !== '') { unpaywallRequestBody.push({ id: '' + (docIndex), value: doi, }); } docIndex++; } if (unpaywallRequestBody.length <= 0) return cb(); got.post(unpaywallWsUrl, { retry: { limit: 5, methods: [ 'POST', ], }, json: unpaywallRequestBody, responseType: 'json', }).then(({ body }) => { for (const unpaywallEnrichment of body) { const docIndex = parseInt(unpaywallEnrichment.id, 10); if (unpaywallEnrichment.value !== 'n/a') { _.set(docObjects[docIndex], 'enrichments.openAccess.unpaywall', unpaywallEnrichment.value); finalJobLogs.processLogs.push(`info: DOI ${docObjects[docIndex].doi} found and is_oa=${unpaywallEnrichment.value?.is_oa}.`); } else { finalJobLogs.processLogs.push(`info: DOI ${docObjects[docIndex].doi} not found by unpaywall.`); } } return cb(null, finalJobLogs); }).catch(error => { finalJobLogs.errLogs.push(`Error querying Web service:\n${error.message}\n${error.stack()}`); return cb(handleError(docObjects, error)); }); }; module.exports = business;