mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-01-20 19:29:55 -05:00
Merge branch 'master' into production
This commit is contained in:
commit
0e9f319315
2 changed files with 19 additions and 74 deletions
|
@ -10,62 +10,20 @@ User = require './User'
|
||||||
|
|
||||||
TrialRequestSchema = new mongoose.Schema {}, {strict: false, minimize: false, read:config.mongo.readpref}
|
TrialRequestSchema = new mongoose.Schema {}, {strict: false, minimize: false, read:config.mongo.readpref}
|
||||||
|
|
||||||
TrialRequestSchema.pre 'save', (next) ->
|
|
||||||
return next() unless @get('status') is 'approved'
|
|
||||||
|
|
||||||
# Add 2 course headcount
|
|
||||||
prepaid = new Prepaid
|
|
||||||
creator: @get('applicant')
|
|
||||||
type: 'course'
|
|
||||||
maxRedeemers: 2
|
|
||||||
properties:
|
|
||||||
trialRequestID: @get('_id')
|
|
||||||
prepaid.save (err) =>
|
|
||||||
if err
|
|
||||||
log.error "Trial request prepaid creation error: #{err}"
|
|
||||||
next()
|
|
||||||
|
|
||||||
TrialRequestSchema.post 'save', (doc) ->
|
TrialRequestSchema.post 'save', (doc) ->
|
||||||
if doc.get('status') is 'approved'
|
# Subscribe to teacher news group
|
||||||
unless trialProperties = doc.get('properties')
|
User.findById doc.get('applicant'), (err, user) =>
|
||||||
log.error "Saving approved trial request #{doc.id} with no properties!"
|
if err
|
||||||
|
log.error "Trial request user find error: #{err}"
|
||||||
return
|
return
|
||||||
|
emails = _.cloneDeep(user.get('emails') ? {})
|
||||||
User.findById doc.get('applicant'), (err, user) =>
|
emails.teacherNews ?= {}
|
||||||
|
emails.teacherNews.enabled = true
|
||||||
|
user.update {$set: {emails: emails}}, {}, (err) =>
|
||||||
if err
|
if err
|
||||||
log.error "Trial request user find error: #{err}"
|
log.error "Trial request user update error: #{err}"
|
||||||
return
|
return
|
||||||
|
|
||||||
# Send trial approved email
|
|
||||||
email = trialProperties.email ? user.get('emailLower')
|
|
||||||
emailParams =
|
|
||||||
recipient:
|
|
||||||
address: email
|
|
||||||
email_id: sendwithus.templates.teacher_request_demo
|
|
||||||
email_data:
|
|
||||||
account_exists: user?.get('anonymous') is false
|
|
||||||
classes_exist: false
|
|
||||||
if user?.get('anonymous') is false
|
|
||||||
Classroom.findOne {ownerID: user.get('_id')}, (err, classroom) =>
|
|
||||||
if err
|
|
||||||
log.error "Trial request classroom find error: #{err}"
|
|
||||||
return
|
|
||||||
emailParams.email_data.classes_exist = classroom?
|
|
||||||
sendwithus.api.send emailParams, (err, result) =>
|
|
||||||
log.error "sendwithus trial request approved error: #{err}, result: #{result}" if err
|
|
||||||
else
|
|
||||||
sendwithus.api.send emailParams, (err, result) =>
|
|
||||||
log.error "sendwithus trial request approved error: #{err}, result: #{result}" if err
|
|
||||||
|
|
||||||
# Subscribe to teacher news group
|
|
||||||
emails = _.cloneDeep(user.get('emails') ? {})
|
|
||||||
emails.teacherNews ?= {}
|
|
||||||
emails.teacherNews.enabled = true
|
|
||||||
user.update {$set: {emails: emails}}, {}, (err) =>
|
|
||||||
if err
|
|
||||||
log.error "Trial request user update error: #{err}"
|
|
||||||
return
|
|
||||||
|
|
||||||
TrialRequestSchema.statics.privateProperties = []
|
TrialRequestSchema.statics.privateProperties = []
|
||||||
TrialRequestSchema.statics.editableProperties = [
|
TrialRequestSchema.statics.editableProperties = [
|
||||||
'created'
|
'created'
|
||||||
|
|
|
@ -64,7 +64,7 @@ describe 'POST /db/trial.request', ->
|
||||||
[res, body] = yield request.postAsync(getURL('/db/trial.request'), { json: true })
|
[res, body] = yield request.postAsync(getURL('/db/trial.request'), { json: true })
|
||||||
expect(res.statusCode).toBe(422)
|
expect(res.statusCode).toBe(422)
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'updates an existing TrialRequest if there is one', utils.wrap (done) ->
|
it 'updates an existing TrialRequest if there is one', utils.wrap (done) ->
|
||||||
yield utils.clearModels([User, TrialRequest])
|
yield utils.clearModels([User, TrialRequest])
|
||||||
@user = yield utils.initUser()
|
@user = yield utils.initUser()
|
||||||
|
@ -98,7 +98,7 @@ describe 'GET /db/trial.request', ->
|
||||||
[res, body] = yield request.postAsync(getURL('/db/trial.request'), { json: fixture })
|
[res, body] = yield request.postAsync(getURL('/db/trial.request'), { json: fixture })
|
||||||
@trialRequest = yield TrialRequest.findById(body._id)
|
@trialRequest = yield TrialRequest.findById(body._id)
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'returns 403 to non-admins', utils.wrap (done) ->
|
it 'returns 403 to non-admins', utils.wrap (done) ->
|
||||||
[res, body] = yield request.getAsync(getURL('/db/trial.request'))
|
[res, body] = yield request.getAsync(getURL('/db/trial.request'))
|
||||||
expect(res.statusCode).toEqual(403)
|
expect(res.statusCode).toEqual(403)
|
||||||
|
@ -130,13 +130,13 @@ describe 'GET /db/trial.request?applicant=:userID', ->
|
||||||
expect(body.length).toBe(1)
|
expect(body.length).toBe(1)
|
||||||
expect(body[0]._id).toBe(@trialRequest1.id)
|
expect(body[0]._id).toBe(@trialRequest1.id)
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'returns 403 when non-admins request other people\'s trial requests', utils.wrap (done) ->
|
it 'returns 403 when non-admins request other people\'s trial requests', utils.wrap (done) ->
|
||||||
[res, body] = yield request.getAsync(getURL('/db/trial.request?applicant='+@user2.id), { json: true })
|
[res, body] = yield request.getAsync(getURL('/db/trial.request?applicant='+@user2.id), { json: true })
|
||||||
expect(res.statusCode).toEqual(403)
|
expect(res.statusCode).toEqual(403)
|
||||||
done()
|
done()
|
||||||
|
|
||||||
|
|
||||||
describe 'PUT /db/trial.request/:handle', ->
|
describe 'PUT /db/trial.request/:handle', ->
|
||||||
putURL = null
|
putURL = null
|
||||||
|
|
||||||
|
@ -149,14 +149,14 @@ describe 'PUT /db/trial.request/:handle', ->
|
||||||
@trialRequest = yield TrialRequest.findById(body._id)
|
@trialRequest = yield TrialRequest.findById(body._id)
|
||||||
putURL = getURL('/db/trial.request/'+@trialRequest.id)
|
putURL = getURL('/db/trial.request/'+@trialRequest.id)
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'returns 403 to non-admins', ->
|
it 'returns 403 to non-admins', ->
|
||||||
[res, body] = yield request.putAsync(getURL("/db/trial.request/#{@trialRequest.id}"))
|
[res, body] = yield request.putAsync(getURL("/db/trial.request/#{@trialRequest.id}"))
|
||||||
expect(res.statusCode).toEqual(403)
|
expect(res.statusCode).toEqual(403)
|
||||||
done()
|
done()
|
||||||
|
|
||||||
describe 'set status to "approved"', ->
|
describe 'set status to "approved"', ->
|
||||||
|
|
||||||
beforeEach utils.wrap (done) ->
|
beforeEach utils.wrap (done) ->
|
||||||
@admin = yield utils.initAdmin()
|
@admin = yield utils.initAdmin()
|
||||||
yield utils.loginUser(@admin)
|
yield utils.loginUser(@admin)
|
||||||
|
@ -164,23 +164,14 @@ describe 'PUT /db/trial.request/:handle', ->
|
||||||
expect(res.statusCode).toBe(200)
|
expect(res.statusCode).toBe(200)
|
||||||
expect(body.status).toBe('approved')
|
expect(body.status).toBe('approved')
|
||||||
setTimeout done, 10 # let changes propagate
|
setTimeout done, 10 # let changes propagate
|
||||||
|
|
||||||
it 'sets reviewDate and reviewer', utils.wrap (done) ->
|
it 'sets reviewDate and reviewer', utils.wrap (done) ->
|
||||||
trialRequest = yield TrialRequest.findById(@trialRequest.id)
|
trialRequest = yield TrialRequest.findById(@trialRequest.id)
|
||||||
expect(trialRequest.get('reviewDate')).toBeDefined()
|
expect(trialRequest.get('reviewDate')).toBeDefined()
|
||||||
expect(trialRequest.get('reviewer').equals(@admin._id))
|
expect(trialRequest.get('reviewer').equals(@admin._id))
|
||||||
expect(new Date(trialRequest.get('reviewDate'))).toBeLessThan(new Date())
|
expect(new Date(trialRequest.get('reviewDate'))).toBeLessThan(new Date())
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'gives the user two enrollments', utils.wrap (done) ->
|
|
||||||
prepaids = yield Prepaid.find({'properties.trialRequestID': @trialRequest._id})
|
|
||||||
expect(prepaids.length).toEqual(1)
|
|
||||||
prepaid = prepaids[0]
|
|
||||||
expect(prepaid.get('type')).toEqual('course')
|
|
||||||
expect(prepaid.get('creator')).toEqual(@user.get('_id'))
|
|
||||||
expect(prepaid.get('maxRedeemers')).toEqual(2)
|
|
||||||
done()
|
|
||||||
|
|
||||||
it 'enables teacherNews for the user', utils.wrap (done) ->
|
it 'enables teacherNews for the user', utils.wrap (done) ->
|
||||||
user = yield User.findById(@user._id)
|
user = yield User.findById(@user._id)
|
||||||
expect(user.get('emails')?.teacherNews?.enabled).toEqual(true)
|
expect(user.get('emails')?.teacherNews?.enabled).toEqual(true)
|
||||||
|
@ -202,8 +193,4 @@ describe 'PUT /db/trial.request/:handle', ->
|
||||||
expect(trialRequest.get('reviewer').equals(@admin._id))
|
expect(trialRequest.get('reviewer').equals(@admin._id))
|
||||||
expect(new Date(trialRequest.get('reviewDate'))).toBeLessThan(new Date())
|
expect(new Date(trialRequest.get('reviewDate'))).toBeLessThan(new Date())
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'does not give the user two enrollments', utils.wrap (done) ->
|
|
||||||
prepaids = yield Prepaid.find({'properties.trialRequestID': @trialRequest._id})
|
|
||||||
expect(prepaids.length).toEqual(0)
|
|
||||||
done()
|
|
||||||
|
|
Loading…
Reference in a new issue