-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·568 lines (567 loc) · 17.8 KB
/
server.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
var express = require('express');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var mongojs = require("mongojs");
var compression = require('compression');
var fs = fs = require('fs');
var engines = require('consolidate');
var bodyParser = require('body-parser');
//db Setup
var db = mongojs('test', ['user', 'application', 'application_user']);
//var db = mongojs('admin:adminisdead@ds129028.mlab.com:29028/ctgs', ['user', 'application'])
var app = express();
var path = require('path');
//session var
var sess;
//Email
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var transporter = nodemailer.createTransport(smtpTransport({
host: 'mail.smtp2go.com'
, port: 587
, auth: {
user: 'goask.ca@gmail.com'
, pass: 'nwyMnWD8ksRI'
}
}));
// Set the default port to localhost 3000.
app.set('port', process.env.PORT || 3000);
// View engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
// Parsing coming JSON object
app.use(compression());
// app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
// Serving all public content only from ./public
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'ssshhhhh'
}));
transporter.verify(function (error, success) {
if (error) {
console.log(error);
}
else {
console.log('Server is ready to take our messages');
}
});
app.get('/', function (req, res) {
res.status(200);
console.log('GET - localhost:3000/');
if (sess != undefined) {
switch (sess.userType) {
case "admin":
console.log("User will be direcred to admin page!");
// res.status(200);
res.render('admin.html');
break;
case "requester":
console.log("User will be direcred to requester page!");
// res.status(200);
res.render('requester.html');
break;
case "evaluator":
console.log("User will be direcred to evaluator page!");
// res.status(200);
res.render('evaluator.html');
break;
case undefined:
res.render('index.html');
default:
// This would never happen if user was caterogorized properly
console.log("Error!");
console.log(sess.userType);
// res.status(200);
res.send('Invaild User Type')
break;
}
}
else {
sess = req.session;
sess.user_id;
sess.email;
sess.fname;
sess.userType;
res.render('index.html');
}
});
app.get('/apply', function (req, res) {
//200 OK
res.status(200);
console.log('GET - localhost:3000/apply')
//Serve content
res.render('apply.html');
});
app.get('/admin', function (req, res) {
// sess = req.session;
console.log('GET - localhost:3000/admin');
if (sess != undefined) {
switch (sess.userType) {
case "admin":
console.log("User will be direcred to admin page!");
res.status(200);
res.render('admin.html');
break;
case "requester":
console.log("User will be direcred to requester page!");
res.status(200);
res.render('requester.html');
break;
case "evaluator":
console.log("User will be direcred to evaluator page!");
res.status(200);
res.render('evaluator.html');
break;
case undefined:
res.render('index.html');
default:
// This would never happen if user was caterogorized properly
console.log("Error!");
console.log(sess.userType);
res.status(200);
res.send('Invaild User Type')
break;
}
}
else {
res.render('index.html');
}
});
app.get('/create-user', function (req, res) {
//200 OK
res.status(200);
console.log('GET - localhost:3000/create_user')
//Serve content
res.render('add_user.html');
});
app.get('/update-user', function (req, res) {
//200 OK
res.status(200);
console.log('GET - localhost:3000/update_user')
//Serve content
res.render('update_user.html');
});
app.get('/set-parameter', function (req, res) {
//200 OK
res.status(200);
console.log('GET - localhost:3000/update_user')
//Serve content
res.render('update_user.html');
});
// Serve requester page
app.get('/requester', function (req, res) {
//200 OK
res.status(200);
console.log('GET - localhost:3000/requester')
//Serve content
res.render('requester.html');
});
app.get('/student-application-status', function (req, res) {
res.render('student_application_status.html');
});
// Temporar /get_application_list_all
app.get('/get-application-list-all', function (req, res) {
db.application.find(function (err, records) {
if (err) {
console.log("Database Error" + err);
res.send("Database Error" + err);
}
else {
// console.log("awer");
console.log(records);
res.send(records);
}
});
});
app.get('/register', function (req, res) {
//200 OK
res.status(200);
console.log('GET - localhost:3000/register')
//Serve content
res.render('register.html');
});
app.get('/application', function (req, res) {
// sess = req.session;
// console.log("req.headers.cookie");
// console.log(req.headers.cookie);
// console.log(req.session);
//200 OK
res.status(200);
console.log('GET - localhost:3000/admin');
//Serve content
res.render('admin.html');
// 3
});
app.get('/evaluator', function (req, res) {
//200 OK
res.status(200);
console.log('localhost:3000/evaluator')
//Serve content
res.render('evaluator.html');
});
app.get('/create-application', function (req, res) {
console.log('localhost:3000/create-application');
if (sess != undefined) {
switch (sess.userType) {
case "admin":
console.log("User will be direcred to admin page!");
res.status(200);
res.send('Invaild User Type - You cannot create application as an admin, please user a requester account.');
break;
case "requester":
console.log("User will be direcred to requester page!");
res.status(200);
res.render('create-application.html');
break;
case "evaluator":
console.log("User will be direcred to evaluator page!");
res.status(200);
res.render('Invaild User Type - You cannot create application as an admin, please user a requester account.');
break;
case undefined:
res.render('index.html');
default:
// This would never happen if user was caterogorized properly
console.log("Error!");
console.log(sess.userType);
res.status(200);
res.send('Invaild User Type')
break;
}
}
else {
res.render('index.html');
}
});
app.get('/logout', function (req, res) {
req.session.destroy(function (err) {
if (err) {
console.log(err);
}
else {
sess = undefined;
res.render('index.html');
}
});
});
app.post('/register', function (req, res) {
//200 OK
res.status(200);
console.log('POST - localhost:3000/register')
//Parse content
var data = req.body;
console.log(data);
var firstname = req.body.fname;
var lastname = req.body.lname;
var email = req.body.email;
var id = req.body.id;
var pwd = req.body.pwd;
var usertype = "admin"
});
// Serve evaluator page
//app.get('/evaluator', function (req, res) {
////200 OK
// res.status(200);
// console.log('localhost:3000/evaluator')
////Serve content
// res.render('evaluator.html');
//});
// Recieve and parse password.
app.post('/login', function (req, res) {
//200 OK
res.status(200);
console.log('POST - localhost:3000/login')
//Parse body
console.log(req.body);
var id = JSON.stringify(req.body.uname);
////only search by id.
var data = '{' + '"id": ' + id + '}';
////Convert String to JSON
data = JSON.parse(data);
//Query Body
db.user.find(data, function (err, records) {
if (err) {
console.log("Database Error" + err);
res.send("Database Error" + err);
}
else {
// No error, but no result returned
if (records[0] == undefined) {
console.log("User does not exist!");
res.send("User does not exist!");
}
else {
//Result returned, user exist.
console.log("User found!")
console.log(records[0]);
if (records[0].password == req.body.password) {
// Password matched.
console.log("Password Matched, redirecting....");
sess = req.session;
sess.userType = records[0].usertype;
sess.fname = records[0].firstname;
sess.email = records[0].email;
sess.user_id = records[0].id;
//Look for user type and return to page
switch (records[0].usertype) {
case "admin":
console.log("User will be direcred to admin page!");
sess.username = records[0].id;
// res.status(200);
res.send('admin');
break;
case "requester":
console.log("User will be direcred to requester page!");
sess.userType = "requester";
// sess.id = "requester";
// console.log(JSON.stringify(sess));
// res.status(200);
res.send('requester');
break;
case "evaluator":
console.log("User will be direcred to evaluator page!");
sess.userType = "evaluator";
// sess.id = "evaluator";
// console.log(JSON.stringify(sess));
// res.status(200);
res.send('evaluator');
break;
default:
// This would never happen if user was caterogorized properly
console.log("Error!");
console.log(sess.userType);
// res.status(200);
res.send('Invaild User Type')
break;
}
}
else {
// sess.stats = false;
// User exist, but wrong password.
console.log("Wrong Password!");
res.send("Wrong Password!");
}
}
}
});
// }
});
app.post('/final-decision', function (req, res) {
//200 OK
res.status(200);
//get user email by query user
//find user email:
console.log(req.body.appid);
console.log('----------');
// var a=db.application_user.find({appid:req.body.appid});
// var appid = req.body.appid;
// appid
// var data = '{' + '"_id": ' + appid + '}';
// data = JSON.parse(data);
// console.log(appid);
console.log('----------');
db.application_user.find(function (err, records) {
if (err) {
console.log("Database Error" + err);
res.send("Database Error" + err);
}
else {
console.log('----------1');
for (var i = 0; i < records.length; i++) {
console.log('----------2');
// var thisappid = JSON.stringify(records[i].appid);
if (records[i].appid == req.body.appid) {
console.log(records[i].uid);
var uid = records[i].uid;
db.user.find(function (err, records) {
console.log(records);
for (var i = 0; i < records.length; i++) {
if (records[i]._id == uid ){
console.log("------------");
console.log(uid);
console.log("------------");
}else{
console.log(records[i]);
console.log(uid);
}
}
});
}
else {
console.log('----------3');
console.log(records[0]);
console.log("0000000");
console.log(records[i].appid);
console.log(req.body.appid);
}
}
}
});
// console.log(JSON.stringify(b));
// var b=db.user.find({_id:a.uid});
// console.log(JSON.stringify(b));
// if (req.body.Decision == "approve") {
// transporter.sendMail({
// from:
// , to: "rogerliuray@gmail.com"
// , subject: 'Application approved'
// , text: 'Hi, your application has been approved! Congratulations! :D'
// }, function (error, response) {
// if (error) {
// console.log(error);
// }
// else {
// console.log('Approved Message sent');
// }
// });
// }
// else {
// transporter.sendMail({
// from: 'CTGS@uottawa.ca'
// , to: "rogerliuray@gmail.com"
// , subject: 'Application refused'
// , text: 'Hi,i am sorry to inform you that your application has been refused!'
// }, function (error, response) {
// if (error) {
// console.log(error);
// }
// else {
// console.log('Refused Message sent');
// }
// });
// }
//Serve content
res.render('evaluator.html');
});
app.post('/get-application', function (req, res) {
//200 OK
res.status(200);
console.log('POST - localhost:3000/getapp');
var data = req.body.appid;
console.log(data);
//Serve content/
db.application.find(function (err, records) {
if (err) {
console.log("Database Error" + err);
res.send("Database Error" + err);
}
else {
for (var i = 0; i<records.length; i++){
if (records[i]._id == req.body.appid){
console.log(records[i]);
// res.send/ ("------------");
res.send (records[i]);
}else{
console.log(records[i]);
}
}
// console.log(records);
// res.send(records);
}
});
// res.render('register.html');
});
app.post('/add-user', function (req, res) {
//200 OK
res.status(200);
console.log('POST - localhost:3000/add_user')
//Parse content
var data = req.body;
console.log(data);
var firstnameI = req.body.fname;
var lastnameI = req.body.lname;
var emailI = req.body.email;
var idI = req.body.id;
var pwd = req.body.pwd;
var usertypeI = req.body.userType;
var data = {
firstname: firstnameI
, lastname: lastnameI
, id: idI
, password: pwd
, email: emailI
, usertype: usertypeI
}
console.log("datasadkfghakjsdhfiuashdjkfhajkshfdjkhsdjkh");
console.log(data);
db.user.insert(data);
res.render("add_user.html")
});
app.post('/delete-user', function (req, res) {
//200 OK
res.status(200);
console.log('POST - localhost:3000/delete_user')
//Parse content
var data = req.body;
console.log(data);
var firstname = req.body.fname;
var lastname = req.body.lname;
var email = req.body.email;
var idI = req.body.id;
var pwd = req.body.pwd;
var usertype = "admin"
db.products.remove({
id: idI
});
});
app.post('/submit', function (req, res) {
//200 OK
res.status(200);
console.log('POST - localhost:3000/submit')
//Parse content
});
app.post('/update-application', function (req, res) {
//200 OK
res.status(200);
console.log('POST - localhost:3000/update_application')
//Parse content
});
app.post('/request', function (req, res) {
//200 OK
res.status(200);
console.log('POST - localhost:3000/request');
console.log(req.body);
//Parse content
});
app.post('/find-user', function (req, res) {
//200 OK
console.log(req.body.email);
db.user.find({
email: req.body.email
}, function (err, records) {
if (err) {
console.log("Database Error" + err);
res.send("Database Error" + err);
}
else {
console.log("awer");
console.log(records);
res.send(records);
}
});
});
app.post('/create-application', function (req, res) {
//200 OK
console.log(req.body);
var data = req.body;
db.application.insert(data);
res.render("create-application");
});
// Custom 404 page.
app.use(function (req, res) {
res.type('text/plain');
res.status(404);
res.send('404 - Not Found');
});
// Custom 500 page.
app.use(function (err, req, res, next) {
console.error(err.stack);
res.type('text/plain');
res.status(500);
res.send('500 - Server Error');
});
// Start the server
app.listen(app.get('port'), function () {
console.log('Express started.');
});