Newer
Older
li-add-unpaywall / index.js
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') {
        populateDocObjectWithUnpaywallEnrichment(docObjects[docIndex], 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));
  });
};

// This function only exists to convert the unpaywallEnrichment object keys to camelcase
function populateDocObjectWithUnpaywallEnrichment (docObject, unpaywallEnrichment) {
  if (!_.has(docObject, 'enrichments.openAccess.unpaywall')) {
    _.set(docObject, 'enrichments.openAccess.unpaywall', {});
  }

  const { unpaywall } = docObject.enrichments.openAccess;

  _.set(unpaywall, 'isOa', unpaywallEnrichment.is_oa);
  _.set(unpaywall, 'oaStatus', unpaywallEnrichment.oa_status);
  _.set(unpaywall, 'hasRepositoryCopy', unpaywallEnrichment.has_repository_copy);

  if (!_.isArray(unpaywallEnrichment.oa_locations)) return;

  unpaywall.oaLocations = unpaywallEnrichment.oa_locations.map(location => {
    const camelCasedLocation = _.mapKeys(location, function(value, key) {
      return _.camelCase(key);
    });
    return camelCasedLocation;
  });
}

module.exports = business;