Newer
Older
li-add-unpaywall / index.js
'use strict';
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 unpaywallWsUrl = config.unpaywallWsUrl;

  const unpaywallRequestBody = [];
  let docIndex = 0;
  for(const {doi} of docObjects) {
    if (doi !== undefined && doi !== '') {
      unpaywallRequestBody.push({
        id: '' + (docIndex++),
        value: doi
      });
    }
  }
  if (unpaywallRequestBody.length <= 0) return cb();

  got.post(unpaywallWsUrl, {
    json: unpaywallRequestBody,
    responseType: 'json'
  }).then(({ body }) => {
    for(const unpaywallEnrichment of body) {
      if (unpaywallEnrichment.value !== 'n/a') {
        const docIndex = parseInt(unpaywallEnrichment.id,10);
        _.set(docObjects[docIndex], 'enrichments.openAccess.unpaywall', unpaywallEnrichment.value);
      }
    }
    return cb();
  }).catch(error => {
    return cb(handleError(docObjects, error));
  });
};



module.exports = business;