const { RuleEngine } = require("node-rules");
/* Creating Rule Engine instance */
var R = new RuleEngine();
/* Add a rule */
var rule = {
"condition": (R, fact) => R.when(fact.transactionTotal < 500),
"consequence": (R, fact) => {
fact.result = false;
fact.reason = `The transaction was blocked as ${fact.transactionTotal} was less than 500`;
R.stop();
}
};
/* Register Rule */
R.register(rule);
/* Add a Fact with less than 500 as transaction, and this should be blocked */
var fact = {
"name": "user4",
"application": "MOB2",
"transactionTotal": 300,
"cardType": "Credit Card"
};
/* Check if the engine blocks it! */
R.execute(fact, function (data) {
if (data.result != false) {
console.log("Valid transaction");
} else {
console.log("Blocked Reason:" + data.reason);
}
});