MCPcopy
hub / github.com/CacheControl/json-rules-engine / start

Function start

examples/08-fact-comparison.js:15–130  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13const { Engine } = require('json-rules-engine')
14
15async function start () {
16 /**
17 * Setup a new engine
18 */
19 const engine = new Engine()
20
21 /**
22 * Rule for determining if account has enough money to purchase a $50 gift card product
23 *
24 * customer-account-balance >= $50 gift card
25 */
26 const rule = {
27 conditions: {
28 all: [{
29 // extract 'balance' from the 'customer' account type
30 fact: 'account',
31 path: '$.balance',
32 params: {
33 accountType: 'customer'
34 },
35
36 operator: 'greaterThanInclusive', // >=
37
38 // "value" in this instance is an object containing a fact definition
39 // fact helpers "path" and "params" are supported here as well
40 value: {
41 fact: 'product',
42 path: '$.price',
43 params: {
44 productId: 'giftCard'
45 }
46 }
47 }]
48 },
49 event: { type: 'customer-can-afford-gift-card' }
50 }
51 engine.addRule(rule)
52
53 engine.addFact('account', (params, almanac) => {
54 // get account list
55 return almanac.factValue('accounts')
56 .then(accounts => {
57 // use "params" to filter down to the type specified, in this case the "customer" account
58 const customerAccount = accounts.filter(account => account.type === params.accountType)
59 // return the customerAccount object, which "path" will use to pull the "balance" property
60 return customerAccount[0]
61 })
62 })
63
64 engine.addFact('product', (params, almanac) => {
65 // get product list
66 return almanac.factValue('products')
67 .then(products => {
68 // use "params" to filter down to the product specified, in this case the "giftCard" product
69 const product = products.filter(product => product.productId === params.productId)
70 // return the product object, which "path" will use to pull the "price" property
71 return product[0]
72 })

Callers 1

Calls 4

addRuleMethod · 0.95
addFactMethod · 0.95
runMethod · 0.95
factValueMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…