node-rules
Version:
Business Rules Engine for JavaScript
42 lines (41 loc) • 1.23 kB
JavaScript
import RuleEngine from '../../lib/node-rules.js';
/* Set of Rules to be applied */
var rules = [{
"priority": 4,
"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();
}
}, {
"priority": 10, // this will apply first
"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"
};
/* This fact will be blocked by the Debit card rule as its of more priority */
R.execute(fact, function(data) {
if (data.result) {
console.log("Valid transaction");
} else {
console.log("Blocked Reason:" + data.reason);
}
});