-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·977 lines (796 loc) · 28.5 KB
/
app.js
File metadata and controls
executable file
·977 lines (796 loc) · 28.5 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
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
var express = require('express'),
app = express(),
server = require('http').createServer(app).listen(process.env.HTTP_PORT || 8080),
io = require('socket.io').listen(server),
hbs = require('hbs'),
fs = require('fs'),
ObjectId = require('mongoose').Types.ObjectId,
MongoController = require('./MongoController.js'),
Mailer = require('./Mailer.js'),
Poller = require('./Poller.js'),
PhantomJobDispatcher = require('./PhantomJobDispatcher.js'),
TermManager = require('./TermManager.js'),
CatalogConnector = require('./CatalogConnector.js');
/*****
username and email are synonymous through this application
*****/
//LESSON LEARNED: In a one to many relationship, store the value of the one object as a field in each of the
// many objects... This way, querying is SO much easier.
//secury copy paste command
// scp -i GTCW.pem /Users/vikram/amazon_ec2/gtcw_gmail_pass.txt ec2-user@54.204.32.244:/home/ec2-user
//*CONFIG
var mongo_url = 'mongodb://localhost/gtcw',
THROTTLE_DELAY_SECS = 8,
PHANTOM_EVENTLOOP_DELAY_MS = 2000,
PROD_EMAIL_SERVICE = 'ses',
HTTPS_ENABLED = (process.env.HTTPS_ENABLED == "true" ? true : false),
TERM_PRODUCER_DELAY, //TermManager
TERM_CONSUMER_DELAY; //CatalogConnector
//*CONSTANTS
var millisInSecond = 1000,
millisInMinute = millisInSecond*60,
millisInHour = millisInMinute*60,
millisInDay = millisInHour*24;
if(process.env.BUILD_ENVIRONMENT == 'production') {
if(HTTPS_ENABLED){
var https_opts = {
key: fs.readFileSync("/home/ec2-user/ssl_key.pem"),
cert: fs.readFileSync("/home/ec2-user/certs/www_gtcoursewatch_us.crt"),
ca: [
fs.readFileSync("/home/ec2-user/certs/AddTrustExternalCARoot.crt"),
fs.readFileSync("/home/ec2-user/certs/COMODORSAAddTrustCA.crt"),
fs.readFileSync("/home/ec2-user/certs/COMODORSADomainValidationSecureServerCA.crt")
]
}
var secureServer = require('https').createServer(https_opts, app).listen( process.env.HTTPS_PORT || 8000);
var hostName = "https://www.gtcoursewatch.us";
}else {
var hostName = "http://www.gtcoursewatch.us";
}
if(PROD_EMAIL_SERVICE == 'gmail') {
var mailerEmail = "gtcoursewatch.mailer@gmail.com";
var mailerPass = fs.readFileSync("/home/ec2-user/gtcw_gmail_pass.txt").toString();
var myMailer = new Mailer(mailerEmail,
{ service: 'gmail',
pass: mailerPass });
} else if(PROD_EMAIL_SERVICE == 'ses') {
var mailerEmail = "admin@gtcoursewatch.us";
if(process.env.HOST_PROVIDER.toLowerCase() == 'digitalocean') {
var ses_creds = JSON.parse( fs.readFileSync('/root/ses_config.json') );
}else if(process.env.HOST_PROVIDER.toLowerCase() == 'amazon') {
var ses_creds = JSON.parse( fs.readFileSync('/home/ec2-user/ses_config.json') );
}
var myMailer = new Mailer(mailerEmail,
{ service: 'ses',
id: ses_creds.accessKeyID,
sekret: ses_creds.accessKeySecret });
}
TERM_PRODUCER_DELAY = 2*millisInMinute;
TERM_CONSUMER_DELAY = 1*millisInMinute;
registerPartials();
} else {
var https_opts = {
key: fs.readFileSync("/Users/vikram/amazon_ec2/ssl_key.pem"),
cert: fs.readFileSync("/Users/vikram/amazon_ec2/gtcw_ssl_certs/www_gtcoursewatch_us.crt"),
ca: [
fs.readFileSync("/Users/vikram/amazon_ec2/gtcw_ssl_certs/AddTrustExternalCARoot.crt"),
fs.readFileSync("/Users/vikram/amazon_ec2/gtcw_ssl_certs/COMODORSAAddTrustCA.crt"),
fs.readFileSync("/Users/vikram/amazon_ec2/gtcw_ssl_certs/COMODORSADomainValidationSecureServerCA.crt")
]
}
var secureServer = require('https').createServer(https_opts, app).listen(8000);
var hostName = "http://localhost:8080";
var mailerEmail = "gtcoursewatch.mailer@gmail.com";
var mailerPass = fs.readFileSync("/Users/vikram/amazon_ec2/gtcw_gmail_pass.txt").toString();
var myMailer = new Mailer(mailerEmail,
{ service: 'gmail',
pass: mailerPass });
TERM_PRODUCER_DELAY = .1*millisInMinute;
TERM_CONSUMER_DELAY = .1*millisInMinute;
//keep reloading partials
setInterval(registerPartials, 1000);
}
if(HTTPS_ENABLED) io.listen(secureServer);
//Ensure partial registration on startup
(function() {
var partials_path = "./views/partials";
partial_files = fs.readdirSync(partials_path);
partial_files.forEach(function(partial) {
var matches = /^([^.]+).hbs.html$/.exec(partial);
if (!matches) { return };
var name = matches[1];
var partial = fs.readFileSync(partials_path + "/" + partial, 'utf8');
hbs.registerPartial(name, partial);
});
})();
//*INITIALIZE CUSTOM MODULES
var myMongoController = new MongoController(mongo_url);
var myTermManager = new TermManager(mongo_url, TERM_PRODUCER_DELAY);
var myCatalogConnector =
new CatalogConnector(mongo_url, myTermManager, TERM_CONSUMER_DELAY);
var myDispatcher = new PhantomJobDispatcher( myMailer, myMongoController);
myDispatcher.startDispatcher(PHANTOM_EVENTLOOP_DELAY_MS);
var springPoller, fallPoller, summerPoller; //pollers
var current_pollers; //object that holds all current pollers
var springTerm, fallTerm, summerTerm; //string ids of current terms, also used to tell which terms to poll
var rejectRequests = false;
var fulfillment_stats = {
fulfilled: 0,
total: 0,
rate: 0
};
myMongoController.getFulfillmentStats(function(stats) {
fulfillment_stats = stats;
});
initPollers();
console.log('Spring Null? ' + (current_pollers['spring'] == null).toString());
console.log('Fall Null? ' + (current_pollers['fall'] == null).toString());
console.log('Summer Null? ' + (current_pollers['summer'] == null).toString());
//*Express Config
app.use(express.cookieParser());
var sessionStore = new express.session.MemoryStore; //equivalent to new express.session.MemoryStore()
app.use(express.session({secret: generateUUID(), store:sessionStore}));
app.configure(function() {
app.use(express.compress());
//middleware + res.locals
app.use(function(req, res, next) {
res.locals.username = req.session.username;
next();
});
//my implementation of flash
app.use(function(req, res, next) {
res.locals.success_flash = req.session.success_flash;
res.locals.warning_flash = req.session.warning_flash;
res.locals.danger_flash = req.session.danger_flash;
req.session.success_flash = null;
req.session.warning_flash = null;
req.session.danger_flash = null;
next();
});
});
app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.use(express.bodyParser());
app.use(app.router);
app.use(express.static('public'));
app.get('/', function(req, res) {
var springLabel, summerLabel, fallLabel;
//initialize labels only if terms are active
if(springTerm) springLabel = createLabel(springTerm);
if(summerTerm) summerLabel = createLabel(summerTerm);
if(fallTerm) fallLabel = createLabel(fallTerm);
res.render('index', {
title: 'GT CourseWatch',
spring: springTerm,
summer: summerTerm,
fall: fallTerm,
springLabel: springLabel,
summerLabel: summerLabel,
fallLabel: fallLabel
});
});
app.get('/about', function(req, res) {
res.render('about', {title:"About"});
});
//verify buzzport for automated registration
app.post('/verifyBuzzport', function(req, res) {
var post = req.body;
strip_whitespace_from_obj(post);
myDispatcher.addVerifyTaskToQueue(
{
username: post.username,
password: post.password
},
function(status) {
res.json({status: status});
}
);
});
//submit an automated registration request.
app.post('/autoRegReq', function(req, res) {
var post = req.body;
post.term = post.term.replace(' ', '-');
strip_whitespace_from_obj(post);
var user = req.session.username;
if(user) {
myMongoController.createAutoRegReq(post.crn, post.email, post.term, post.username, post.password, function(doc) {
myMongoController.userAccessor(user, function(user_arr) {
user_arr[0].auto_reqs.push(doc._id);
user_arr[0].save();
});
myMongoController.createConfirmationStat(0,0,1);
myMailer.sendConfirmationMail(post.email, post.crn, false, true);
});
res.json({status: "SUCCESS"});
} else{
res.json({status: "NOT_LOGGED_IN"});
}
});
//submit a regular, email only request
app.post('/reg_req_sub', function(req, res) {
var post = req.body;
strip_whitespace_from_obj(post);
myMongoController.createRequest(post.crn, post.email, post.term, function(doc) {
var user = req.session.username;
if(user) {
myMongoController.userAccessor(user, function(user_arr) {
user_arr[0].reg_reqs.push(doc._id);
user_arr[0].save();
});
}
myMailer.sendConfirmationMail(post.email, post.crn, false, false);
myMongoController.createConfirmationStat(1,0,0);
myMongoController.addToArchive('REG', post.email, post.term,
post.crn, null);
res.json({status: "SUCCESS"});
});
});
//submit a email + sms reuqest
app.post('/sms_req_sub', function(req, res) {
var post = req.body;
strip_whitespace_from_obj(post);
myMongoController.createSMSRequest(post.crn, post.email, post.gatewayedNumber, post.term, function(doc) {
var user = req.session.username;
if(user) {
myMongoController.userAccessor(user, function(user_arr) {
user_arr[0].sms_reqs.push(doc._id);
user_arr[0].save();
});
}
myMailer.sendConfirmationMail(post.email, post.crn, true, false);
myMongoController.createConfirmationStat(0,1,0);
myMongoController.addToArchive('SMS', post.email,
post.term, post.crn, post.gatewayedNumber);
res.json({status: "SUCCESS"});
});
});
//Throttling
app.get('/getTimeoutStatus', function(req, res) {
var THROTTLE_DELAY_MS = THROTTLE_DELAY_SECS*millisInSecond;
if(req.session.last_throttle_time == null) {
//initial hit
req.session.last_throttle_time = Date.now();
res.json({status:"good"});
} else{
//check if timeoout is up
var timeDelta = Date.now() - req.session.last_throttle_time;
if( timeDelta < THROTTLE_DELAY_MS) res.json({status:"bad", timeLeft: (THROTTLE_DELAY_MS-timeDelta)/1000});
else{
req.session.last_throttle_time = Date.now();
res.json({status:"good"})
}
}
});
app.get('/getFulfillmentStats', function(req, res) {
res.json(fulfillment_stats);
});
//get the number of other people in our database watching a particular CRN when a user makse a request
app.get('/getNumWatchers/:crn', function(req, res) {
strip_whitespace_from_obj(req.params);
myMongoController.Request.find({crn:req.params.crn}, function(err, requests) {
myMongoController.smsRequest.find({crn:req.params.crn}, function(err, smsRequests) {
//since stat is for 'other watcher' we don't include the request just made by the user, hence the -1
//we also don't want to accidently display a negative value
var numWatchers = Math.max(smsRequests.length + requests.length - 1 , 0);
res.json({numWatchers: numWatchers});
});
});
});
//Send OSCAR scraped stats for capacity, remaining, etc. AND number of people in our database watching a seat.
app.get('/getStats/:crn/:term', function(req, res) {
strip_whitespace_from_obj(req.params);
myMongoController.Request.find({crn:req.params.crn}, function(err, requests) {
myMongoController.smsRequest.find({crn:req.params.crn}, function(err, smsRequests) {
var pollers = getActivePollers(),
termPoller;
pollers.forEach(function(poller) {
if(poller.term == req.params.term) termPoller = poller;
});
//not executed asyncronously since forEach is blocking
if(termPoller) {
termPoller.getSeatStats(req.params.crn, function(crn, result) {
result['numWatchers'] = requests.length + smsRequests.length;
res.send(result);
});
} else{
if(!termPoller) res.send("bad req");
}
});
});
});
//verify a CRN on request forms
app.get('/verifyCRN/:crn/:term', function(req, res) {
var pollers = getActivePollers(),
termPoller;
strip_whitespace_from_obj(req.params);
pollers.forEach(function(poller) {
if(poller.term==req.params.term) termPoller = poller;
});
console.log(termPoller.term);
if(termPoller) {
termPoller.getSeatStats(req.params.crn, function(crn, result) {
if(result.hasOwnProperty('remaining')) {
res.send({verification_status:1})
} else{
res.send({verification_status:0})
}
});
} else{
res.send({verification_status:0})
}
});
app.get('/sign_up', function(req, res) {
//pass param user: req.session.username
res.render('sign_up',{title:"Sign Up"});
});
//create an account and send out an email verification link
app.post('/create_account', function(req, res) {
// myMongoController.createUser("jo@jo.com", "password", "uuid");
var post = req.body;
strip_whitespace_from_obj(post);
var email = post.email,
password = post.password,
password_conf = post.password_conf;
if(password != password_conf) {
req.session.danger_flash = "Passwords did not match!";
res.redirect('back');
} else if(password.length < 6) {
req.session.danger_flash = "Password must be at least 6 characters in length";
res.redirect('back');
}
else if(!isEmail(email)) {
req.session.danger_flash = "Invalid email format!";
res.redirect('back');
}
else{ // valid credentials
myMongoController.userAccessor(email, function(user_arr) {
if(user_arr.length > 0 && user_arr[0].activated) {
req.session.danger_flash = "That e-mail address has already been taken"
res.redirect('back');
} else{
if(user_arr.length > 0 && !user_arr[0].activated){
user_arr[0].remove();
}
var uuid=generateUUID(),
emailLink = generateEmailVerificationURL(email, uuid);
myMongoController.createUser(email, password, uuid);
myMailer.sendEmailVerification(email, emailLink);
req.session.success_flash = 'You have successfully signed up, now check your e-mail and activate your account before you can log in.';
res.redirect('/');
}
});
}
});
//endpoint used to confirm validation of an email account
app.get('/verifyEmail', function(req, res) {
var email = req.query.email,
uuid = req.query.uuid;
myMongoController.userAccessor(email, function(user_arr) {
var user = user_arr[0];
if(user && user.uuid == uuid) {
if(user.activated == true) {
req.session.warning_flash = "Your account has already been activated"
res.redirect('/');
return
}
user.activated = true;
user.save();
req.session.success_flash = "Account activated!"
res.redirect('/');
} else{
req.session.danger_flash = "Account activation failed"
res.redirect('/');
}
});
});
app.get('/log_in', function(req, res) {
res.render('login',{title:"Login"});
});
//endpoint to authenticate a user
app.post('/login_auth', function(req, res) {
var user = req.body.email;
var pass = req.body.password;
myMongoController.authenticate(user, pass, function(authRes, foundUser) {
if(authRes == true) {
if(foundUser.activated == false) {
req.session.warning_flash = "You need to activate your account from your e-mail before you can log in"
res.send({redirect: '/log_in'});
} else{
req.session.username = user;
req.session.success_flash = "You have successfully logged in"
res.send({redirect: '/'});
}
} else{
res.set('Content-Type', 'text/plain');
res.send(authRes);
}
});
});
app.get('/logout', checkAuth, function(req, res) {
req.session.destroy();
res.redirect('/');
});
app.get('/my_account', checkAuth, function(req, res) {
var username = req.session.username;
myMongoController.userAccessor(username, function(user_arr) {
res.render('settings', {user:user_arr[0]});
});
});
//endpoint to change a password from account settings.
app.post('/change_password', checkAuth, function(req, res) {
var post = req.body,
password = post.password,
password_conf = post.password_conf;
strip_whitespace_from_obj(post);
if(password != password_conf) {
req.session.danger_flash = "Passwords did not match!";
res.redirect('back');
} else if(password.length < 6) {
req.session.danger_flash = "Password must be at least 6 characters in length";
res.redirect('back');
} else{ //success
myMongoController.changePassword(req.session.username, password);
req.session.success_flash = "Password changed successfully"
res.redirect('back');
}
});
//send out a forgotten password link
app.post('/request_pass_change', function(req, res) {
var email = req.body.email
myMongoController.userAccessor(email, function(user_arr) {
var uuid=generateUUID(),
emailLink = generateEmailPassChangeURL(email, uuid),
user = user_arr[0];
if(user) {
user.uuid = uuid;
user.save();
myMailer.sendPassChangeVerification(email, emailLink);
res.send("success");
} else{
res.send("failure");
}
});
});
//render change password form ONLY if the email and UUID from the URL match a user in the DB
app.get('/verify_pass_change', function(req, res) {
var email = req.query.email,
uuid = req.query.uuid;
myMongoController.userAccessor(email, function(user_arr) {
var user = user_arr[0];
if(user && user.uuid == uuid) {
req.session.uuid = uuid;
res.render('change_password', {email: email});
} else{
req.session.danger_flash = "Invalid change password link";
res.redirect('/');
}
});
});
// endpoint submitted to from 'change_password' form;
// validates password and ONLY accepts password change if the session UUID matches the UUID of user being reset
// this way, we confirm the user in the current session clicked on the verification link
// (since the verification link route sets session UUID), and it wasn't just some random guy submitting a
// POST request to our endpoint with email and password params to change
app.post('/change_forgotten_password', function(req, res) {
var post = req.body,
password = post.password,
password_conf = post.password_conf,
email = post.email;
strip_whitespace_from_obj(post);
if(password != password_conf) {
req.session.danger_flash = "Passwords did not match!";
res.redirect('back');
} else if(password.length < 6) {
req.session.danger_flash = "Password must be at least 6 characters in length";
res.redirect('back');
} else{
myMongoController.userAccessor(email, function(user_arr) {
var user = user_arr[0];
if(user && (user.uuid == req.session.uuid)) {
user_arr[0].uuid = generateUUID(); //so that the old link doesn't work anymore
user_arr[0].save();
myMongoController.changePassword(email, password);
req.session.success_flash = "Password changed successfully"
res.redirect('/');
} else{
req.session.danger_flash = "Bad token.";
res.redirect('/');
}
});
}
});
//display a user's current requests
app.get('/my_requests', checkAuth, function(req, res) {
myMongoController.userAccessor(req.session.username, function(user_arr) {
var user = user_arr[0],
reg_reqs,
sms_reqs,
auto_reqs;
find_reqs(user.reg_reqs, myMongoController.Request, function(result) {
reg_reqs = result;
find_reqs(user.sms_reqs, myMongoController.smsRequest, function(result) {
sms_reqs = result;
find_reqs(user.auto_reqs, myMongoController.autoRegReq, function(result) {
auto_reqs = result;
format_terms(reg_reqs, sms_reqs, auto_reqs);
res.render('my_requests', {
sms_reqs: sms_reqs,
reg_reqs: reg_reqs,
auto_reqs: auto_reqs
});
});
});
});
//the for-each are blocking, therefore no async issues
function format_terms(reg_arr, sms_arr, auto_arr) {
reg_arr.forEach(function(e, i, a) {
if(e) a[i].term = format_watch_req(e.term);
});
sms_arr.forEach(function(e, i, a) {
if(e) {
a[i].term = format_watch_req(e.term);
a[i].gatewayedNumber = e.gatewayedNumber.replace(/@.+/,'');
}
});
auto_arr.forEach(function(e, i, a) {
if(e) a[i].term = format_auto_req(e.term);
});
}
function find_reqs(collection, model, next) {
var result = [],
collection = collection,
model = model,
requests_remaining = collection.length;
if( requests_remaining > 0) {
collection.forEach(function(id) {
model.findById(id, function(err, doc) {
//Disregard null requests from user's request collection.
if(doc) result.push(doc);
requests_remaining--;
if(requests_remaining == 0) { //only the last executed async call will meet this condition.
next(result);
}
});
});
} else{
next(result);
}
}
});
});
//endpoint that handles request cancellations
app.get('/cancel_req/:type/:id', checkAuth, function(req, res) {
strip_whitespace_from_obj(req.params);
var id = req.params.id,
type = req.params.type,
username = req.session.username;
switch(type) {
case "EMAIL":
myMongoController.user.update({email:username}, //initial query
{$pull:{reg_reqs: new ObjectId(id)}}, //array pull query
function(err,data) {} //REQUIRED callback..
);
myMongoController.Request.findByIdAndRemove(id, cancellation_redirect);
break;
case "SMS":
myMongoController.user.update({email:username}, //initial query
{$pull:{sms_reqs: new ObjectId(id)}}, //array pull query
function(err,data) {} //REQUIRED callback..
);
myMongoController.smsRequest.findByIdAndRemove(id, cancellation_redirect);
break;
case "AUTOMATED":
myMongoController.user.update({email:username}, //initial query
{$pull:{auto_reqs: new ObjectId(id)}}, //array pull query
function(err,data) {} //REQUIRED callback..
);
myMongoController.autoRegReq.findByIdAndRemove(id, cancellation_redirect);
break;
default:
console.log("Cancellation error... No type matched.");
}
function cancellation_redirect() {
req.session.success_flash = "Request successfully cancelled";
res.redirect('back');
}
});
//*WEBSOCKET HANDLING
// io.disable('heartbeats');
//io.set('transports', ['xhr-polling']);
io.sockets.on('connection', socketHandler);
function socketHandler(socket) {
socket.emit('message', {message:'WebSocket connection established; Welcome to the chat!'});
socket.on('sendMessage', function(data) {
io.sockets.emit('message', data);
});
socket.on('contactReq', function(data) {
myMailer.contactMailJob(data.email, data.name, data.message);
})
}
//figure out what terms are open presently and initalize pollers for them.
function initPollers() {
var pathComponents= ['/pls/bprod/bwckschd.p_disp_detail_sched?term_in=','4digityear','2digitmonth','&crn_in='];
initSpringPoller(pathComponents);
initSummerPoller(pathComponents);
initFallPoller(pathComponents);
current_pollers = { spring: springPoller,
summer: summerPoller,
fall: fallPoller };
//hibernation months, accept or process no requests,
//no labels for term selection either
if( !getNumPollers() ) {
rejectRequests = true;
}else{
rejectRequests = false;
}
}
//spring registration poller
function initSpringPoller(pathComponents) {
var d = new Date();
month = d.getMonth(),
year = (month == 0 ? d.getFullYear() : d.getFullYear() + 1),
pathComponents[1] = year;
if( isSpring(month) ) {
springTerm = 'spring' + year.toString();
pathComponents[2] = '02';
var springBasePath = pathComponents.join('');
springPoller = new Poller(myMongoController, myMailer, springBasePath, springTerm, myDispatcher);
}else {
springPoller = springTerm = null;
}
}
function initSummerPoller(pathComponents) {
var d = new Date();
month = d.getMonth(),
year = d.getFullYear(),
pathComponents[1] = year;
if ( isSummer(month) ) {
summerTerm = 'summer' + year.toString();
pathComponents[2] = '05';
var summerBasePath = pathComponents.join('');
summerPoller = new Poller(myMongoController, myMailer, summerBasePath, summerTerm, myDispatcher);
}else {
summerTerm = summerPoller = null;
}
}
function initFallPoller(pathComponents) {
var d = new Date();
month = d.getMonth(),
year = d.getFullYear(),
pathComponents[1] = year;
if ( isFall(month) ) {
fallTerm = 'fall' + year.toString();
pathComponents[2] = '08';
var fallBasePath = pathComponents.join('');
fallPoller = new Poller(myMongoController, myMailer, fallBasePath, fallTerm, myDispatcher);
}else {
fallTerm = fallPoller = null;
}
}
function isSpring(month) {
return month >= 9 || month <= 0;
}
function isFall(month) {
return month >= 2 && month <= 8;
}
function isSummer(month) {
return month >= 2 && month <= 5;
}
function getNumPollers() {
return getActivePollers().length;
}
//register partials
function registerPartials() {
var partials_path = "./views/partials",
partial_files = fs.readdirSync(partials_path);
partial_files.forEach(function(partial) {
var matches = /^([^.]+).hbs.html$/.exec(partial);
if (!matches) { return };
var name = matches[1];
var partial = fs.readFileSync(partials_path + "/" + partial, 'utf8');
hbs.registerPartial(name, partial);
});
}
//get all pollers for current school-terms.
function getActivePollers() {
var result = [];
for (var key in current_pollers) {
if (current_pollers.hasOwnProperty(key)) {
if(current_pollers[key]) {
result.push(current_pollers[key]);
}
}
}
return result;
}
function checkAuth(req, res, next) {
if (!req.session.username) {
req.session.danger_flash = "You must be logged in to perform that action";
res.redirect('/');
} else {
next();
}
}
//periodically access unused sessions so that they are expired by Express.
function sessionCleanup() {
sessionStore.all(function(err, sessions) {
for (var i = 0; i < sessions.length; i++) {
sessionStore.get(sessions[i], function() {} );
}
});
}
function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
function generateEmailVerificationURL(email, uuid) {
return hostName+"/verifyEmail?email=" +
email + "&uuid=" + uuid;
}
function generateEmailPassChangeURL(email, uuid) {
return hostName+"/verify_pass_change?email=" +
email + "&uuid=" + uuid;
}
//Semi-alias for create createLabel method, although different implementation..
function format_watch_req(term) {
var year_idx = term.indexOf(2),
season = term.slice(0,year_idx),
year = term.slice(year_idx),
capitalizedSeason = season.charAt(0).toUpperCase() + season.slice(1);
return capitalizedSeason + " " + year;
}
function format_auto_req(term) {
return term.replace('-', ' ');
}
//create labels for front-end term selectors
function createLabel(term) {
var length = term.length,
year = term.slice(length-4,length),
season = term.slice(0,length-4);
season = capitalizeFirstLetter(season);
return season + " " + year;
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function isEmail(email) {
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
function strip_whitespace(input) {
return input.replace(/\s+/g, "");
}
function strip_whitespace_from_obj(input_obj) {
for (var key in input_obj) {
if (input_obj.hasOwnProperty(key)) {
input_obj[key] = strip_whitespace(input_obj[key]);
}
}
}
//*SCHEDULED JOBS
myMongoController.cleanExpiredReqs();
//Daily Tasks
setInterval(function() {
initPollers();
myMongoController.cleanExpiredReqs();
sessionCleanup();
}, millisInDay);
//polling job
setInterval(function() {
for (var key in current_pollers) {
if (current_pollers.hasOwnProperty(key)) {
//alert(key + " -> " + p[key]);
if(current_pollers[key]) current_pollers[key].pollAllSeats();
}
}
}, 2*millisInMinute); //*millisInMinute
//refresh fulfillment stats
setInterval(function() {
myMongoController.getFulfillmentStats(function(stats) {
fulfillment_stats = stats;
});
}, 5*millisInMinute);
//Uncomment to re-run the crn scan on the term
// myTermManager.set_probed('201502', false);