2015-06-05 09:48:09 -04:00
|
|
|
log = require 'winston'
|
|
|
|
mongoose = require 'mongoose'
|
|
|
|
config = require '../../server_config'
|
|
|
|
hipchat = require '../hipchat'
|
|
|
|
sendwithus = require '../sendwithus'
|
|
|
|
Prepaid = require '../prepaids/Prepaid'
|
|
|
|
jsonSchema = require '../../app/schemas/models/trial_request.schema'
|
|
|
|
|
|
|
|
TrialRequestSchema = new mongoose.Schema {}, {strict: false, minimize: false, read:config.mongo.readpref}
|
|
|
|
|
|
|
|
TrialRequestSchema.pre 'save', (next) ->
|
|
|
|
return next() unless @get('status') is 'approved'
|
2015-11-12 19:36:20 -05:00
|
|
|
|
2015-11-24 13:32:15 -05:00
|
|
|
# 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}"
|
|
|
|
|
|
|
|
# Special HoC trial: Add 500 course headcount with end date
|
|
|
|
endDate = new Date()
|
|
|
|
endDate.setUTCMonth(endDate.getUTCMonth() + 2)
|
2015-06-05 09:48:09 -04:00
|
|
|
prepaid = new Prepaid
|
2015-11-24 13:32:15 -05:00
|
|
|
creator: @get('applicant')
|
|
|
|
type: 'course'
|
|
|
|
maxRedeemers: 500
|
2015-06-05 09:48:09 -04:00
|
|
|
properties:
|
2015-11-24 13:32:15 -05:00
|
|
|
endDate: endDate
|
|
|
|
trialRequestID: @get('_id')
|
2015-06-05 09:48:09 -04:00
|
|
|
prepaid.save (err) =>
|
|
|
|
if err
|
|
|
|
log.error "Trial request prepaid creation error: #{err}"
|
2015-11-24 13:32:15 -05:00
|
|
|
next()
|
2015-06-05 09:48:09 -04:00
|
|
|
|
|
|
|
TrialRequestSchema.post 'save', (doc) ->
|
2015-12-10 14:21:02 -05:00
|
|
|
if doc.get('status') is 'approved'
|
2015-12-08 18:16:23 -05:00
|
|
|
endDate = new Date()
|
|
|
|
endDate.setUTCMonth(endDate.getUTCMonth() + 2)
|
2015-06-05 09:48:09 -04:00
|
|
|
emailParams =
|
|
|
|
recipient:
|
|
|
|
address: doc.get('properties')?.email
|
2015-11-24 13:32:15 -05:00
|
|
|
email_id: sendwithus.templates.teacher_free_trial_hoc
|
2015-12-08 18:16:23 -05:00
|
|
|
email_data:
|
2015-12-08 18:23:35 -05:00
|
|
|
endDate: endDate.toDateString()
|
2015-06-05 09:48:09 -04:00
|
|
|
sendwithus.api.send emailParams, (err, result) =>
|
|
|
|
log.error "sendwithus trial request approved error: #{err}, result: #{result}" if err
|
|
|
|
|
|
|
|
TrialRequestSchema.statics.privateProperties = []
|
|
|
|
TrialRequestSchema.statics.editableProperties = [
|
2015-11-19 19:06:58 -05:00
|
|
|
'created'
|
2015-06-05 09:48:09 -04:00
|
|
|
'prepaidCode'
|
|
|
|
'properties'
|
|
|
|
'reviewDate'
|
|
|
|
'reviewer'
|
|
|
|
'status'
|
|
|
|
'type'
|
|
|
|
]
|
|
|
|
|
|
|
|
TrialRequestSchema.statics.jsonSchema = jsonSchema
|
|
|
|
module.exports = TrialRequest = mongoose.model 'trial.request', TrialRequestSchema, 'trial.requests'
|