Import approved trial requests into Close.io

This commit is contained in:
Matt Lott 2016-02-26 06:21:28 -08:00
parent 449ded5083
commit 7bd0936c93
4 changed files with 79 additions and 13 deletions

View file

@ -24,13 +24,7 @@ block content
th Phone
th Status
tbody
- var numReviewed = 0
- var maxReviewedShown = 1000
each trialRequest in trialRequests
if trialRequest.get('status') !== 'submitted'
- numReviewed++
if numReviewed > maxReviewedShown
- break
tr
td.created= trialRequest.get('created').substring(0, 10)
td.reviewed

50
server/lib/closeIO.coffee Normal file
View file

@ -0,0 +1,50 @@
config = require '../../server_config'
log = require 'winston'
request = require 'request'
apiKey = config.closeIO?.apiKey
module.exports =
logError: (msg) ->
log.error("Close.io Error: #{msg}")
createSalesLead: (user, email, newLeadData) ->
return @logError('No API key available') unless apiKey
@getLead email, (error, lead) =>
return @logError(JSON.stringify(error)) if error
return if lead
@createLead(user, email, newLeadData)
createLead: (user, email, newLeadData) ->
name = newLeadData.name ? email
postData =
display_name: newLeadData.organization ? name
name: newLeadData.organization ? name
contacts: [{
emails: [{email: email}]
name: name
}]
custom: {}
postData.contacts[0].phones = [phone: newLeadData.phone] if newLeadData.phone
for key, val of newLeadData
continue if key in ['name', 'organization', 'phone']
continue if _.isEmpty(val)
postData.custom[key] = val
postData.custom['userID'] = user.get('_id').valueOf() if user
options =
uri: 'https://' + apiKey + ':X@app.close.io/api/v1/lead/',
body: JSON.stringify(postData)
request.post options, (error, response, body) =>
return @logError(JSON.stringify(error)) if error
getLead: (email, done) ->
uri = 'https://' + apiKey + ':X@app.close.io/api/v1/lead/?query=email_address:' + email
request.get uri, (error, response, body) =>
return done(error) if error
leads = JSON.parse(body)
return done("Unexpected leads format: " + body) unless leads.data?
if leads.data?.length is 1
return done(null, leads.data[0])
else if leads.data?.length > 1
return done('ERROR: multiple leads returned for ' + email + ' ' + leads.data.length)
return done()

View file

@ -1,3 +1,4 @@
closeIO = require '../lib/closeIO'
log = require 'winston'
mongoose = require 'mongoose'
config = require '../../server_config'
@ -26,18 +27,36 @@ TrialRequestSchema.pre 'save', (next) ->
TrialRequestSchema.post 'save', (doc) ->
if doc.get('status') is 'approved'
emailParams =
recipient:
address: doc.get('properties')?.email
email_id: sendwithus.templates.teacher_free_trial
sendwithus.api.send emailParams, (err, result) =>
log.error "sendwithus trial request approved error: #{err}, result: #{result}" if err
unless trialProperties = doc.get('properties')
log.error "Saving approved trial request #{doc.id} with no properties!"
return
# Subscribe to teacher news group
User.findById doc.get('applicant'), (err, user) =>
if err
log.error "Trial request user find error: #{err}"
return
# Send trial approved email
email = trialProperties.email ? user.get('emailLower')
emailParams =
recipient:
address: email
email_id: sendwithus.templates.teacher_free_trial
sendwithus.api.send emailParams, (err, result) =>
log.error "sendwithus trial request approved error: #{err}, result: #{result}" if err
closeIO.createSalesLead(user, email,
name: trialProperties.name
organization: trialProperties.organization
location: _.filter(_.at(trialProperties, 'city', 'state', 'country')).join(' ')
educationLevel: (trialProperties.educationLevel or []).join(', ')
numStudents: trialProperties.numStudents
role: trialProperties.role
phone: trialProperties.phoneNumber
notes: trialProperties.notes
)
# Subscribe to teacher news group
emails = _.cloneDeep(user.get('emails') ? {})
emails.teacherNews ?= {}
emails.teacherNews.enabled = true

View file

@ -34,6 +34,9 @@ else
config.apple =
verifyURL: process.env.COCO_APPLE_VERIFY_URL or 'https://sandbox.itunes.apple.com/verifyReceipt'
config.closeIO =
apiKey: process.env.COCO_CLOSEIO_API_KEY or ''
config.stripe =
secretKey: process.env.COCO_STRIPE_SECRET_KEY or 'sk_test_MFnZHYD0ixBbiBuvTlLjl2da'