2016-03-30 16:57:19 -04:00
|
|
|
_ = require 'lodash'
|
|
|
|
utils = require '../lib/utils'
|
|
|
|
errors = require '../commons/errors'
|
2016-05-20 17:52:04 -04:00
|
|
|
schemas = require '../../app/schemas/schemas'
|
2016-03-30 16:57:19 -04:00
|
|
|
wrap = require 'co-express'
|
2016-05-26 15:35:46 -04:00
|
|
|
log = require 'winston'
|
2016-03-30 16:57:19 -04:00
|
|
|
Promise = require 'bluebird'
|
|
|
|
database = require '../commons/database'
|
|
|
|
mongoose = require 'mongoose'
|
2016-04-06 13:56:06 -04:00
|
|
|
Classroom = require '../models/Classroom'
|
2016-04-13 12:54:24 -04:00
|
|
|
Course = require '../models/Course'
|
|
|
|
Campaign = require '../models/Campaign'
|
|
|
|
Level = require '../models/Level'
|
2016-03-30 16:57:19 -04:00
|
|
|
parse = require '../commons/parse'
|
2016-04-06 13:56:06 -04:00
|
|
|
LevelSession = require '../models/LevelSession'
|
|
|
|
User = require '../models/User'
|
2016-05-13 13:55:22 -04:00
|
|
|
CourseInstance = require '../models/CourseInstance'
|
2016-05-06 16:28:19 -04:00
|
|
|
TrialRequest = require '../models/TrialRequest'
|
|
|
|
sendwithus = require '../sendwithus'
|
2016-07-21 18:15:59 -04:00
|
|
|
co = require 'co'
|
2016-03-30 16:57:19 -04:00
|
|
|
|
|
|
|
module.exports =
|
2016-05-11 17:39:26 -04:00
|
|
|
fetchByCode: wrap (req, res, next) ->
|
|
|
|
code = req.query.code
|
2016-08-01 19:40:48 -04:00
|
|
|
return next() unless req.query.hasOwnProperty('code')
|
2016-06-17 18:15:13 -04:00
|
|
|
classroom = yield Classroom.findOne({ code: code.toLowerCase().replace(RegExp(' ', 'g') , '') }).select('name ownerID aceConfig')
|
2016-05-11 17:39:26 -04:00
|
|
|
if not classroom
|
2016-05-26 15:35:46 -04:00
|
|
|
log.debug("classrooms.fetchByCode: Couldn't find Classroom with code: #{code}")
|
2016-05-25 12:04:35 -04:00
|
|
|
throw new errors.NotFound('Classroom not found.')
|
2016-05-11 17:39:26 -04:00
|
|
|
classroom = classroom.toObject()
|
|
|
|
# Tack on the teacher's name for display to the user
|
|
|
|
owner = (yield User.findOne({ _id: mongoose.Types.ObjectId(classroom.ownerID) }).select('name')).toObject()
|
|
|
|
res.status(200).send({ data: classroom, owner } )
|
|
|
|
|
2016-03-30 16:57:19 -04:00
|
|
|
getByOwner: wrap (req, res, next) ->
|
|
|
|
options = req.query
|
|
|
|
ownerID = options.ownerID
|
|
|
|
return next() unless ownerID
|
|
|
|
throw new errors.UnprocessableEntity('Bad ownerID') unless utils.isID ownerID
|
|
|
|
throw new errors.Unauthorized() unless req.user
|
2016-05-26 15:35:46 -04:00
|
|
|
unless req.user.isAdmin() or ownerID is req.user.id
|
|
|
|
log.debug("classrooms.getByOwner: Can't fetch classroom you don't own. User: #{req.user.id} Owner: #{ownerID}")
|
|
|
|
throw new errors.Forbidden('"ownerID" must be yourself')
|
2016-03-30 16:57:19 -04:00
|
|
|
sanitizedOptions = {}
|
|
|
|
unless _.isUndefined(options.archived)
|
|
|
|
# Handles when .archived is true, vs false-or-null
|
|
|
|
sanitizedOptions.archived = { $ne: not (options.archived is 'true') }
|
|
|
|
dbq = Classroom.find _.merge sanitizedOptions, { ownerID: mongoose.Types.ObjectId(ownerID) }
|
|
|
|
dbq.select(parse.getProjectFromReq(req))
|
|
|
|
classrooms = yield dbq
|
|
|
|
classrooms = (classroom.toObject({req: req}) for classroom in classrooms)
|
|
|
|
res.status(200).send(classrooms)
|
|
|
|
|
2016-04-13 12:54:24 -04:00
|
|
|
fetchAllLevels: wrap (req, res, next) ->
|
|
|
|
classroom = yield database.getDocFromHandle(req, Classroom)
|
|
|
|
if not classroom
|
|
|
|
throw new errors.NotFound('Classroom not found.')
|
|
|
|
|
|
|
|
levelOriginals = []
|
|
|
|
for course in classroom.get('courses') or []
|
|
|
|
for level in course.levels
|
|
|
|
levelOriginals.push(level.original)
|
|
|
|
|
|
|
|
levels = yield Level.find({ original: { $in: levelOriginals }, slug: { $exists: true }}).select(parse.getProjectFromReq(req))
|
|
|
|
levels = (level.toObject({ req: req }) for level in levels)
|
|
|
|
|
|
|
|
# maintain course order
|
|
|
|
levelMap = {}
|
|
|
|
for level in levels
|
|
|
|
levelMap[level.original] = level
|
|
|
|
levels = (levelMap[levelOriginal.toString()] for levelOriginal in levelOriginals)
|
|
|
|
|
2016-05-13 13:55:22 -04:00
|
|
|
res.status(200).send(_.filter(levels)) # for dev server where not all levels will be found
|
2016-04-13 12:54:24 -04:00
|
|
|
|
|
|
|
fetchLevelsForCourse: wrap (req, res) ->
|
|
|
|
classroom = yield database.getDocFromHandle(req, Classroom)
|
|
|
|
if not classroom
|
|
|
|
throw new errors.NotFound('Classroom not found.')
|
|
|
|
|
|
|
|
levelOriginals = []
|
|
|
|
for course in classroom.get('courses') or []
|
|
|
|
if course._id.toString() isnt req.params.courseID
|
|
|
|
continue
|
|
|
|
for level in course.levels
|
|
|
|
levelOriginals.push(level.original)
|
|
|
|
|
|
|
|
levels = yield Level.find({ original: { $in: levelOriginals }, slug: { $exists: true }}).select(parse.getProjectFromReq(req))
|
|
|
|
levels = (level.toObject({ req: req }) for level in levels)
|
|
|
|
|
|
|
|
# maintain course order
|
|
|
|
levelMap = {}
|
|
|
|
for level in levels
|
|
|
|
levelMap[level.original] = level
|
|
|
|
levels = (levelMap[levelOriginal.toString()] for levelOriginal in levelOriginals)
|
|
|
|
|
|
|
|
res.status(200).send(levels)
|
|
|
|
|
2016-03-30 16:57:19 -04:00
|
|
|
fetchMemberSessions: wrap (req, res, next) ->
|
|
|
|
throw new errors.Unauthorized() unless req.user
|
|
|
|
memberLimit = parse.getLimitFromReq(req, {default: 10, max: 100, param: 'memberLimit'})
|
|
|
|
memberSkip = parse.getSkipFromReq(req, {param: 'memberSkip'})
|
|
|
|
classroom = yield database.getDocFromHandle(req, Classroom)
|
|
|
|
throw new errors.NotFound('Classroom not found.') if not classroom
|
|
|
|
throw new errors.Forbidden('You do not own this classroom.') unless req.user.isAdmin() or classroom.get('ownerID').equals(req.user._id)
|
|
|
|
members = classroom.get('members') or []
|
2016-03-30 19:20:37 -04:00
|
|
|
members = members.slice(memberSkip, memberSkip + memberLimit)
|
2016-03-30 16:57:19 -04:00
|
|
|
dbqs = []
|
2016-08-04 21:37:01 -04:00
|
|
|
select = 'state.complete level creator playtime changed created dateFirstCompleted submitted'
|
2016-03-30 16:57:19 -04:00
|
|
|
for member in members
|
2016-03-30 19:20:37 -04:00
|
|
|
dbqs.push(LevelSession.find({creator: member.toHexString()}).select(select).exec())
|
2016-03-30 16:57:19 -04:00
|
|
|
results = yield dbqs
|
|
|
|
sessions = _.flatten(results)
|
|
|
|
res.status(200).send(sessions)
|
2016-08-04 21:37:01 -04:00
|
|
|
|
2016-03-30 16:57:19 -04:00
|
|
|
fetchMembers: wrap (req, res, next) ->
|
|
|
|
throw new errors.Unauthorized() unless req.user
|
|
|
|
memberLimit = parse.getLimitFromReq(req, {default: 10, max: 100, param: 'memberLimit'})
|
|
|
|
memberSkip = parse.getSkipFromReq(req, {param: 'memberSkip'})
|
|
|
|
classroom = yield database.getDocFromHandle(req, Classroom)
|
|
|
|
throw new errors.NotFound('Classroom not found.') if not classroom
|
2016-03-30 18:55:20 -04:00
|
|
|
isOwner = classroom.get('ownerID').equals(req.user._id)
|
|
|
|
isMember = req.user.id in (m.toString() for m in classroom.get('members'))
|
|
|
|
unless req.user.isAdmin() or isOwner or isMember
|
2016-05-26 15:35:46 -04:00
|
|
|
log.debug "classrooms.fetchMembers: Can't fetch members for class (#{classroom.id}) you (#{req.user.id}) don't own and aren't a member of."
|
2016-03-30 18:55:20 -04:00
|
|
|
throw new errors.Forbidden('You do not own this classroom.')
|
2016-03-30 16:57:19 -04:00
|
|
|
memberIDs = classroom.get('members') or []
|
2016-03-30 19:20:37 -04:00
|
|
|
memberIDs = memberIDs.slice(memberSkip, memberSkip + memberLimit)
|
2016-03-30 16:57:19 -04:00
|
|
|
|
|
|
|
members = yield User.find({ _id: { $in: memberIDs }}).select(parse.getProjectFromReq(req))
|
2016-04-07 17:55:42 -04:00
|
|
|
# members = yield User.find({ _id: { $in: memberIDs }, deleted: { $ne: true }}).select(parse.getProjectFromReq(req))
|
2016-03-30 16:57:19 -04:00
|
|
|
memberObjects = (member.toObject({ req: req, includedPrivates: ["name", "email"] }) for member in members)
|
|
|
|
|
|
|
|
res.status(200).send(memberObjects)
|
2016-04-13 14:39:17 -04:00
|
|
|
|
|
|
|
post: wrap (req, res) ->
|
|
|
|
throw new errors.Unauthorized() unless req.user and not req.user.isAnonymous()
|
2016-05-26 15:35:46 -04:00
|
|
|
unless req.user?.isTeacher()
|
2016-05-26 19:15:09 -04:00
|
|
|
log.debug "classrooms.post: Can't create classroom if you (#{req.user?.id}) aren't a teacher."
|
2016-05-26 15:35:46 -04:00
|
|
|
throw new errors.Forbidden()
|
2016-04-13 14:39:17 -04:00
|
|
|
classroom = database.initDoc(req, Classroom)
|
|
|
|
classroom.set 'ownerID', req.user._id
|
|
|
|
classroom.set 'members', []
|
|
|
|
database.assignBody(req, classroom)
|
2016-07-15 16:28:35 -04:00
|
|
|
|
2016-06-19 23:23:32 -04:00
|
|
|
# Copy over data from how courses are right now
|
2016-07-21 18:15:59 -04:00
|
|
|
coursesData = yield module.exports.generateCoursesData(req)
|
|
|
|
classroom.set('courses', coursesData)
|
|
|
|
|
|
|
|
# finish
|
|
|
|
database.validateDoc(classroom)
|
|
|
|
classroom = yield classroom.save()
|
|
|
|
res.status(201).send(classroom.toObject({req: req}))
|
|
|
|
|
|
|
|
updateCourses: wrap (req, res) ->
|
|
|
|
throw new errors.Unauthorized() unless req.user and not req.user.isAnonymous()
|
|
|
|
classroom = yield database.getDocFromHandle(req, Classroom)
|
|
|
|
if not classroom
|
|
|
|
throw new errors.NotFound('Classroom not found.')
|
|
|
|
unless req.user._id.equals(classroom.get('ownerID'))
|
|
|
|
throw new errors.Forbidden('Only the owner may update their classroom content')
|
|
|
|
|
|
|
|
coursesData = yield module.exports.generateCoursesData(req)
|
|
|
|
classroom.set('courses', coursesData)
|
|
|
|
classroom = yield classroom.save()
|
|
|
|
res.status(200).send(classroom.toObject({req: req}))
|
|
|
|
|
|
|
|
generateCoursesData: co.wrap (req) ->
|
|
|
|
# helper function for generating the latest version of courses
|
2016-07-15 16:28:35 -04:00
|
|
|
query = {}
|
2016-07-22 19:37:53 -04:00
|
|
|
query = {releasePhase: 'released'} unless req.user?.isAdmin()
|
2016-07-15 16:28:35 -04:00
|
|
|
courses = yield Course.find(query)
|
2016-07-16 03:35:52 -04:00
|
|
|
courses = Course.sortCourses courses
|
2016-04-13 12:54:24 -04:00
|
|
|
campaigns = yield Campaign.find({_id: {$in: (course.get('campaignID') for course in courses)}})
|
|
|
|
campaignMap = {}
|
|
|
|
campaignMap[campaign.id] = campaign for campaign in campaigns
|
|
|
|
coursesData = []
|
|
|
|
for course in courses
|
|
|
|
courseData = { _id: course._id, levels: [] }
|
|
|
|
campaign = campaignMap[course.get('campaignID').toString()]
|
|
|
|
levels = _.values(campaign.get('levels'))
|
|
|
|
levels = _.sortBy(levels, 'campaignIndex')
|
|
|
|
for level in levels
|
|
|
|
levelData = { original: mongoose.Types.ObjectId(level.original) }
|
2016-07-14 18:48:42 -04:00
|
|
|
_.extend(levelData, _.pick(level, 'type', 'slug', 'name', 'practice', 'practiceThresholdMinutes', 'shareable'))
|
2016-04-13 12:54:24 -04:00
|
|
|
courseData.levels.push(levelData)
|
|
|
|
coursesData.push(courseData)
|
2016-07-21 18:15:59 -04:00
|
|
|
return coursesData
|
2016-05-13 13:55:22 -04:00
|
|
|
|
|
|
|
join: wrap (req, res) ->
|
|
|
|
unless req.body?.code
|
|
|
|
throw new errors.UnprocessableEntity('Need a code')
|
|
|
|
if req.user.isTeacher()
|
2016-05-26 15:35:46 -04:00
|
|
|
log.debug("classrooms.join: Cannot join a classroom as a teacher: #{req.user.id}")
|
2016-05-13 13:55:22 -04:00
|
|
|
throw new errors.Forbidden('Cannot join a classroom as a teacher')
|
2016-06-17 18:15:13 -04:00
|
|
|
code = req.body.code.toLowerCase().replace(RegExp(' ', 'g'), '')
|
2016-05-13 13:55:22 -04:00
|
|
|
classroom = yield Classroom.findOne({code: code})
|
|
|
|
if not classroom
|
2016-05-26 15:35:46 -04:00
|
|
|
log.debug("classrooms.join: Classroom not found with code #{code}")
|
|
|
|
throw new errors.NotFound("Classroom not found with code #{code}")
|
2016-05-13 13:55:22 -04:00
|
|
|
members = _.clone(classroom.get('members'))
|
|
|
|
if _.any(members, (memberID) -> memberID.equals(req.user._id))
|
|
|
|
return res.send(classroom.toObject({req: req}))
|
|
|
|
update = { $push: { members : req.user._id }}
|
|
|
|
yield classroom.update(update)
|
|
|
|
members.push req.user._id
|
|
|
|
classroom.set('members', members)
|
|
|
|
|
|
|
|
# make user role student
|
|
|
|
if not req.user.get('role')
|
|
|
|
req.user.set('role', 'student')
|
|
|
|
yield req.user.save()
|
|
|
|
|
|
|
|
# join any course instances for free courses in the classroom
|
|
|
|
courseIDs = (course._id for course in classroom.get('courses'))
|
|
|
|
courses = yield Course.find({_id: {$in: courseIDs}, free: true})
|
|
|
|
freeCourseIDs = (course._id for course in courses)
|
|
|
|
freeCourseInstances = yield CourseInstance.find({ classroomID: classroom._id, courseID: {$in: freeCourseIDs} }).select('_id')
|
|
|
|
freeCourseInstanceIDs = (courseInstance._id for courseInstance in freeCourseInstances)
|
|
|
|
yield CourseInstance.update({_id: {$in: freeCourseInstanceIDs}}, { $addToSet: { members: req.user._id }})
|
|
|
|
yield User.update({ _id: req.user._id }, { $addToSet: { courseInstances: { $each: freeCourseInstanceIDs } } })
|
|
|
|
res.send(classroom.toObject({req: req}))
|
2016-05-20 17:52:04 -04:00
|
|
|
|
|
|
|
setStudentPassword: wrap (req, res, next) ->
|
|
|
|
newPassword = req.body.password
|
|
|
|
{ classroomID, memberID } = req.params
|
|
|
|
teacherID = req.user.id
|
|
|
|
return next() if teacherID is memberID or not newPassword
|
|
|
|
ownedClassrooms = yield Classroom.find({ ownerID: mongoose.Types.ObjectId(teacherID) })
|
|
|
|
ownedStudentIDs = _.flatten ownedClassrooms.map (c) ->
|
|
|
|
c.get('members').map (id) ->
|
|
|
|
id.toString()
|
2016-05-26 18:49:33 -04:00
|
|
|
unless memberID in ownedStudentIDs
|
|
|
|
throw new errors.Forbidden("Can't reset the password of a student that's not in one of your classrooms.")
|
2016-05-20 17:52:04 -04:00
|
|
|
student = yield User.findById(memberID)
|
|
|
|
if student.get('emailVerified')
|
2016-05-26 15:35:46 -04:00
|
|
|
log.debug "classrooms.setStudentPassword: Can't reset password for a student (#{memberID}) that has verified their email address."
|
|
|
|
throw new errors.Forbidden("Can't reset password for a student that has verified their email address.")
|
2016-05-20 17:52:04 -04:00
|
|
|
{ valid, error } = tv4.validateResult(newPassword, schemas.passwordString)
|
|
|
|
unless valid
|
|
|
|
throw new errors.UnprocessableEntity(error.message)
|
|
|
|
yield student.update({ $set: { passwordHash: User.hashPassword(newPassword) } })
|
|
|
|
res.status(200).send({})
|
2016-06-02 13:20:19 -04:00
|
|
|
|
2016-05-06 16:28:19 -04:00
|
|
|
inviteMembers: wrap (req, res) ->
|
|
|
|
if not req.body.emails
|
2016-06-06 16:43:41 -04:00
|
|
|
log.debug "classrooms.inviteMembers: No emails included in request: #{JSON.stringify(req.body)}"
|
2016-05-06 16:28:19 -04:00
|
|
|
throw new errors.UnprocessableEntity('Emails not included')
|
|
|
|
|
|
|
|
classroom = yield database.getDocFromHandle(req, Classroom)
|
|
|
|
if not classroom
|
|
|
|
throw new errors.NotFound('Classroom not found.')
|
2016-06-06 16:43:41 -04:00
|
|
|
|
2016-05-06 16:28:19 -04:00
|
|
|
unless classroom.get('ownerID').equals(req.user?._id)
|
2016-06-06 16:43:41 -04:00
|
|
|
log.debug "classroom_handler.inviteMembers: Can't invite to classroom (#{classroom.id}) you (#{req.user.get('_id')}) don't own"
|
2016-05-06 16:28:19 -04:00
|
|
|
throw new errors.Forbidden('Must be owner of classroom to send invites.')
|
|
|
|
|
|
|
|
for email in req.body.emails
|
2016-06-02 13:27:20 -04:00
|
|
|
joinCode = (classroom.get('codeCamel') or classroom.get('code'))
|
2016-05-06 16:28:19 -04:00
|
|
|
context =
|
2016-06-06 19:53:05 -04:00
|
|
|
email_id: sendwithus.templates.course_invite_email
|
2016-05-06 16:28:19 -04:00
|
|
|
recipient:
|
|
|
|
address: email
|
|
|
|
email_data:
|
2016-06-02 13:27:20 -04:00
|
|
|
teacher_name: req.user.broadName()
|
2016-05-06 16:28:19 -04:00
|
|
|
class_name: classroom.get('name')
|
2016-06-02 13:27:20 -04:00
|
|
|
join_link: "https://codecombat.com/courses?_cc=" + joinCode
|
|
|
|
join_code: joinCode
|
2016-05-06 16:28:19 -04:00
|
|
|
sendwithus.api.send context, _.noop
|
2016-06-02 13:27:20 -04:00
|
|
|
|
2016-05-06 16:28:19 -04:00
|
|
|
res.status(200).send({})
|
2016-06-21 12:29:41 -04:00
|
|
|
|
|
|
|
getUsers: wrap (req, res, next) ->
|
|
|
|
throw new errors.Unauthorized('You must be an administrator.') unless req.user?.isAdmin()
|
|
|
|
classrooms = yield Classroom.find().select('ownerID members').lean()
|
|
|
|
res.status(200).send(classrooms)
|