-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmongodb_app.js
1377 lines (1306 loc) · 44.3 KB
/
mongodb_app.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
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const randomArray = require('array-random');
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
const MongoStore = require('connect-mongo');
const app = express();
const http = require("http");
const socketio = require("socket.io")
const server = http.createServer(app);
const io = socketio(server);
var ObjectId = require('mongodb').ObjectID;
const CircularJSON = require("circular-json")
const {
fork
} = require("child_process");
const util = require('util');
const {
Worker,
isMainThread,
parentPort,
workerData
} = require('worker_threads');
const {
json
} = require("body-parser");
require('dotenv').config();
app.set('socketio', io);
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static("public"));
mongoose.connect(`mongodb+srv://admin-Raghav:${encodeURIComponent(process.env.MONGOCLUSTERPASS)}@cluster0.tbblr.mongodb.net/CollegeScheduler?retryWrites=true&w=majority`, {
poolSize: 460,
useNewUrlParser: true,
useUnifiedTopology: true
});
app.use(session({
secret: "You were expecting this string to be a secret used to encrypt cookies, but It was I, DIO!",
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: `mongodb+srv://admin-Raghav:${encodeURIComponent(process.env.MONGOCLUSTERPASS)}@cluster0.tbblr.mongodb.net/CollegeScheduler?retryWrites=true&w=majority`
})
}));
app.use(passport.initialize());
app.use(passport.session());
io.on('connection', function (socket) {
console.log('a user connected');
socket.on('disconnect', function () {
console.log('user disconnected');
});
});
mongoose.set("useCreateIndex", true);
const courseSchema = new mongoose.Schema({
courseName: String,
taughtTo: [mongoose.Types.ObjectId],
taughtBy: [mongoose.Types.ObjectId],
periods: [mongoose.Types.ObjectId]
});
const Course = new mongoose.model("course", courseSchema);
const periodSchema = new mongoose.Schema({
periodName: String,
parentCourse: mongoose.Types.ObjectId,
profTaking: mongoose.Types.ObjectId,
roomUsed: mongoose.Types.ObjectId,
periodLength: Number,
periodFrequency: Number,
groupsAttending: [mongoose.Types.ObjectId],
periodTime: Number,
periodAntiTime: [Number],
});
const Period = new mongoose.model("period", periodSchema);
const userSchema = new mongoose.Schema({
instituteName: {
type: String,
required: true
},
numberOfDays: {
type: Number,
default: 1,
required: true
},
periodsPerDay: {
type: Number,
default: 1,
required: true
},
schedule: {
type: Object,
default: new Object()
},
rooms: {
type: [new mongoose.Schema({
roomName: String,
roomCapacity: Number,
unAvialability: [Number],
periodsUsedIn: [mongoose.Types.ObjectId]
})],
requied: true,
default: new Array()
},
professors: {
type: [new mongoose.Schema({
profName: String,
coursesTaught: [mongoose.Types.ObjectId],
periodsTaken: [mongoose.Types.ObjectId],
unAvialability: [Number]
})],
requied: true,
default: new Array(),
},
groups: {
type: [new mongoose.Schema({
groupName: String,
groupQuantity: Number,
periodsAttended: [mongoose.Types.ObjectId],
coursesTaken: [mongoose.Types.ObjectId],
unAvialability: [Number]
})],
required: true,
default: new Array(),
},
courses: {
type: [courseSchema],
required: true,
default: new Array(),
},
periods: {
type: [periodSchema],
required: true,
default: new Array()
}
});
userSchema.plugin(passportLocalMongoose, {usernameField:"email"});
const User = new mongoose.model("user", userSchema);
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.get("/", (req, res) => res.sendFile(__dirname + "/webPages/index.html"));
app.get("/loginFailed", (req, res) => res.render("message", {
message: "You password or Username was incorrect. Try again"
}));
app.get("/login", (req, res) => res.sendFile(__dirname + "/webPages/login.html"));
app.post("/login", function (req, res) {
const user = new User({
email: req.body.email,
password: req.body.password
});
req.login(user, (err) => {
if (!err) {
passport.authenticate("local", {
failureRedirect: "/loginFailed"
})(req, res, () => {
res.redirect("/homepage");
});
} else {
console.log(err);
res.redirect("/");
}
});
});
app.get("/register", (req, res) => res.sendFile(__dirname + "/webPages/register.html"));
app.post("/register", (req, res) => {
// console.log(req.body)
User.exists({
email: req.body.email
}, (err, emailTaken) => {
if (emailTaken) {
return res.render("message", {
message: "Sorry, email taken"
});
} else {
User.register({
email: req.body.email,
instituteName: req.body.instituteName
}, req.body.password, (err, user) => {
if (!err) {
passport.authenticate("local")(req, res, function () {
res.redirect("/homepage");
});
} else {
console.log(err);
res.render("message", {
message: "Sorry, some internal error has occured"
});
}
});
}
});
});
app.get("/logout", (req, res) => {
req.logout();
res.redirect("/");
});
app.get("/homepage", (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
return res.sendFile(__dirname + "/webPages/homepage.html");
});
app.get("/parameter", (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
res.render("getParam", {
days: req.user.numberOfDays,
periods: req.user.periodsPerDay
})
});
app.post("/parameter", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
const promiseOne = User.updateOne({
_id: req.user._id
}, {
numberOfDays: req.body.days,
periodsPerDay: req.body.periods
});
const promiseTwo = User.updateOne({
_id: req.user._id
}, {
$set: {
"professors.$[].unAvialability": new Array()
}
});
const promiseThree = User.updateOne({
_id: req.user._id
}, {
$set: {
"rooms.$[].unAvialability": new Array()
}
});
const promiseFour = User.updateOne({
_id: req.user._id
}, {
$set: {
"groups.$[].unAvialability": new Array()
}
});
const promiseFive = User.updateOne({
_id: req.user._id
}, {
$set: {
"periods.$[].periodAntiTime": new Array()
},
$set: {
"periods.$[].periodTime": -1
}
});
await promiseOne;
await promiseTwo;
await promiseThree;
await promiseFour;
await promiseFive;
res.redirect("/homepage");
});
//Professor
{
app.get("/professor", (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
return res.render("getProf", {
profs: req.user.professors,
numberOfDays: req.user.numberOfDays,
periodsPerDay: req.user.periodsPerDay
});
});
app.get("/professorForm", (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
return res.render("professorForm", {
numberOfDays: req.user.numberOfDays,
periodsPerDay: req.user.periodsPerDay
});
});
app.post("/professor", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
unAvialability = new Array(0);
for (let loopitrt = 0; loopitrt < req.user.numberOfDays * req.user.periodsPerDay; loopitrt++)
if (req.body["periodTaken" + String(loopitrt)] === "on")
unAvialability.push(loopitrt);
await User.updateOne({
_id: req.user._id
}, {
$push: {
professors: {
profName: req.body.profName,
coursesTaught: new Array(0),
periodsTaken: new Array(0),
unAvialability: unAvialability
}
}
});
return res.redirect("/professorForm");
});
app.get("/deleteProfessor/:professorId", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
const {
coursesTaught,
periodsTaken
} = req.user.professors.find(prof => prof._id == req.params.professorId);
//below line removes the professor from the courses that s/he teaches
const promiseOne = User.updateOne({
_id: req.user._id,
}, {
$pull: {
"courses.$[element].taughtBy": req.params.professorId
}
}, {
arrayFilters: [{
"element._id": {
$in: coursesTaught
}
}]
});
//delete the periods assosiated with it
const promiseTwo = deleteManyPeriods(periodsTaken, req.user)
//delete the prof
const promiseThree = User.updateOne({
_id: req.user._id
}, {
$pull: {
"professors": {
_id: req.params.professorId
}
}
});
await promiseOne;
await promiseTwo;
await promiseThree;
return res.redirect("/professor");
});
}
//Groups
{
app.get("/group", (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
res.render("getGroup", {
groups: req.user.groups,
numberOfDays: req.user.numberOfDays,
periodsPerDay: req.user.periodsPerDay
});
});
app.get("/groupForm", (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
return res.render("groupForm", {
numberOfDays: req.user.numberOfDays,
periodsPerDay: req.user.periodsPerDay
});
});
app.post("/group", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
unAvialability = new Array(0);
for (let loopitrt = 0; loopitrt < req.user.numberOfDays * req.user.periodsPerDay; loopitrt++)
if (req.body["periodTaken" + String(loopitrt)] === "on")
unAvialability.push(loopitrt);
await User.updateOne({
_id: req.user._id
}, {
$push: {
groups: {
groupName: req.body.groupName,
groupQuantity: req.body.groupQuantity,
periodsAttended: new Array(0),
unAvialability: unAvialability
}
}
});
return res.redirect("/groupForm");
});
app.get("/deleteGroup/:groupId", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
const {
coursesTaken,
periodsAttended
} = req.user.groups.find(group => group._id == req.params.groupId);
//below line removes the groups from the courses that it attends
const promiseOne = User.updateOne({
_id: req.user._id
}, {
$pull: {
"courses.$[element].taughtTo": req.params.groupId
}
}, {
arrayFilters: [{
"element._id": {
$in: coursesTaken
}
}]
});
//delete the periods assosiated with it
const promiseTow = deleteManyPeriods(periodsAttended, req.user);
//delete the group
const promiseThree = User.updateOne({
_id: req.user._id
}, {
$pull: {
"groups": {
_id: req.params.groupId
}
}
});
await promiseOne;
await promiseTow;
await promiseThree;
return res.redirect("/group");
});
app.get("/editGroup/:groupId", (req, res) => {
const group = req.user.groups.find(group => group._id == req.params.groupId);
res.render("editGroup", {
group: group,
numberOfDays: req.user.numberOfDays,
periodsPerDay: req.user.periodsPerDay
});
});
app.post("/editGroup/:groupId", async (req, res) => {
const group = req.user.groups.find(group => group._id == req.params.groupId);
unAvialability = new Array(0);
for (let loopitrt = 0; loopitrt < req.user.numberOfDays * req.user.periodsPerDay; loopitrt++)
if (req.body["periodTaken" + String(loopitrt)] === "on")
unAvialability.push(loopitrt);
await User.updateOne({
_id: req.user._id,
"groups._id": req.params.groupId
}, {
$set: {
"groups.$": {
groupName: req.body.groupName,
groupQuantity: req.body.groupQuantity,
unAvialability: unAvialability,
periodsAttended: group.periodsAttended,
coursesTaken: group.coursesTaken,
_id: group._id
}
}
});
return res.redirect("/group");
});
}
//Rooms
{
app.get("/room", (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
res.render("getRoom", {
rooms: req.user.rooms,
numberOfDays: req.user.numberOfDays,
periodsPerDay: req.user.periodsPerDay
});
});
app.get("/roomForm", (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
return res.render("roomForm", {
numberOfDays: req.user.numberOfDays,
periodsPerDay: req.user.periodsPerDay
});
});
app.post("/room", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
unAvialability = new Array(0);
for (let loopitrt = 0; loopitrt < req.user.numberOfDays * req.user.periodsPerDay; loopitrt++)
if (req.body["periodTaken" + String(loopitrt)] === "on")
unAvialability.push(loopitrt);
await User.updateOne({
_id: req.user._id
}, {
$push: {
rooms: {
roomName: req.body.roomName,
roomCapacity: req.body.roomCapacity,
unAvialability: unAvialability,
periodsUsedIn: new Array(0)
}
}
});
return res.redirect("/roomForm");
});
app.get("/deleteroom/:roomId", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
const {
periodsUsedIn
} = req.user.rooms.find(room => room._id == req.params.roomId);
//delete the periods assosiated with it
const promiseOne = deleteManyPeriods(periodsUsedIn, req.user);
//delete the room
const promiseTwo = User.updateOne({
_id: req.user._id
}, {
$pull: {
"rooms": {
_id: req.params.roomId
}
}
});
await promiseOne;
await promiseTwo;
return res.redirect("/room");
});
}
//Courses
{
app.get("/course", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
let courseDisplay = new Array(0);
for (const course of req.user.courses) {
let taughtBy = new Array(0),
taughtTo = new Array(0);
// if (course.taughtBy.length == 0 || course.taughtTo.length == 0)
// return res.redirect("/deleteCourse/" + String(course._id));
for (const professor of course.taughtBy)
taughtBy.push(req.user.professors.find(prof => String(prof._id) == professor).profName);
for (const groupId of course.taughtTo)
taughtTo.push(req.user.groups.find(group => String(group._id) == groupId).groupName);
courseDisplay.push({
courseName: course.courseName,
taughtBy: taughtBy,
taughtTo: taughtTo,
_id: course._id
});
}
res.render("getCourse", {
courseDisplay: courseDisplay
});
});
app.get("/courseForm", (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
return res.render("courseForm", {
groups: req.user.groups,
profs: req.user.professors
});
});
app.post("/course", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
let taughtBy = new Array(0),
taughtTo = new Array(0);
for (const key in req.body)
if (req.body[key] === "on")
if (key[0] === 'P')
taughtBy.push(key.substring(1, key.length)); //key is of a professor
else
taughtTo.push(key.substring(1, key.length)); //key is of a group
if (taughtTo.length == 0 || taughtBy.length == 0)
return res.redirect("/course");
//course is created, but not saved, so that it doesn't create its own collection
const course = new Course({
courseName: req.body.courseName,
taughtTo: taughtTo,
taughtBy: taughtBy
});
const {
_id: thisCourseId
} = course;
//adding the course
const promiseOne = User.updateOne({
_id: req.user._id
}, {
$push: {
courses: course
}
});
//update all prof documents to reflect they teach this course
const promiseTwo = User.updateOne({
_id: req.user._id
}, {
$push: {
"professors.$[element].coursesTaught": thisCourseId
}
}, {
arrayFilters: [{
"element._id": {
$in: taughtBy
}
}]
});
//update all groups documents to reflect they learn this course
const promiseThree = User.updateOne({
_id: req.user._id
}, {
$push: {
"groups.$[element].coursesTaken": thisCourseId
}
}, {
arrayFilters: [{
"element._id": {
$in: taughtTo
}
}]
});
const {
numberOfLectures,
numberOfTutorials,
numberOfLabs
} = req.body;
await promiseOne;
await promiseTwo;
await promiseThree;
if (numberOfLectures == 0 && numberOfTutorials == 0 && numberOfLabs == 0)
res.redirect("/courseForm");
else
res.redirect("/courseTemplate/" + String(thisCourseId) + "/?numberOfLectures=" + numberOfLectures + "&numberOfTutorials=" + numberOfTutorials + "&numberOfLabs=" + numberOfLabs);
});
app.get("/deleteCourse/:courseId", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
const {
taughtTo,
taughtBy,
_id: thisCourseId,
periods
} = req.user.courses.find(course => String(course._id) == req.params.courseId);
//update all profs to reflect they don't teach this course anymore
const promiseOne = User.updateOne({
_id: req.user._id
}, {
$pull: {
"professors.$[element].coursesTaught": thisCourseId
}
}, {
arrayFilters: [{
"element._id": {
$in: taughtBy
}
}]
});
//update all groups to reflect they don't learn this course anymore.
const promiseTwo = User.updateOne({
_id: req.user._id
}, {
$pull: {
"groups.$[element].coursesTaken": thisCourseId
}
}, {
arrayFilters: [{
"element._id": {
$in: taughtTo
}
}]
});
//removing the course itself
const promiseThree = User.updateOne({
_id: req.user._id
}, {
$pull: {
"courses": {
_id: thisCourseId
}
}
});
//deleting all periods of the course
await deleteManyPeriods(periods, req.user);
await promiseOne;
await promiseTwo;
await promiseThree;
res.redirect("/course");
});
app.get("/courseTemplate/:courseId", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
const {
taughtBy: taughtByIds,
taughtTo: taughtToIds,
courseName
} = req.user.courses.find(course => String(course._id) == req.params.courseId);
if (taughtByIds.length == 0 || taughtToIds.length == 0)
return res.redirect("/deleteCourse/" + String(req.params.courseId));
const
taughtBy = req.user.professors.filter(prof => taughtByIds.includes(String(prof._id))),
taughtTo = req.user.groups.filter(group => taughtToIds.includes(String(group._id)));
res.render("courseTemplate", {
taughtBy: taughtBy,
taughtTo: taughtTo,
rooms: req.user.rooms,
numberOfDays: req.user.numberOfDays,
periodsPerDay: req.user.periodsPerDay,
courseId: req.params.courseId,
numberOfLectures: Number(req.query.numberOfLectures),
numberOfTutorials: Number(req.query.numberOfTutorials),
numberOfLabs: Number(req.query.numberOfLabs),
courseName: courseName
});
});
app.post("/courseTemplate", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
const {
courseId,
numberOfLectures,
numberOfTutorials,
numberOfLabs
} = req.body;
const course = req.user.courses.find(course => String(course._id) == courseId);
let promiseOne, promiseTwo, promiseThree;
if (Number(numberOfLectures) !== 0) {
let periodArgs = {
courseId: courseId,
periodName: (course.courseName + " Lecture "),
profId: req.body.lecture.profId,
periodLength: req.body.lecture.periodLength,
periodFrequency: numberOfLectures,
roomId: req.body.lecture.roomId,
};
for (const groupId of course.taughtTo)
periodArgs[groupId] = "on";
promiseOne = createPeriod(periodArgs, req.user);
}
if (Number(numberOfTutorials) !== 0)
for (let lpitrt = 0; lpitrt < course.taughtTo.length; lpitrt++) {
const {
groupId,
roomId,
profId,
periodLength
} = req.body["tutorial" + String(lpitrt)];
let periodArgs = {
courseId: courseId,
periodName: course.courseName + " Tutorial " + req.user.groups.find(group => String(group._id) == groupId).groupName,
profId: profId,
periodLength: periodLength,
periodFrequency: numberOfTutorials,
roomId: roomId,
};
periodArgs[groupId] = "on";
promiseTwo = createPeriod(periodArgs, req.user);
}
if (Number(numberOfLabs) !== 0)
for (let lpitrt = 0; lpitrt < course.taughtTo.length; lpitrt++) {
const {
groupId,
roomId,
profId,
periodLength
} = req.body["lab" + lpitrt];
let periodArgs = {
courseId: courseId,
periodName: course.courseName + " Lab " + req.user.groups.find(group => String(group._id) == groupId).groupName,
profId: profId,
periodLength: periodLength,
periodFrequency: numberOfLabs,
roomId: roomId,
};
periodArgs[groupId] = "on";
promiseThree = createPeriod(periodArgs, req.user);
}
await promiseOne;
await promiseTwo;
await promiseThree;
res.redirect("/courseForm");
});
// app.get("/editCourse/:courseId", async (req, res) => {
// if (!req.isAuthenticated())
// return res.redirect("/login");
// const course = req.user.courses.find(course => String(course._id) == req.params.courseId);
// if (!course)
// return res.send("The course id you sent wasn't linked to any course in the database.");
// return res.render("editCourse", {
// course: course,
// profs: req.user.professors,
// groups: req.user.groups
// });
// });
// app.post("/editCourse/:courseId", async (req, res) => {
// //this method is incorrect because it doesn't delete the periods that should not exist.
// //Say course C has a group G and has 2 periods P1,P2 of G
// //Now if the edit removes G then the periods P1,P2 should be deleted, but this method doesn't do that
// //..will add it in the future if the need of it is ever expressed.
// if (!req.isAuthenticated())
// return res.redirect("/login");
// let taughtBy = new Array(0),
// taughtTo = new Array(0);
// for (const key in req.body)
// if (req.body[key] === "on")
// if (req.user.professors.find(prof => String(prof._id) == key))
// taughtBy.push(mongoose.Types.ObjectId(key)); //key is of a professor
// else
// taughtTo.push(mongoose.Types.ObjectId(key)); //key is of a group
// await User.updateOne({
// _id: req.user._id,
// "courses._id": req.params.courseId
// }, {
// $set: {
// "courses.$.taughtBy": taughtBy,
// "courses.$.taughtTo": taughtTo
// }
// });
// return res.redirect("/course");
// });
}
//Periods
{
app.get("/period/:courseId", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
const {
periods: periodIds,
taughtBy: taughtByIds,
taughtTo: taughtToIds
} = req.user.courses.find(course => String(course._id) == req.params.courseId);
if (taughtByIds.length == 0 || taughtToIds.length == 0)
return res.redirect("/deleteCourse/" + req.params.courseId);
let periodDisplay = new Array(0);
const {
numberOfDays,
periodsPerDay
} = req.user;
for (const periodId of periodIds) {
period = req.user.periods.find(period => String(period._id) == periodId);
groupsAttendingDisplay = new Array(0);
for (const groupId of period.groupsAttending)
groupsAttendingDisplay.push(req.user.groups.find(group => String(group._id) == groupId).groupName);
const {
roomName: roomUsedDisplay
} = req.user.rooms.find(room => String(room._id) == period.roomUsed);
let preferenceString = ""
if (period.periodTime !== -1) {
preferenceString = "The period must take place at D" + String(Math.floor(period.periodTime / periodsPerDay) + 1) + "P" + String((period.periodTime % periodsPerDay) + 1);
} else if (period.periodAntiTime.length !== 0) {
preferenceString = "The period must not take place at ";
for (const prefval of period.periodAntiTime)
preferenceString += "D" + String(Math.floor(prefval / periodsPerDay) + 1) + "P" + String((prefval % periodsPerDay) + 1) + " , ";
} else {
preferenceString = "No special preference.";
}
const periodDisplayObject = {
_id: period._id,
periodName: period.periodName,
parentCourse: req.user.courses.find(course => String(course._id) == period.parentCourse).courseName,
profTaking: req.user.professors.find(prof => String(prof._id) == period.profTaking).profName,
periodLength: period.periodLength,
periodFrequency: period.periodFrequency,
groupsAttending: groupsAttendingDisplay,
roomUsed: roomUsedDisplay,
preference: preferenceString
};
periodDisplay.push(periodDisplayObject);
}
res.render("getPeriod", {
periods: periodDisplay
});
});
app.get("/periodForm/:courseId", (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
const {
taughtBy: taughtByIds,
taughtTo: taughtToIds
} = req.user.courses.find(course => String(course._id) == req.params.courseId);
if (taughtByIds.length == 0 || taughtToIds.length == 0)
return res.redirect("/deleteCourse/" + req.params.courseId);
let taughtBy = new Array(0),
taughtTo = new Array(0),
rooms = req.user.rooms;
const {
numberOfDays,
periodsPerDay
} = req.user;
for (const profId of taughtByIds)
taughtBy.push(req.user.professors.find(prof => String(prof._id) == profId));
for (const groupId of taughtToIds)
taughtTo.push(req.user.groups.find(prof => String(prof._id) == groupId));
res.render("periodForm", {
taughtBy: taughtBy,
taughtTo: taughtTo,
rooms: rooms,
numberOfDays: numberOfDays,
periodsPerDay: periodsPerDay,
courseId: (req.params.courseId)
});
});
app.post("/addPeriod", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
await createPeriod(req.body, req.user);
const {
courseId
} = req.body;
res.redirect("/periodForm/" + String(courseId));
});
app.get("/deletePeriod/:periodId", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
const period = req.user.periods.find(period => String(period._id) == req.params.periodId);
const courseId = period.parentCourse;
await deletePeriod(req.params.periodId, req.user);
res.redirect("/period/" + String(courseId));
});
}
//time table
{
app.get("/userDatabaseObject", (req,res) => {
if( !req.isAuthenticated())
return res.redirect("/login");
else{
return res.send(req.user);
}
});
app.get("/generateSchedule", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
if (req.user.periods.length == 0)
return res.render("message", {
message: "Please make some periods first"
});
else {
return res.sendFile( __dirname + "/webPages/waitingAnt.html");
}
});
app.get("/geneticAlgo", async (req, res) => {
if (!req.isAuthenticated())
return res.redirect("/login");
if (req.user.periods.length == 0)
return res.render("message", {
message: "Please make some periods first"
});
else {
console.log(req.user.periods.length, req.user.periods.length == 0)
res.sendFile(__dirname + "/webPages/waitingPage.html");
const childProcess = fork("./ScheduleGenerator.js");
setTimeout(() => childProcess.send({
"user": CircularJSON.stringify(req.user),
"io": CircularJSON.stringify(req.app.get('socketio'))
}), 1500);
childProcess.on("message", async message => {
if (message.case == "emit")
io.emit("message", message.emit);
if (message.case == "schedule") {
io.emit("message", {
case: "complete"
});
console.log(message.schedule, typeof message.schedule);
console.log(await User.updateOne({
_id: req.user._id
}, {
$set: {