-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeclaims.js
More file actions
125 lines (101 loc) · 3.75 KB
/
Copy pathnodeclaims.js
File metadata and controls
125 lines (101 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// This is a dockerize version of the claims sample from:
// https://github.com/zosconnect/sample-nodejs-clients
//
// The sample rule handles health insurance claims. The following rules
// are used when processing a claim:
//
// Drug claim - amount exceeded claim limit of $1000
// Dental claim - amount exceeded claim limit of $800
// Medical claim - amount exceeded claim limit of $500
//
var express = require('express');
var rule = require('node-rules');
var app = express();
const portNum = 8082;
// Display Ready Message if root path was specified
app.get('/', displayReady);
// REST API to Get claim result
app.get('/claim/rule', getClaimResult);
// Assign to server constant so we can handle closing later
const server = app.listen(portNum, displayStarted);
// Handle the termination properly (SIGTERM)
process.on('SIGTERM', () => {
console.info('SIGTERM signal received.');
console.log('Closing http server.');
server.close(() => {
console.log('Http server closed.');
});
});
//
// This function get the result of claim based on claim type and claim amount
// The following fields are passed as query parameters
// - claimType
// - claimAmount
//
function getClaimResult(req, res) {
var results = { };
// Create a Rule Engine instance
var R = new rule();
// Add a rule
var claimRule = {
"condition" : function(R) {
R.when((this.claimType === 'MEDICAL' && this.claimAmount > 100) ||
(this.claimType === 'DENTAL' && this.claimAmount > 800) ||
(this.claimType === 'DRUG' && this.claimAmount > 1000));
},
"consequence" : function(R) {
this.result = false;
R.stop();
}
};
// Register claim rule
R.register(claimRule);
// Get the data to be used for rule processing from request parameters
var claimData = {
'claimType' : req.query.claimType,
'claimAmount' : req.query.claimAmount
};
//
// Callbank function to set response from rule processing
//
var setResultDetails = function (result) {
results['claimType'] = claimData.claimType;
results['claimAmount'] = claimData.claimAmount;
if (result.result) {
results['status'] = 'Accepted';
results['reason'] = 'Normal claim';
}
else {
results['status'] = 'Rejected';
switch(claimData.claimType) {
case 'MEDICAL' :
results['reason'] = 'Amount exceeded $100. Claim require further review.';
break;
case 'DENTAL' :
results['reason'] = 'Amount exceeded $800. Claim require further review.';
break;
case 'DRUG' :
results['reason'] = 'Amount exceeded $1000. Claim require further review.';
break;
}
}
res.status(200).send(results);
}
// Process the rules using the data passed by caller
R.execute(claimData, setResultDetails);
}
function displayReady(req, res) {
res.write("===================================================\n");
res.write("= Node.js sample application on Docker container =\n");
res.write("===================================================\n");
res.write("= ****** ** *** ****** ** ** ****** =\n");
res.write("= ** ** ** ** ** *** *** ** =\n");
res.write("= ** ** ******* ** ** ** ** **** =\n");
res.write("= ** ** ** ** ** ** ** ** =\n");
res.write("= ****** ****** ** ** ****** ** ** ****** =\n");
res.write("===================================================\n");
res.end();
}
function displayStarted() {
console.log('Node.js application started and listening on local port ' + portNum);
}