Skip to content

Commit

Permalink
getDocumentByIdentifierWithInfo function and test
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinND committed Jun 25, 2014
1 parent 5fcd9e5 commit a5271b8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
32 changes: 32 additions & 0 deletions lib/utility/get-document-by-identifier-with-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

var async = require('async');

/**
* Fetch the document, with keys `document_type` and `provider` populated
* @param {String} The identifier of the document to get
* @param {Function} cb(err, document)
*/
module.exports = function getDocumentWithInfoByIdentifier(identifier, finalCb) {
var documentUri = '/documents/identifier/' + identifier;
var pages = {
'/document_types': {},
'/providers': {}
};
pages[documentUri] = {};

var self = this;
async.waterfall([
function makeBatchCall(cb) {
self.batch(pages, cb);
},

function handleResults(res, cb) {
var doc = res.body[documentUri];
doc.provider = res.body['/providers'][doc.provider];
doc.document_type = res.body['/document_types'][doc.document_type];

cb(null, doc);
}
], finalCb);
};
1 change: 0 additions & 1 deletion lib/utility/get-document-with-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var isMongoId = require('../helpers/is-mongo-id.js');
* @param {String} The id of the document to get. Must be a valid MongoDB ObjectId
* @param {Function} cb(err, document)
*/
// TODO: same function by `identifier`
module.exports = function getDocumentWithInfo(id, finalCb) {
if(!id || !isMongoId(id)) {
return finalCb(new Error('Argument error: the first argument must be a valid MongoDB ObjectId'));
Expand Down
13 changes: 13 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ describe('<High-level helper functions>', function() {
});
});

it('same thing by identifier', function(done) {
var identifier = configuration.test.fakeDocument.identifier;
anyfetch.getDocumentByIdentifierWithInfo(identifier, function(err, doc) {
should(err).not.be.ok;
should(doc).be.ok;
doc.should.have.properties('id', 'identifier', 'provider', 'document_type');
doc.provider.should.have.properties('client', 'name', 'document_count');
doc.document_type.should.have.properties('id', 'name', 'templates');

done();
});
});

after(function(done) {
anyfetch.deleteDocumentById(documentId, done);
});
Expand Down

0 comments on commit a5271b8

Please sign in to comment.