Merge branch 'master' into production

This commit is contained in:
Matt Lott 2016-05-25 16:25:18 -07:00
commit a14aae8c94
5 changed files with 77 additions and 13 deletions

View file

@ -0,0 +1,38 @@
// Unset someone with a teacher role. Remove trial requests, set role to student or nothing
// depending on if they're in any classrooms.
// Usage
// ---------------
// In mongo shell
//
// > db.loadServerScripts();
// > deteacher('some@email.com');
var deteacher = function deteacher(email) {
var user = db.users.findOne({emailLower: email.toLowerCase()});
if (!user) {
print('User not found');
return;
}
print('Found user', user.name, user.email, user.role, user._id);
var trialRequests = db.trial.requests.find({applicant: user._id}).toArray();
for (var index in trialRequests) {
var trialRequest = trialRequests[index];
print('Delete trial request', JSON.stringify(trialRequest, null, ' '), db.trial.requests.remove({_id: trialRequest._id}, true));
}
var classroomCount = db.classrooms.count({members: user._id});
if (classroomCount > 0) {
print('Set to student', db.users.update({_id: user._id}, {$set: {role: 'student'}}));
}
else {
print('Unset role', db.users.update({_id: user._id}, {$unset: {role: ''}}));
}
}
db.system.js.save(
{
_id: 'deteacher',
value: deteacher
}
)

View file

@ -48,3 +48,24 @@ module.exports =
else if leads.data?.length > 1
return done('ERROR: multiple leads returned for ' + email + ' ' + leads.data.length)
return done()
getSalesContactEmail: (email, done) ->
try
# NOTE: does not work on + email addresses due to Close.io API bug
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?
return done(null, config.mail.supportSchools) unless leads.data?.length > 0
lead = leads.data[0]
uri = "https://#{apiKey}:X@app.close.io/api/v1/activity/?lead_id=#{lead.id}"
request.get uri, (error, response, body) =>
return done(error) if error
activities = JSON.parse(body)
return done("Unexpected activities format: " + body) unless activities.data?
for activity in activities.data when activity._type is 'Email'
return done(null, activity.sender) if /@codecombat\.com/ig.test(activity.sender)
return done(null, config.mail.supportSchools)
catch error
return done(error, config.mail.supportSchools)

View file

@ -1,4 +1,3 @@
closeIO = require '../lib/closeIO'
log = require 'winston'
mongoose = require 'mongoose'
config = require '../../server_config'

View file

@ -5,11 +5,12 @@ sendwithus = require '../sendwithus'
async = require 'async'
LevelSession = require '../models/LevelSession'
moment = require 'moment'
closeIO = require '../lib/closeIO'
module.exports.setup = (app) ->
app.post '/contact', (req, res) ->
return res.end() unless req.user
#log.info "Sending mail from #{req.body.email} saying #{req.body.message}"
# log.info "Sending mail from #{req.body.email} saying #{req.body.message}"
createMailContext req, (context) ->
sendwithus.api.send context, (err, result) ->
if err
@ -32,30 +33,35 @@ createMailContext = (req, done) ->
#{message}
--
<a href='http://codecombat.com/user/#{user.get('slug') or user.get('_id')}'>#{user.get('name') or 'Anonymous'}</a> - Level #{level}#{if teacher then ' - Teacher' else ''}#{if premium then ' - Subscriber' else ''}#{if country then ' - ' + country else ''}
http://codecombat.com/user/#{user.get('slug') or user.get('_id')}
#{user.get('name') or 'Anonymous'} - Level #{level}#{if teacher then ' - Teacher' else ''}#{if premium then ' - Subscriber' else ''}#{if country then ' - ' + country else ''}
"""
if req.body.browser
content += "\n#{req.body.browser} - #{req.body.screenSize}"
address = switch
when teacher then config.mail.supportSchools
toAddress = switch
when premium then config.mail.supportPremium
else config.mail.supportPrimary
fromAddress = sender or user.get('email')
context =
email_id: sendwithus.templates.plain_text_email
recipient:
address: address
address: toAddress
sender:
address: config.mail.username
reply_to: sender or user.get('email')
reply_to: fromAddress
name: user.get('name')
email_data:
subject: "[CodeCombat] #{subject ? ('Feedback - ' + (sender or user.get('email')))}"
subject: "[CodeCombat] #{subject ? ('Feedback - ' + fromAddress)}"
content: content
if recipientID is 'schools@codecombat.com'
context.recipient.address = 'schools@codecombat.com'
req.user.update({$set: { enrollmentRequestSent: true }}).exec(_.noop)
done context
if recipientID is 'schools@codecombat.com' or teacher
req.user.update({$set: { enrollmentRequestSent: true }}).exec(_.noop) if recipientID is 'schools@codecombat.com'
closeIO.getSalesContactEmail fromAddress, (err, salesContactEmail) ->
console.error "Error getting sales contact for #{sender}: #{err}" if err
context.recipient.address = salesContactEmail ? config.mail.supportSchools
context.sender.address = fromAddress
done context
else if recipientID and (user.isAdmin() or ('employer' in (user.get('permissions') ? [])))
User.findById(recipientID, 'email').exec (err, document) ->
if err

View file

@ -36,7 +36,7 @@ if (database.generateMongoConnectionString() !== dbString) {
throw Error('Stopping server tests because db connection string was not as expected.');
}
//jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 120; // for long Stripe tests
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 10; // for long Stripe tests
require('../server/common'); // Make sure global testing functions are set up
var initialized = false;