|
1 |
| -"use strict"; |
2 | 1 | /*
|
3 | 2 | * This is a basic example demonstrating a condition that compares two facts
|
4 | 3 | *
|
|
9 | 8 | * DEBUG=json-rules-engine node ./examples/08-fact-comparison.js
|
10 | 9 | */
|
11 | 10 |
|
12 |
| -require("colors"); |
13 |
| -const { Engine } = require("json-rules-engine"); |
| 11 | +import "colors"; |
| 12 | +import { Engine } from "json-rules-engine"; |
14 | 13 |
|
15 | 14 | async function start() {
|
16 | 15 | /**
|
@@ -52,36 +51,35 @@ async function start() {
|
52 | 51 | };
|
53 | 52 | engine.addRule(rule);
|
54 | 53 |
|
55 |
| - engine.addFact("account", (params, almanac) => { |
| 54 | + engine.addFact("account", async (params, almanac) => { |
56 | 55 | // get account list
|
57 |
| - return almanac.factValue("accounts").then((accounts) => { |
58 |
| - // use "params" to filter down to the type specified, in this case the "customer" account |
59 |
| - const customerAccount = accounts.filter( |
60 |
| - (account) => account.type === params.accountType, |
61 |
| - ); |
62 |
| - // return the customerAccount object, which "path" will use to pull the "balance" property |
63 |
| - return customerAccount[0]; |
64 |
| - }); |
| 56 | + const accounts = await almanac.factValue<{ type: string }[]>("accounts"); |
| 57 | + // use "params" to filter down to the type specified, in this case the "customer" account |
| 58 | + const customerAccount = accounts.filter( |
| 59 | + (account) => account.type === params.accountType, |
| 60 | + ); |
| 61 | + // return the customerAccount object, which "path" will use to pull the "balance" property |
| 62 | + return customerAccount[0]; |
65 | 63 | });
|
66 | 64 |
|
67 |
| - engine.addFact("product", (params, almanac) => { |
| 65 | + engine.addFact("product", async (params, almanac) => { |
68 | 66 | // get product list
|
69 |
| - return almanac.factValue("products").then((products) => { |
70 |
| - // use "params" to filter down to the product specified, in this case the "giftCard" product |
71 |
| - const product = products.filter( |
72 |
| - (product) => product.productId === params.productId, |
73 |
| - ); |
74 |
| - // return the product object, which "path" will use to pull the "price" property |
75 |
| - return product[0]; |
76 |
| - }); |
| 67 | + const products = |
| 68 | + await almanac.factValue<{ productId: string }[]>("products"); |
| 69 | + // use "params" to filter down to the product specified, in this case the "giftCard" product |
| 70 | + const product = products.filter( |
| 71 | + (product) => product.productId === params.productId, |
| 72 | + ); |
| 73 | + // return the product object, which "path" will use to pull the "price" property |
| 74 | + return product[0]; |
77 | 75 | });
|
78 | 76 |
|
79 | 77 | /**
|
80 | 78 | * Register listeners with the engine for rule success and failure
|
81 | 79 | */
|
82 |
| - let facts; |
| 80 | + let facts: Record<string, unknown>; |
83 | 81 | engine
|
84 |
| - .on("success", (event, almanac) => { |
| 82 | + .on("success", (event) => { |
85 | 83 | console.log(
|
86 | 84 | facts.userId +
|
87 | 85 | " DID ".green +
|
|
0 commit comments