Created
May 10, 2016 14:24
-
-
Save mike-casas/5c4ccb77f554ef77c2ac054a7609dde7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _self = {}; | |
_self.find_rule= function(param){ | |
return new Promise(function(resolve, reject){ | |
var template=[]; | |
async.each(data, function(value) { | |
async.forEachOf(value.templates, function (rule, key) { | |
if(rule.id== param){ | |
template.push({"categoryName": value.name, rule}); | |
} | |
}); | |
}); | |
if(template.length === 0){ | |
return reject(new Error("rule no found")); | |
} | |
resolve(template); | |
}); | |
}; | |
_self.categories= function(rule){ | |
return new Promise(function(resolve,reject){ | |
let categories=[]; | |
async.forEachOf(data, function (value, key, callback) { | |
async.each(value.templates, function(i){ | |
async.each(i.categories, function(x){ | |
if(x == rule[0].categoryName){ | |
categories.push(i); | |
} | |
}); | |
}); | |
return callback(); | |
}, function (err) { | |
if (err) { | |
return reject(err.message); | |
} | |
resolve([{rule,"categories":categories}]); | |
}); | |
}); | |
}; | |
module.exports= _self; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
router.get('/rules/:ruleId', function(req, res) { | |
RulesServices.find_rule(req.params.ruleId) | |
.then(function(rule){ | |
RulesServices.categories(rule) | |
.then(function(data){ | |
sendJSONresponse(res,200, data); | |
}); | |
}).catch(function(err){ | |
sendJSONresponse(res,404, {"message":err.message}); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment