diff --git a/error-handler.js b/error-handler.js index 9b1c775..9ac2c89 100644 --- a/error-handler.js +++ b/error-handler.js @@ -14,17 +14,19 @@ * @param {string} errName The name of the `Error` instance. * @returns {Error} The new `Error` instance or the original one if it was provided. */ -function handleError (docObject, errName) { +function handleError (docObject, error) { // Looks for the appropriate error handler + const errName = error.name; let errorMsg = errorList[errName]; if (!errorMsg) { docObject.errCode = errName; - docObject._errMsg = errorMsg; } else { docObject.errCode = docObject._errMsg = errorMsg = errName; } + docObject.errMessage = error.message+"\n"+error.stack; const finalError = new Error(errorMsg); finalError.nonBlocking = true; + return new Error(finalError); } diff --git a/index.js b/index.js index ed6f344..7530ec2 100644 --- a/index.js +++ b/index.js @@ -7,23 +7,41 @@ const business = {}; business.doTheJob = function (docObject, cb) { - const doi = docObject.doi; - // if no DOI found - if (doi === undefined || doi === '') { - return 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: { value: doi }, + json: unpaywallRequestBody, responseType: 'json' }).then(({ body }) => { - const enrichments = body[0].value; - _.set(docObject, 'enrichments.openAccess.unpaywall', enrichments); + 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(docObject, error.name)); + return cb(handleError(docObjects, error)); }); }; + + module.exports = business; diff --git a/test/run.js b/test/run.js index 65afff8..ff982b4 100644 --- a/test/run.js +++ b/test/run.js @@ -7,29 +7,48 @@ // const config = require('../config/config'); // const nock = require('nock'); -let docObject, unpaywallEnrichments; +let docObjects, unpaywallEnrichments=[]; describe(pkg.name + '/index.js', function () { describe('#Vérification de l\'ajout des enrichissement unpaywall au cas où:', function () { - this.timeout(10000); + this.timeout(3000); before(function (done) { - docObject = { - doi: '10.15282/ijame.14.4.2017.7.0368' - }; - business.doTheJob(docObject, function () { - unpaywallEnrichments = docObject.enrichments.openAccess.unpaywall; + docObjects = [ + { + idIstex: "0123456789012345678901234567890123456789", + doi: '10.15282/ijame.14.4.2017.7.0368' + }, + { + idIstex: "1123456789012345678901234567890123456789", + doi: '10.1002/ijc.33939' + }, + { + idIstex: "2123456789012345678901234567890123456789", + doi: '10.1002/abcdefedcba' + } + ]; + business.finalJob(docObjects, function () { + for (const docObject of docObjects) { + unpaywallEnrichments.push(docObject?.enrichments?.openAccess?.unpaywall); + } done(); }); }); - it('Vérification du résultat', function (done) { - expect(unpaywallEnrichments).to.not.equal(undefined); + it('Vérifications génériques du résultat', function (done) { + expect(unpaywallEnrichments[0]).to.not.equal(undefined); + expect(unpaywallEnrichments[1]).to.not.equal(undefined); + expect(unpaywallEnrichments[2]).to.equal(undefined); done(); }); // Check result contain url it('L\'enrichissement contient un champs url', function (done) { - expect(unpaywallEnrichments).to.not.equal(undefined); + expect(unpaywallEnrichments[0].is_oa).to.be.true; + expect(unpaywallEnrichments[0].oa_locations.length).to.be.gte(0); + expect(unpaywallEnrichments[0].oa_locations[0].url.indexOf('http')).to.be.equal(0); + expect(unpaywallEnrichments[1].is_oa).to.be.false; + done(); }); });