-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate-monthly-payment-snap.js
79 lines (65 loc) · 2.58 KB
/
calculate-monthly-payment-snap.js
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
var script = function() {
var getData = function(params) {
var data = {
error: false,
errorMessage: "",
inputDataFields: {}
};
var keyArray = params.keySet().toArray();
for (var index in keyArray) {
var key = keyArray[index];
data.inputDataFields[key] = params.get(key);
}
return data;
},
validateParameters = function(inputDataFields) {
if (!inputDataFields["interest_rate"]
|| isNaN(inputDataFields["interest_rate"])
|| inputDataFields["interest_rate"] <= 0
|| inputDataFields["interest_rate"] >= 100)
throw "interest_rate is invalid";
if (!inputDataFields["term_months"]
|| isNaN(inputDataFields["term_months"])
|| inputDataFields["term_months"] <= 0)
throw "term_months is invalid";
if (!inputDataFields["loan_amt"]
|| isNaN(inputDataFields["loan_amt"])
|| inputDataFields["loan_amt"] <= 0)
throw "loan_amt is invalid";
},
calculatePayment = function(interest_rate, term_months, loan_amt) {
var monthly_rate = interest_rate / 12;
var factor = Math.pow(1 + monthly_rate, term_months);
return parseFloat(
(monthly_rate / (factor - 1) * (loan_amt * factor)).toFixed(2)
);
};
return {
input: input,
output: output,
error: error,
log: log,
execute: function() {
log.info("Payment Calc script started");
while (input.hasNext()) {
var data = {};
try {
var doc = input.next();
data = getData(doc);
validateParameters(data.inputDataFields);
data.monthlyPayment = calculatePayment(
data.inputDataFields["interest_rate"],
data.inputDataFields["term_months"],
data.inputDataFields["loan_amt"]);
output.write(doc, data);
} catch (err) {
data.error = true;
data.errorMessage = err ? err.toString() : "empty";
error.write(doc, data);
}
}
log.info("Payment Calc script finished");
}
};
}();
var hook = new com.snaplogic.scripting.language.ScriptHook(script);