2015-09-03 14:04:40 -04:00
|
|
|
async = require 'async'
|
2015-08-29 10:15:35 -04:00
|
|
|
Handler = require '../commons/Handler'
|
2015-10-07 19:58:59 -04:00
|
|
|
Campaign = require '../campaigns/Campaign'
|
2015-09-03 14:04:40 -04:00
|
|
|
Course = require './Course'
|
2015-08-29 10:15:35 -04:00
|
|
|
CourseInstance = require './CourseInstance'
|
2015-09-13 01:01:59 -04:00
|
|
|
LevelSession = require '../levels/sessions/LevelSession'
|
|
|
|
LevelSessionHandler = require '../levels/sessions/level_session_handler'
|
2015-09-03 14:04:40 -04:00
|
|
|
Prepaid = require '../prepaids/Prepaid'
|
2015-10-01 18:23:20 -04:00
|
|
|
PrepaidHandler = require '../prepaids/prepaid_handler'
|
2015-09-13 01:01:59 -04:00
|
|
|
User = require '../users/User'
|
|
|
|
UserHandler = require '../users/user_handler'
|
2015-10-01 18:23:20 -04:00
|
|
|
utils = require '../../app/core/utils'
|
2015-10-05 19:00:47 -04:00
|
|
|
sendwithus = require '../sendwithus'
|
2015-08-29 10:15:35 -04:00
|
|
|
|
|
|
|
CourseInstanceHandler = class CourseInstanceHandler extends Handler
|
|
|
|
modelClass: CourseInstance
|
|
|
|
jsonSchema: require '../../app/schemas/models/course_instance.schema'
|
|
|
|
allowedMethods: ['GET', 'POST', 'PUT', 'DELETE']
|
|
|
|
|
2015-09-03 14:04:40 -04:00
|
|
|
logError: (user, msg) ->
|
2015-10-01 18:23:20 -04:00
|
|
|
console.warn "Course instance error: #{user.get('slug')} (#{user._id}): '#{msg}'"
|
2015-09-03 14:04:40 -04:00
|
|
|
|
2015-08-29 10:15:35 -04:00
|
|
|
hasAccess: (req) ->
|
2015-09-13 01:01:59 -04:00
|
|
|
req.method in @allowedMethods or req.user?.isAdmin()
|
2015-08-29 10:15:35 -04:00
|
|
|
|
2015-09-03 14:04:40 -04:00
|
|
|
hasAccessToDocument: (req, document, method=null) ->
|
2015-09-13 01:01:59 -04:00
|
|
|
return true if document?.get('ownerID')?.equals(req.user?.get('_id'))
|
|
|
|
return true if req.method is 'GET' and _.find document?.get('members'), (a) -> a.equals(req.user?.get('_id'))
|
2015-09-03 14:04:40 -04:00
|
|
|
req.user?.isAdmin()
|
|
|
|
|
|
|
|
getByRelationship: (req, res, args...) ->
|
|
|
|
relationship = args[1]
|
|
|
|
return @createAPI(req, res) if relationship is 'create'
|
2015-09-13 01:01:59 -04:00
|
|
|
return @getLevelSessionsAPI(req, res, args[0]) if args[1] is 'level_sessions'
|
|
|
|
return @getMembersAPI(req, res, args[0]) if args[1] is 'members'
|
2015-10-05 19:00:47 -04:00
|
|
|
return @inviteStudents(req, res, args[0]) if relationship is 'invite_students'
|
2015-10-06 14:20:53 -04:00
|
|
|
return @redeemPrepaidCodeAPI(req, res) if args[1] is 'redeem_prepaid'
|
2015-09-03 14:04:40 -04:00
|
|
|
super arguments...
|
|
|
|
|
|
|
|
createAPI: (req, res) ->
|
2015-10-01 18:23:20 -04:00
|
|
|
return @sendUnauthorizedError(res) if not req.user? or req.user?.isAnonymous()
|
2015-09-03 14:04:40 -04:00
|
|
|
|
|
|
|
# Required Input
|
|
|
|
seats = req.body.seats
|
|
|
|
unless seats > 0
|
|
|
|
@logError(req.user, 'Course create API missing required seats count')
|
|
|
|
return @sendBadInputError(res, 'Missing required seats count')
|
|
|
|
# Optional - unspecified means create instances for all courses
|
|
|
|
courseID = req.body.courseID
|
|
|
|
# Optional
|
|
|
|
name = req.body.name
|
|
|
|
# Optional - as long as course(s) are all free
|
2015-10-01 18:23:20 -04:00
|
|
|
stripeToken = req.body.stripe?.token
|
2015-09-03 14:04:40 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
query = if courseID? then {_id: courseID} else {}
|
|
|
|
Course.find query, (err, courses) =>
|
2015-09-03 14:04:40 -04:00
|
|
|
if err
|
2015-10-01 18:23:20 -04:00
|
|
|
@logError(user, "Find courses error: #{JSON.stringify(err)}")
|
|
|
|
return done(err)
|
2015-09-03 14:04:40 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
PrepaidHandler.purchasePrepaidCourse req.user, courses, seats, new Date().getTime(), stripeToken, (err, prepaid) =>
|
|
|
|
if err
|
|
|
|
@logError(req.user, err)
|
|
|
|
return @sendBadInputError(res, err) if err is 'Missing required Stripe token'
|
|
|
|
return @sendDatabaseError(res, err)
|
2015-09-03 14:04:40 -04:00
|
|
|
|
2015-10-01 18:23:20 -04:00
|
|
|
courseInstances = []
|
|
|
|
makeCreateInstanceFn = (course, name, prepaid) =>
|
|
|
|
(done) =>
|
|
|
|
@createInstance req, course, name, prepaid, (err, newInstance)=>
|
|
|
|
courseInstances.push newInstance unless err
|
|
|
|
done(err)
|
|
|
|
tasks = (makeCreateInstanceFn(course, name, prepaid) for course in courses)
|
|
|
|
async.parallel tasks, (err, results) =>
|
2015-09-03 14:04:40 -04:00
|
|
|
return @sendDatabaseError(res, err) if err
|
2015-10-01 18:23:20 -04:00
|
|
|
@sendCreated(res, courseInstances)
|
2015-09-03 14:04:40 -04:00
|
|
|
|
|
|
|
createInstance: (req, course, name, prepaid, done) =>
|
|
|
|
courseInstance = new CourseInstance
|
|
|
|
courseID: course.get('_id')
|
|
|
|
members: [req.user.get('_id')]
|
|
|
|
name: name
|
|
|
|
ownerID: req.user.get('_id')
|
|
|
|
prepaidID: prepaid.get('_id')
|
|
|
|
courseInstance.save (err, newInstance) =>
|
|
|
|
done(err, newInstance)
|
|
|
|
|
2015-09-13 01:01:59 -04:00
|
|
|
getLevelSessionsAPI: (req, res, courseInstanceID) ->
|
|
|
|
CourseInstance.findById courseInstanceID, (err, courseInstance) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
return @sendNotFoundError(res) unless courseInstance
|
2015-10-07 19:58:59 -04:00
|
|
|
Course.findById courseInstance.get('courseID'), (err, course) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
return @sendNotFoundError(res) unless course
|
|
|
|
Campaign.findById course.get('campaignID'), (err, campaign) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
return @sendNotFoundError(res) unless campaign
|
|
|
|
levelIDs = (levelID for levelID of campaign.get('levels'))
|
|
|
|
memberIDs = _.map courseInstance.get('members') ? [], (memberID) -> memberID.toHexString?() or memberID
|
|
|
|
query = {$and: [{creator: {$in: memberIDs}}, {'level.original': {$in: levelIDs}}]}
|
|
|
|
LevelSession.find query, (err, documents) =>
|
|
|
|
return @sendDatabaseError(res, err) if err?
|
|
|
|
cleandocs = (LevelSessionHandler.formatEntity(req, doc) for doc in documents)
|
|
|
|
@sendSuccess(res, cleandocs)
|
2015-09-13 01:01:59 -04:00
|
|
|
|
|
|
|
getMembersAPI: (req, res, courseInstanceID) ->
|
|
|
|
CourseInstance.findById courseInstanceID, (err, courseInstance) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
return @sendNotFoundError(res) unless courseInstance
|
|
|
|
memberIDs = courseInstance.get('members') ? []
|
|
|
|
User.find {_id: {$in: memberIDs}}, (err, users) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
cleandocs = (UserHandler.formatEntity(req, doc) for doc in users)
|
|
|
|
@sendSuccess(res, cleandocs)
|
2015-09-03 14:04:40 -04:00
|
|
|
|
2015-10-05 19:00:47 -04:00
|
|
|
inviteStudents: (req, res, courseInstanceID) ->
|
|
|
|
if not req.body.emails
|
|
|
|
return @sendBadInputError(res, 'Emails not included')
|
|
|
|
CourseInstance.findById courseInstanceID, (err, courseInstance) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
return @sendNotFoundError(res) unless courseInstance
|
|
|
|
return @sendForbiddenError(res) unless @hasAccessToDocument(req, courseInstance)
|
|
|
|
|
2015-10-07 20:24:28 -04:00
|
|
|
Course.findById courseInstance.get('courseID'), (err, course) =>
|
2015-10-05 19:00:47 -04:00
|
|
|
return @sendDatabaseError(res, err) if err
|
2015-10-07 20:24:28 -04:00
|
|
|
return @sendNotFoundError(res) unless course
|
|
|
|
|
|
|
|
Prepaid.findById courseInstance.get('prepaidID'), (err, prepaid) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
return @sendNotFoundError(res) unless prepaid
|
|
|
|
return @sendForbiddenError(res) unless prepaid.get('maxRedeemers') > prepaid.get('redeemers').length
|
|
|
|
for email in req.body.emails
|
|
|
|
context =
|
|
|
|
email_id: sendwithus.templates.course_invite_email
|
|
|
|
recipient:
|
|
|
|
address: email
|
|
|
|
subject: course.get('name')
|
|
|
|
email_data:
|
|
|
|
class_name: course.get('name')
|
2015-10-08 08:47:08 -04:00
|
|
|
join_link: "https://codecombat.com/courses/students?_ppc=" + prepaid.get('code')
|
2015-10-07 20:24:28 -04:00
|
|
|
sendwithus.api.send context, _.noop
|
|
|
|
return @sendSuccess(res, {})
|
2015-10-05 19:00:47 -04:00
|
|
|
|
2015-10-06 14:20:53 -04:00
|
|
|
redeemPrepaidCodeAPI: (req, res) ->
|
|
|
|
return @sendUnauthorizedError(res) if not req.user? or req.user?.isAnonymous()
|
|
|
|
return @sendBadInputError(res) unless req.body?.prepaidCode
|
|
|
|
|
|
|
|
prepaidCode = req.body?.prepaidCode
|
|
|
|
Prepaid.find code: prepaidCode, (err, prepaids) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
return @sendNotFoundError(res) if prepaids.length < 1
|
|
|
|
return @sendDatabaseError(res, "Multiple prepaid codes found for #{prepaidCode}") if prepaids.length > 1
|
|
|
|
prepaid = prepaids[0]
|
|
|
|
|
|
|
|
CourseInstance.find prepaidID: prepaid.get('_id'), (err, courseInstances) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
return @sendForbiddenError(res) if prepaid.get('redeemers')?.length >= prepaid.get('maxRedeemers')
|
|
|
|
|
2015-10-07 18:14:26 -04:00
|
|
|
if _.find((prepaid.get('redeemers') ? []), (a) -> a.userID.equals(req.user.id))
|
|
|
|
return @sendSuccess(res, courseInstances)
|
|
|
|
|
2015-10-06 14:20:53 -04:00
|
|
|
# Add to prepaid redeemers
|
|
|
|
query =
|
|
|
|
_id: prepaid.get('_id')
|
|
|
|
'redeemers.userID': { $ne: req.user.get('_id') }
|
|
|
|
$where: "this.redeemers.length < #{prepaid.get('maxRedeemers')}"
|
|
|
|
update = { $push: { redeemers : { date: new Date(), userID: req.user.get('_id') } }}
|
|
|
|
Prepaid.update query, update, (err, nMatched) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
|
|
|
if nMatched is 0
|
|
|
|
@logError(req.user, "Course instance update prepaid lost race on maxRedeemers")
|
|
|
|
return @sendForbiddenError(res)
|
|
|
|
|
|
|
|
# Add to each course instance
|
|
|
|
makeAddMemberToCourseInstanceFn = (courseInstance) =>
|
|
|
|
(done) => courseInstance.update({$addToSet: { members: req.user.get('_id')}}, done)
|
|
|
|
tasks = (makeAddMemberToCourseInstanceFn(courseInstance) for courseInstance in courseInstances)
|
|
|
|
async.parallel tasks, (err, results) =>
|
|
|
|
return @sendDatabaseError(res, err) if err
|
2015-10-06 15:30:14 -04:00
|
|
|
@sendSuccess(res, courseInstances)
|
2015-10-05 19:00:47 -04:00
|
|
|
|
2015-08-29 10:15:35 -04:00
|
|
|
module.exports = new CourseInstanceHandler()
|