2017-01-26 09:03:53 -05:00
|
|
|
var _ = require('lodash');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dgService getDocFromAlias
|
|
|
|
* @description Get an array of docs that match this alias, relative to the originating doc.
|
|
|
|
*/
|
2017-04-01 14:34:10 -04:00
|
|
|
module.exports = function getDocFromAlias(aliasMap) {
|
2017-01-26 09:03:53 -05:00
|
|
|
|
|
|
|
return function getDocFromAlias(alias, originatingDoc) {
|
|
|
|
var docs = aliasMap.getDocs(alias);
|
|
|
|
|
|
|
|
// If there is more than one item with this name then try to filter them by the originatingDoc's
|
|
|
|
// area
|
|
|
|
if (docs.length > 1 && originatingDoc && originatingDoc.area) {
|
|
|
|
docs = _.filter(docs, function(doc) { return doc.area === originatingDoc.area; });
|
|
|
|
}
|
|
|
|
|
|
|
|
// If filtering by area left us with none then let's start again
|
|
|
|
if (docs.length === 0) {
|
|
|
|
docs = aliasMap.getDocs(alias);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is more than one item with this name then try to filter them by the originatingDoc's
|
|
|
|
// module
|
|
|
|
if (docs.length > 1 && originatingDoc && originatingDoc.module) {
|
|
|
|
docs = _.filter(docs, function(doc) { return doc.module === originatingDoc.module; });
|
|
|
|
}
|
|
|
|
|
|
|
|
return docs;
|
|
|
|
};
|
2017-04-01 14:34:10 -04:00
|
|
|
};
|