node-rules
Version:
Business Rules Engine for JavaScript
44 lines (43 loc) • 1.43 kB
JavaScript
import RuleEngine from '../../lib/node-rules.js';
/* Set of Rules to be applied
First blocks a transaction if less than 500
Second blocks a debit card transaction.*/
/*Note that here we are not specifying which rule to apply first.
Rules will be applied as per their index in the array.
If you need to enforce priority manually, then see examples with prioritized rules */
var rules = [{
"condition": function(R) {
R.when(this.transactionTotal < 500);
},
"consequence": function(R) {
this.result = false;
this.reason = "The transaction was blocked as it was less than 500";
R.stop();//stop if matched. no need to process next rule.
}
}, {
"condition": function(R) {
R.when(this.cardType === "Debit");
},
"consequence": function(R) {
this.result = false;
this.reason = "The transaction was blocked as debit cards are not allowed";
R.stop();
}
}];
/* Creating Rule Engine instance and registering rule */
var R = new RuleEngine();
R.register(rules);
/* Fact with more than 500 as transaction but a Debit card, and this should be blocked */
var fact = {
"name": "user4",
"application": "MOB2",
"transactionTotal": 600,
"cardType": "Debit"
};
R.execute(fact, function(data) {
if (data.result) {
console.log("Valid transaction");
} else {
console.log("Blocked Reason:" + data.reason);
}
});