-
Notifications
You must be signed in to change notification settings - Fork 0
/
eLearn.js
721 lines (690 loc) · 24.1 KB
/
eLearn.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
var express = require('express')
var router = express.Router()
var bodyParser = require('body-parser')
var cookieParser = require('cookie-parser')
var mongoose = require('mongoose')
mongoose.Promise = global.Promise;
var jwt = require('jsonwebtoken')
var passport = require('passport')
var bcrypt = require('bcryptjs')
var Strategy = require('passport-http-bearer').Strategy
var BoxSDK = require('box-node-sdk')
var fs = require('fs')
var formidable = require('formidable')
var util = require('util')
var path = require('path')
var PDFDocument = require('pdfkit')
var prettyjson = require('prettyjson')
var Course = require('./models/Course')
var Enrollable = require('./models/Enrollable')
var UserElearn = require('./models/UserElearn')
var Quiz = require('./models/Quiz').Quiz
var SubmittedItem = require('./models/SubmittedItem')
require("dotenv").config({silent: true});
var ADMIN_EMAIL = process.env.ADMIN_EMAIL;
var TOKENSECRET = process.env.SECRET;
var KID = process.env.KID;
var CLIENT_ID = process.env.BOX_CLIENT_ID;
var CLIENT_SECRET = process.env.BOX_CLIENT_SECRET;
var PUBLIC_KEY_ID = process.env.BOX_PUBLIC_KEY_ID;
// var PRIVATE_KEY = process.env.BOX_PRIVATE_KEY;
var PRIVATE_KEY_PASSPHRASE = process.env.BOX_PRIVATE_KEY_PASSPHRASE;
var APP_ID = process.env.BOX_APP_ID;
var ENTERPRISE_ID = process.env.ENTERPRISE_ID;
var PRIVATE_KEY = fs.readFileSync("private_key.pem", 'utf-8');
var sdk = new BoxSDK({
clientID: CLIENT_ID,
clientSecret: CLIENT_SECRET,
appAuth: {
keyID: PUBLIC_KEY_ID,
privateKey: PRIVATE_KEY,
passphrase: PRIVATE_KEY_PASSPHRASE
}
});
//initialize BOX sdk to access course lessons
var box = sdk.getAppAuthClient('enterprise', ENTERPRISE_ID);
box.users.get(box.CURRENT_USER_ID, null, function(err, currentUser) {
if(err) console.log("error: ", err);
});
var jsonParser = bodyParser.json()
//create passport strategy to check for valid json web tokens.
passport.use(new Strategy(
function(token, done) {
if(token) {
jwt.verify(token, TOKENSECRET, function(err, decoded) {
if(err) {
return done(err)
}
return done(null, decoded, {scope: 'all'})
})
} else {
return done(null, false)
}
}
))
router.use(passport.initialize())
//serve files form build folder
router.use(express.static('build'));
//parse cookies
router.use(cookieParser());
//register new user
//requires name, email and password in request body
//returns status 201 with mongo id, name, courses (enrolled in) and json web token valide for 24 hours
//returns status 400 for email already in use, status 500 for server or database error
router.post('/users', jsonParser, function(req,res) {
var name = req.body.name
var email = req.body.email
var password = req.body.password
var enrolledCoursesUser = [], enrolledCoursesAdmin = [];
UserElearn.findOne({email: email}).exec()
.then(function(user) {
if(user != null) {
throw "emailInUse";
}
return Enrollable.find({email: req.body.email}).exec()
})
.then(function(enroll) {
if(enroll) {
enroll.forEach(function(entry) {
enrolledCoursesUser.push(entry.course_id);
if(entry.admin) enrolledCoursesAdmin.push(entry.course_id);
});
}
return bcrypt.genSalt(10, function(err, salt) {
if(err) {
throw err;
}
return bcrypt.hash(password, salt, function(err, hash) {
if(err) {
throw err;
}
var user = new UserElearn()
user.name = name;
user.email = email;
user.password = hash;
if(enrolledCoursesUser) user.courses = enrolledCoursesUser;
console.log("new user: ", user);
user.save()
.then(function(user) {
console.log("user saved: ", user);
console.log("enrolledCoursesAdmin: ", enrolledCoursesAdmin, enrolledCoursesAdmin == null, enrolledCoursesAdmin == []);
if(enrolledCoursesAdmin.length) {
return Promise.all(enrolledCoursesAdmin.map(function(course_id) {
return Course.findOneAndUpdate({_id: course_id},
{$push: {admin: user._id}}).exec();
}))
.then(function(courses) {
console.log("courses admin updated: ", courses);
return user;
})
} else {
return user
}
})
.then(function(user) {
return UserElearn.findOne({_id: user._id})
.populate({
path: 'courses',
populate: {
path: 'quizzes',
select: '_id, title'
}
}).lean().exec();
})
.then(function(user) {
var courses = user.courses.map((course) => {
course.admin = course.admin.toString().indexOf(user._id) > -1;
return course;
});
var token = jwt.sign({
_id: user._id,
name: user.name,
email: user.email,
courses: user.courses
}, TOKENSECRET, {
expiresIn: "24h"
});
return res.status(201).json({
_id: user._id,
name: user.name,
courses: courses,
token: token
});
})
});
});
})
.catch(function(err) {
console.log("error: ", err);
if(err === "emailInUse") {
return res.status(400).json({message: "email already associated with an account"});
}
return res.status(500).json({message: 'Internal server error'});
})
})
//unenroll user from a course
//must be course admin to submit request
//requires user email and course id in request body
//returns status 200 with remaining enrolled courses for user
//returns status 500 for server or database error
router.put('/users', jsonParser, passport.authenticate('bearer', {session: false}), function(req, res) {
var course_id = req.body.course_id;
var courseInToken = req.user.courses.find(function(courseInToken) {
if(courseInToken._id === course_id) return true;
});
if(!courseInToken.admin) return res.status(401).json({message: "only course admin can delete users from course"});
if(req.body.email === ADMIN_EMAIL) return res.status(401).json({message: 'Unable to delete site admin from course'});
UserElearn.findOneAndUpdate({email: req.body.email},
{$pull: {
courses: {$in: [course_id]}
}
}).exec()
.then(function() {
return UserElearn.find({courses: course_id}).exec()
})
.then(function(enrolled) {
return res.status(200).json({
enrolled: enrolled
})
})
.catch(function(err) {
console.log('Mongo error', err)
return res.status(500).json('Internal Server Error')
});
})
//render certificate of completion pdf, send to client
//requires course id and json web token to be included in request url
//returns status 200 with rendered pdf
//returns status 401 for invalid token
router.get('/users/certificate/:course/:token', function(req, res) {
if(req.params.token) {
jwt.verify(req.params.token, TOKENSECRET, function(err, decoded) {
if(err) {
console.log("err: ", err)
res.status(401).json({message: "unauthorized"})
} else {
var token = decoded;
var doc = new PDFDocument({layout: 'landscape'});
res.writeHead(200, {
'Content-Type': 'application/pdf',
'Access-Control-Allow-Origin': '*',
'Content-Disposition': 'inline; filename=Untitled.pdf'
});
doc.pipe(res)
doc.image('utilities/Certificate.jpg', (doc.page.width - 720)/2)
doc.font('Times-Roman')
doc.fontSize(30)
doc.text(token.name, {align: 'center'}, 250)
doc.fontSize(20)
doc.text(req.params.course, {align: 'center'}, 450)
doc.end()
}
})
} else {
res.status(401).json({message: "unauthorized"});
}
})
//logs in user, generates json web token valid for 24 hours
//requires user email and password in request body
//returns status 200 with user mongo id, name, email, enrolled courses, ids of passed quizzes and json web token
//returns status 400 for invalide password or token
router.post('/login', jsonParser, function(req, res) {
var password = req.body.password
UserElearn.findOne({email: req.body.email})
.populate({
path: 'courses',
populate: {
path: 'quizzes',
select: '_id, title'
}
})
.populate('passed', 'of')
.exec()
.then(function(user) {
if(!user) return res.status(400)
user.validatePassword(password, function(err, isValid) {
if(err) {
return res.status(400).json({message: 'Invalid token'})
}
if(!isValid) {
return res.status(400).json({message: 'Incorrect password'})
}
var token = jwt.sign({
_id: user._id,
name: user.name,
email: user.email,
courses: user.courses
}, TOKENSECRET, {
expiresIn: "24h"
})
user = user.toObject();
var courses = user.courses.map((course) => {
course.admin = course.admin.toString().indexOf(user._id) > -1;
return course;
})
return res.status(200).cookie({credential: token}).json({
_id: user._id,
name: user.name,
courses: courses,
passed: user.passed,
token: token
});
})
})
.catch(function(err) {
console.log("error: ", err);
return res.status(500).json({message: 'Internal server error'})
});
})
//querys a course for enrolled and enrollable users
//requires course id in request url
//must be course admin to make request
//returns status 200 with list of enrolled users and enrollable unregistered user emails
//returns status 401 if request is not made by course admin
//returns status 500 for server or database error
router.get('/course/enrollable/:course_id',
passport.authenticate('bearer', {session: false}), function(req, res) {
console.log("cookies in request: ", req.cookies);
var course_id = req.params.course_id;
var courseInToken = req.user.courses.find(function(courseInToken) {
if(courseInToken._id === course_id) return true;
});
if(!courseInToken.admin) return res.status(401).json({message: "only course admin can see enrolled users"});
Enrollable.find({course_id: course_id}).exec()
.then(function(enrollable) {
return UserElearn.find({courses: course_id}).select('_id name email').exec()
.then(function(enrolled) {
return [enrollable, enrolled];
})
})
.then(function(enrollArray) {
return res.status(200).json({
enrollable: enrollArray[0],
enrolled: enrollArray[1]
});
})
.catch(function(err) {
console.log('Mongo error ', err)
return res.status(500).json('Internal Server Error')
});
});
//adds unregistered user email to list of emails enrollable in a course
//must be course admin to make request
//requires course id, unregistered user email and admin boolean, indicating if user should be listed as a course admin, in request body
//returns status 201 with updated list of enrollable users for course
//returns status 401 if request is not made by course admin
//returns status 500 for server or database error
router.post('/course/enrollable', jsonParser, passport.authenticate('bearer', {session: false}), function(req, res) {
var course_id = req.body.course_id;
var courseInToken = req.user.courses.find(function(courseInToken) {
if(courseInToken._id === course_id) return true;
});
if(!courseInToken.admin) return res.status(401).json({message: "only admin can alter course"});
var enrolled = new Enrollable();
enrolled.email = req.body.email;
enrolled.course_id = course_id;
enrolled.admin = req.body.admin;
enrolled.save()
.then(function(enrolled) {
return Enrollable.find({course_id: course_id}).exec();
})
.then(function(enrollable) {
return res.status(201).json({enrollable: enrollable});
})
.catch(function(err) {
console.log('Mongo error ', err)
return res.status(500).json('Internal Server Error')
});
});
//removes unregistered user email from list of emails enrollable in course
//must be course admin to make request
//requires course id, and unregistered user email in request body
//returns status 200 with updated list of enrollable users for course
//returns status 401 if request is not made by course admin
//returns status 500 for server or database error
router.delete('/course/enrollable', jsonParser, passport.authenticate('bearer', {session: false}), function(req, res) {
var course_id = req.body.course_id;
var courseInToken = req.user.courses.find(function(courseInToken) {
if(courseInToken._id === course_id) return true;
});
if(!courseInToken.admin) return res.status(401).json({message: "only admin can alter course"});
Enrollable.findOne({email: req.body.email, course_id: course_id}).remove().exec()
.then(function(removedUser) {
return Enrollable.find({course_id: course_id}).exec();
})
.then(function(enrollable) {
return res.status(200).json({enrollable: enrollable});
})
.catch(function(err) {
console.log('Mongo error ', err)
return res.status(500).json('Internal Server Error')
});
});
//updates course name
//must be course admin to make request
//requires course id and updated course name in request body
//returns status 200 with updated course data (course quizzes populated with quiz id and title)
//returns status 401 if request is not made by course admin
//returns status 500 for server or database error
router.put('/course', jsonParser, passport.authenticate('bearer', {session: false}), function(req, res) {
var course = req.body.course;
var courseInToken = req.user.courses.find(function(courseInToken) {
if(courseInToken._id === course._id) return true;
});
if(!courseInToken.admin) return res.status(401).json({message: "only admin can alter course"});
return Course.findOneAndUpdate({_id: course._id},
{$set: {name: course.name}},
{new: true})
.populate({
path: 'quizzes',
select: '_id, title'
}).exec()
.then(function(updatedCourse) {
return res.status(200).json({course: updatedCourse})
})
.catch(function(err) {
console.log('Mongo error ', err)
return res.status(500).json('Internal Server Error')
})
});
//fetches quiz and any submissions of quiz by user
//requires quiz id and user id in request url
//returns status 200 with quiz and prior submissions of quiz by user
//returns status 500 for server or database error
router.get('/quiz/:quizId/:userId', passport.authenticate('bearer', {session:false}), function(req, res) {
Quiz.find({_id: req.params.quizId}).exec()
.then(function(quiz) {
return SubmittedItem.find({user: req.params.userId, of: req.params.quizId})
.exec()
.then(function(submitted) {
return [quiz, submitted];
});
})
.then(function(results) {
console.log("Quiz sent");
return res.status(200).json(results)
})
.catch(function(err) {
console.log("Mongo ERROR: ", err)
return res.status(500).json('Internal Server Error')
});
})
//creates or updates quiz; new quiz is listed to course
//must be course admin to make request
//requires course id, quiz id, title, courses, items, and minimumScore in request body
//returns status 201 with created quiz and updated course when quiz created
//returns status 200 with updated quiz and course when quiz updated
//returns status 401 if request is not made by course admin
//returns status 500 for server or database error
router.put('/quiz', jsonParser, passport.authenticate('bearer', {session: false}), function(req, res) {
console.log("save quiz request received");
var course = req.user.courses.find(function(course) {
if(course._id === req.body.courseID) return true;
});
if(!course.admin) return res.status(401).json({message: "only admin can alter course"});
var quiz = req.body.quiz;
if(quiz.courses[0] === "undefined") quiz.courses[0] = {id: req.body.courseID};
if(!quiz._id || quiz._id == null) {
var newQuiz = new Quiz();
newQuiz.title = quiz.title;
newQuiz.courses = quiz.courses;
newQuiz.items = quiz.items;
newQuiz.minimumScore = quiz.minimumScore;
newQuiz.live = quiz.live
newQuiz.save()
.then(function(quiz) {
return Course.findOne({_id: course._id}).populate({
path: 'quizzes',
select: '_id, title'
}).exec()
.then(function(courseToUpdate) {
courseToUpdate.quizzes.push({_id: quiz._id, title: quiz.title});
return courseToUpdate.save();
})
.then(function(course) {
course.admin = true;
return [quiz, course]
});
})
.then(function(state) {
console.log("quiz created, sending response...");
return res.status(201).json(state);
}).catch(function(err) {
console.log("error: ", err);
return res.status(500).json("Internal server error");
})
} else {
Quiz.findOneAndUpdate({_id: quiz._id}, {
title: quiz.title,
courses: quiz.courses,
items: quiz.items,
minimumScore: quiz.minimumScore,
live: quiz.live
}, {new: true, upsert: true}, function(err, quiz) {
if(err) {
console.log('Mongo error ', err)
return res.status(500).json('Internal Server Error')
}
console.log("quiz updated, sending response...");
Course.findOne({_id: course._id}).populate({
path: 'quizzes',
select: '_id, title'
}).exec()
.then(function(course) {
course.admin = true;
return res.status(200).json([quiz, course]);
})
.catch(function(err) {
if(err) console.log("Mongo error: ", err);
return res.status(500).json('Internal Server Error');
});
});
}
});
//deletes quiz, and removes quiz listing from course
//must be course admin to make request
//requires quiz id and course id in request url
//returns status 200 with updated course listings for user
//returns status 401 if request is not made by course admin
//returns status 500 for server or database error
router.delete('/quiz/:quizId/:courseId', passport.authenticate('bearer', {session:false}), function(req, res) {
var courseInToken = req.user.courses.find(function(course) {
if(course._id === req.params.courseId) return true;
});
if(!courseInToken.admin) return res.status(401).json({message: "only admin can alter course"});
var course = Course.findOneAndUpdate({_id: req.params.courseId},
{ $pull: {
quizzes: {$in: [req.params.quizId]}
}
}).exec()
course.then(function(course) {
return Quiz.remove({_id: req.params.quizId}).exec();
})
.then(function(quiz) {
return Quiz.findOne({_id: req.params.quizId}).exec()
})
.then(function(quiz){
if(quiz) {
throw new err;
}
})
.then(function() {
return UserElearn.findOne({email: req.user.email})
.populate({
path: 'courses',
populate: {
path: 'quizzes',
select: '_id, title'
}
})
.exec()
})
.then(function(user) {
res.status(200).json({courses: user.courses});
})
.catch(function(err) {
if(err) {
console.log("error: ", err);
res.status(500).json("Internal server error");
}
})
});
//records quiz submission by user
//requires quizData (quiz submision), quiz id, user id in request body
//calculates and saves submission score and date into entry
//returns status 200 with quiz score, submission and passed (boolean, depreciated client side)
//returns status 500 for server or database error
router.post('/quiz/submit', passport.authenticate('bearer', {session:false}), jsonParser, function(req, res) {
// score
let quizData = req.body.quizData;
let score = quizData.map(question => {
return question.correct ? 1 : 0;
});
score = score.reduce((a,b) => {return a + b}, 0);
var submission = new SubmittedItem;
submission.user = req.body.user_Id;
submission.submitted = new Date;
submission.score = score;
submission.item = quizData;
submission.of = req.body.quiz_Id;
submission.save()
.then(function(submission) {
return Quiz.findOne({_id: submission.of}).exec()
.then(function(quiz) {
console.log("sub and quiz: ", submission.score, quiz.minimumScore);
if(submission.score >= quiz.minimumScore) {
return [submission, true]
} else {
return [submission, false]
}
})
})
.then(function(state) {
if(state[1]) {
return UserElearn.findOne({_id: req.body.user_Id}).exec()
.then(function(user) {
user.passed.push(state[0]._id);
return user.save()
.then(function(user) {
return state;
})
})
}
else {
return state;
}
})
.then(function(state) {
return res.status(200).json({
message: "quiz submitted",
score: state[0].score,
attempt: state[0],
passed: state[1]
})
})
.catch(function(err) {
console.log("Mongo ERROR: ", err)
res.status(500).json('Internal Server Error')
})
})
//queries list of lessons in BOX folder
//currently hardcoded folder, will need to implement course id or folder id in request url
//returns status 200 with entries found by BOX (contains lesson name, id, modified time, size, url and sync state)
//returns status 500 for server or BOX sdk error
router.get('/lessons',
passport.authenticate('bearer',
{session:false}),
function(req, res) {
//box folder id is first arg
console.log("loading BOX lessons");
box.folders.getItems(
'18155048374',
{
fields: 'name,modified_at,size,url,sync_state',
offset: 0,
limit: 25
},
function(err, data) {
if(err) {
console.log("error: ", err);
res.status(500).json('Internal Server Error');
} else {
res.status(200).json(data);
}
}
);
})
//fetchs BOX preview and download url of lesson pdf
//requires BOX lesson id in request url
//returns status 200 with selectedPdf preview (url), download (url), name and id
//returns status 500 for server or BOX sdk error
router.get(
'/lessons/:id',
passport.authenticate('bearer', {session:false}),
function(req, res) {
box.files.update(
req.params.id,
{
shared_link: box.accessLevels.DEFAULT
},
function(err, link) {
if(err) {
console.log("error: ", err);
res.status(500).json('Internal Server Error');
}
res.status(200).json({
selectedPdf: {
preview: link.shared_link.url,
download: link.shared_link.download_url,
name: link.name,
id: link.id
}
});
})
})
//serves client side assests, html, css and js
//enables react-router to navigate in client-side app
router.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
//for file upload
router.post('/lessons',
passport.authenticate('bearer', {session:false}),
function(req, res) {
var form = new formidable.IncomingForm();
form.multiples = true;
form.uploadDir = path.join(__dirname);
form.parse(req, function(err, fields, files) {
console.log("parsed files: ", files);
console.log("parsed files: ", files.file.path);
var stream = fs.createReadStream(files.file.path);
box.files.uploadFile('18155048374', files.file.name, stream, function() {
if(err) {
console.log("box error: ", err);
return res.status(500).json('Internal Server Error');
}
res.status(200).json(files);
fs.unlink(files.file.path, function(err) {
if(err) console.log("file delete error: ", err);
})
})
});
}
)
router.delete('/lessons/:courseID/:lessonID',
passport.authenticate('bearer', {session:false}),
function(req,res) {
var courseInToken = req.user.courses.find(function(course) {
if(course._id === req.params.courseID) return true;
});
if(!courseInToken.admin) return res.status(401).json({message: "only admin can alter course"});
box.files.delete(req.params.lessonID, function(err) {
if(err) {
console.log("box error: ", err);
return res.status(500).json('Internal Server Error');
}
res.status(200).json("Lesson " + req.params.lessonID + " deleted");
})
}
)
module.exports = router