UNPKG

1.41 kBJavaScriptView Raw
1var RuleEngine = require('../index');
2/* Set of Rules to be applied
3First blocks a transaction if less than 500
4Second blocks a debit card transaction.*/
5/*Note that here we are not specifying which rule to apply first.
6Rules will be applied as per their index in the array.
7If you need to enforce priority manually, then see examples with prioritized rules */
8var rules = [{
9 "condition": function(R) {
10 R.when(this.transactionTotal < 500);
11 },
12 "consequence": function(R) {
13 this.result = false;
14 this.reason = "The transaction was blocked as it was less than 500";
15 R.stop();//stop if matched. no need to process next rule.
16 }
17}, {
18 "condition": function(R) {
19 R.when(this.cardType === "Debit");
20 },
21 "consequence": function(R) {
22 this.result = false;
23 this.reason = "The transaction was blocked as debit cards are not allowed";
24 R.stop();
25 }
26}];
27/* Creating Rule Engine instance and registering rule */
28var R = new RuleEngine();
29R.register(rules);
30/* Fact with more than 500 as transaction but a Debit card, and this should be blocked */
31var fact = {
32 "name": "user4",
33 "application": "MOB2",
34 "transactionTotal": 600,
35 "cardType": "Debit"
36};
37R.execute(fact, function(data) {
38 if (data.result) {
39 console.log("Valid transaction");
40 } else {
41 console.log("Blocked Reason:" + data.reason);
42 }
43});
\No newline at end of file