diff --git a/server/trial_requests/TrialRequest.coffee b/server/trial_requests/TrialRequest.coffee index f552167c3..4efd541cd 100644 --- a/server/trial_requests/TrialRequest.coffee +++ b/server/trial_requests/TrialRequest.coffee @@ -10,6 +10,8 @@ TrialRequestSchema = new mongoose.Schema {}, {strict: false, minimize: false, re TrialRequestSchema.pre 'save', (next) -> return next() unless @get('status') is 'approved' + + # Add subscription Prepaid.generateNewCode (code) => unless code log.error "Trial request pre save prepaid gen new code failure" @@ -24,8 +26,20 @@ TrialRequestSchema.pre 'save', (next) -> prepaid.save (err) => if err log.error "Trial request prepaid creation error: #{err}" + return next() @set('prepaidCode', code) - next() + + # 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) -> if doc.get('status') is 'submitted' diff --git a/test/server/functional/trial_request.spec.coffee b/test/server/functional/trial_request.spec.coffee index 3069a25d5..22e2fbbfb 100644 --- a/test/server/functional/trial_request.spec.coffee +++ b/test/server/functional/trial_request.spec.coffee @@ -110,7 +110,7 @@ describe 'Trial Requests', -> createTrialRequest user, 'subscription', properties, (trialRequest) -> loginNewUser (admin) -> admin.set('permissions', ['admin']) - admin.save (err, user) -> + admin.save (err, admin) -> requestBody = trialRequest.toObject() requestBody.status = 'approved' request.put {uri: URL, json: requestBody }, (err, res, body) -> @@ -128,7 +128,13 @@ describe 'Trial Requests', -> expect(new Date(doc.get('reviewDate'))).toBeLessThan(new Date()) expect(doc.get('reviewer')).toEqual(admin._id) expect(doc.get('prepaidCode')).toBeDefined() - done() + Prepaid.findOne {'properties.trialRequestID': doc.get('_id')}, (err, doc) -> + expect(err).toBeNull() + return done(err) if err + expect(doc.get('type')).toEqual('course') + expect(doc.get('creator')).toEqual(user.get('_id')) + expect(doc.get('maxRedeemers')).toEqual(2) + done() it 'Deny trial request', (done) -> loginNewUser (user) ->