forked from bigyunicorn/security_lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contributions.js
114 lines (78 loc) · 2.98 KB
/
contributions.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
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
/********************************************************************************/
/* */
/* contributions.js */
/* */
/* Handle contributions */
/* */
/********************************************************************************/
/********************************************************************************/
/* */
/* Imports */
/* */
/********************************************************************************/
var db = require("./database.js");
/********************************************************************************/
/* */
/* Hnadle Display Contributions page */
/* */
/********************************************************************************/
function displayContributions(req,res,next)
{
displayContributions0(req,res,next,false);
}
function displayContributions0(req,res,next,sts)
{
var userid = req.session.userId;
var q = "SELECT * FROM Contributions WHERE userId = " + userid;
db.query(q,function (e1,d1) { displayContributions1(req,res,next,sts,e1,d1); } );
}
function displayContributions1(req,res,next,sts,err,data)
{
if (err) return next(err);
var contrib = data.rows[0];
if (sts == true) contrib.updateSuccess = true;
return res.render("contributions",contrib);
}
/********************************************************************************/
/* */
/* Handle Contributions Update Page */
/* */
/********************************************************************************/
function handleContributionsUpdate(req,res,next)
{
// convert to numbers
var preTax = eval(req.body.preTax);
var afterTax = eval(req.body.afterTax);
var roth = eval(req.body.roth);
var userId = req.session.userId;
//validate contributions
if (isNaN(preTax) || isNaN(afterTax) || isNaN(roth) || preTax < 0 || afterTax < 0 || roth < 0) {
return res.render("contributions", {
updateError: "Invalid contribution percentages",
userId: userId
});
}
// Prevent more than 30% contributions
if (preTax + afterTax + roth > 30) {
return res.render("contributions", {
updateError: "Contribution percentages cannot exceed 30 %",
userId: userId
});
}
var q = "UPDATE Contributions SET preTax = " + preTax + ", afterTax = " + afterTax +
", roth = " + roth + " WHERE userId = " + userId;
db.query(q,function (e1,d1) { handleContributionsUpdate1(req,res,next,e1,d1); } );
}
function handleContributionsUpdate1(req,res,next,err,data)
{
if (err) return next(err);
return displayContributions0(req,res,next,true);
}
/********************************************************************************/
/* */
/* Exports */
/* */
/********************************************************************************/
exports.displayContributions = displayContributions;
exports.handleContributionsUpdate = handleContributionsUpdate;
/* end of contributions.js */