Switch POST /db/classroom/:id/invite-members to refactored version

This commit is contained in:
Scott Erickson 2016-06-02 10:27:20 -07:00
parent 6e48d700bc
commit 5e8b402c3d
4 changed files with 7 additions and 40 deletions

View file

@ -28,8 +28,6 @@ ClassroomHandler = class ClassroomHandler extends Handler
false false
getByRelationship: (req, res, args...) -> getByRelationship: (req, res, args...) ->
method = req.method.toLowerCase()
return @inviteStudents(req, res, args[0]) if args[1] is 'invite-members'
return @removeMember(req, res, args[0]) if req.method is 'DELETE' and args[1] is 'members' return @removeMember(req, res, args[0]) if req.method is 'DELETE' and args[1] is 'members'
return @getMembersAPI(req, res, args[0]) if args[1] is 'members' return @getMembersAPI(req, res, args[0]) if args[1] is 'members'
super(arguments...) super(arguments...)
@ -68,32 +66,6 @@ ClassroomHandler = class ClassroomHandler extends Handler
return doc.toObject() return doc.toObject()
return _.omit(doc.toObject(), 'code', 'codeCamel') return _.omit(doc.toObject(), 'code', 'codeCamel')
inviteStudents: (req, res, classroomID) ->
return @sendUnauthorizedError(res) if not req.user?
if not req.body.emails
return @sendBadInputError(res, 'Emails not included')
Classroom.findById classroomID, (err, classroom) =>
return @sendDatabaseError(res, err) if err
return @sendNotFoundError(res) unless classroom
unless classroom.get('ownerID').equals(req.user.get('_id'))
log.debug "classroom_handler.inviteStudents: Can't invite to classroom (#{classroom.id}) you (#{req.user.get('_id')}) don't own"
return @sendForbiddenError(res)
for email in req.body.emails
joinCode = (classroom.get('codeCamel') or classroom.get('code'))
context =
email_id: sendwithus.templates.course_invite_email
recipient:
address: email
email_data:
teacher_name: req.user.broadName()
class_name: classroom.get('name')
join_link: "https://codecombat.com/courses?_cc=" + joinCode
join_code: joinCode
sendwithus.api.send context, _.noop
return @sendSuccess(res, {})
get: (req, res) -> get: (req, res) ->
if ownerID = req.query.ownerID if ownerID = req.query.ownerID
unless req.user and (req.user.isAdmin() or ownerID is req.user.id) unless req.user and (req.user.isAdmin() or ownerID is req.user.id)

View file

@ -228,23 +228,20 @@ module.exports =
throw new errors.NotFound('Classroom not found.') throw new errors.NotFound('Classroom not found.')
unless classroom.get('ownerID').equals(req.user?._id) unless classroom.get('ownerID').equals(req.user?._id)
log.debug "classroom_handler.inviteStudents: Can't invite to classroom (#{classroom.id}) you (#{req.user.get('_id')}) don't own"
throw new errors.Forbidden('Must be owner of classroom to send invites.') throw new errors.Forbidden('Must be owner of classroom to send invites.')
user = req.user
teacherName = user.get('name')
teacherName ?= _.filter([user.get('firstName'), user.get('lastName')]).join(' ')
trialRequest = yield TrialRequest.findOne({applicant: user._id})
schoolName = trialRequest?.get('properties')?.organization
for email in req.body.emails for email in req.body.emails
joinCode = (classroom.get('codeCamel') or classroom.get('code'))
context = context =
email_id: sendwithus.templates.course_invite_email email_id: sendwithus.templates.course_invite_email
recipient: recipient:
address: email address: email
email_data: email_data:
teacher_name: req.user.broadName()
class_name: classroom.get('name') class_name: classroom.get('name')
teacher_name: teacherName join_link: "https://codecombat.com/courses?_cc=" + joinCode
school_name: schoolName join_code: joinCode
join_link: "https://codecombat.com/courses?_cc=" + (classroom.get('codeCamel') or classroom.get('code'))
sendwithus.api.send context, _.noop sendwithus.api.send context, _.noop
res.status(200).send({}) res.status(200).send({})

View file

@ -60,7 +60,7 @@ module.exports.setup = (app) ->
app.get('/db/classroom', mw.classrooms.fetchByCode, mw.classrooms.getByOwner) app.get('/db/classroom', mw.classrooms.fetchByCode, mw.classrooms.getByOwner)
app.get('/db/classroom/:handle/levels', mw.classrooms.fetchAllLevels) app.get('/db/classroom/:handle/levels', mw.classrooms.fetchAllLevels)
app.get('/db/classroom/:handle/courses/:courseID/levels', mw.classrooms.fetchLevelsForCourse) app.get('/db/classroom/:handle/courses/:courseID/levels', mw.classrooms.fetchLevelsForCourse)
# app.post('/db/classroom/:handle/invite-members', mw.classrooms.inviteMembers) app.post('/db/classroom/:handle/invite-members', mw.classrooms.inviteMembers)
app.get('/db/classroom/:handle/member-sessions', mw.classrooms.fetchMemberSessions) app.get('/db/classroom/:handle/member-sessions', mw.classrooms.fetchMemberSessions)
app.get('/db/classroom/:handle/members', mw.classrooms.fetchMembers) # TODO: Use mw.auth? app.get('/db/classroom/:handle/members', mw.classrooms.fetchMembers) # TODO: Use mw.auth?
app.post('/db/classroom/:classroomID/members/:memberID/reset-password', mw.classrooms.setStudentPassword) app.post('/db/classroom/:classroomID/members/:memberID/reset-password', mw.classrooms.setStudentPassword)

View file

@ -346,7 +346,6 @@ describe 'POST /db/classroom/:id/invite-members', ->
it 'takes a list of emails and sends invites', utils.wrap (done) -> it 'takes a list of emails and sends invites', utils.wrap (done) ->
user = yield utils.initUser({role: 'teacher', name: 'Mr Professerson'}) user = yield utils.initUser({role: 'teacher', name: 'Mr Professerson'})
yield utils.loginUser(user) yield utils.loginUser(user)
yield utils.makeTrialRequest({ properties: { organization: 'Greendale' } })
classroom = yield utils.makeClassroom() classroom = yield utils.makeClassroom()
url = classroomsURL + "/#{classroom.id}/invite-members" url = classroomsURL + "/#{classroom.id}/invite-members"
data = { emails: ['test@test.com'] } data = { emails: ['test@test.com'] }
@ -355,7 +354,6 @@ describe 'POST /db/classroom/:id/invite-members', ->
expect(context.email_id).toBe(sendwithus.templates.course_invite_email) expect(context.email_id).toBe(sendwithus.templates.course_invite_email)
expect(context.recipient.address).toBe('test@test.com') expect(context.recipient.address).toBe('test@test.com')
expect(context.email_data.teacher_name).toBe('Mr Professerson') expect(context.email_data.teacher_name).toBe('Mr Professerson')
expect(context.email_data.school_name).toBe('Greendale')
done() done()
[res, body] = yield request.postAsync { uri: url, json: data } [res, body] = yield request.postAsync { uri: url, json: data }
expect(res.statusCode).toBe(200) expect(res.statusCode).toBe(200)