-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (27 loc) · 944 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'use strict';
const elasticsearch = require('elasticsearch');
const { merge } = require('lodash');
const client = new elasticsearch.Client();
const coOpenAccess = {};
coOpenAccess.doTheJob = function (docObject, next) {
if (!docObject.hasDoi) return next(null, docObject);
client.search({
q: `doi:"${docObject.doi}"`,
index: 'unpaywall'
}).then(response => {
const hasZeroOrManyResult = response.hits.total !== 1;
if (hasZeroOrManyResult) return next(null, docObject);
const result = response.hits.hits.pop()._source;
const enrichment = {
isOa: result.is_oa,
isJournalIsOa: result.journal_is_oa,
journalIsInDoaj: result.journal_is_in_doaj,
oaStatus: result.oa_status,
bestOaLocation: result.best_oa_location,
oaLocations: result.oa_locations
};
docObject = merge(enrichment, docObject);
next(null, docObject);
}).catch(next);
};
module.exports = coOpenAccess;