2014-01-24 14:48:00 -05:00
|
|
|
mail = require '../commons/mail'
|
|
|
|
map = _.invert mail.MAILCHIMP_GROUP_MAP
|
|
|
|
User = require '../users/User.coffee'
|
|
|
|
errors = require '../commons/errors'
|
2014-01-24 14:55:44 -05:00
|
|
|
request = require 'request'
|
2014-01-24 15:53:41 -05:00
|
|
|
config = require '../../server_config'
|
2014-01-24 14:48:00 -05:00
|
|
|
|
2014-01-24 16:14:31 -05:00
|
|
|
badLog = (text) ->
|
|
|
|
console.log text
|
|
|
|
request.post 'http://requestb.in/1brdpaz1', { form: {log: text} }
|
|
|
|
|
2014-01-24 14:48:00 -05:00
|
|
|
module.exports.setupRoutes = (app) ->
|
2014-01-24 15:53:41 -05:00
|
|
|
app.all config.mail.mailchimpWebhook, (req, res) ->
|
2014-01-24 14:48:00 -05:00
|
|
|
post = req.body
|
2014-01-24 16:14:31 -05:00
|
|
|
badLog("Got post data: #{JSON.stringify(post, null, '\t')}")
|
|
|
|
|
|
|
|
unless post.type in ['unsubscribe', 'profile']
|
|
|
|
badLog("Bad post type: #{post.type}")
|
|
|
|
return errors.badInput(res, 'Bad post type')
|
|
|
|
|
|
|
|
unless post.data.email
|
|
|
|
badLog("Ignoring because no email: #{JSON.stringify(req.body, null, '\t')}")
|
|
|
|
return errors.badInput(res, 'No email provided')
|
|
|
|
|
|
|
|
unless post.data.email is 'sderickson@gmail.com'
|
|
|
|
badLog("Ignoring because this is a test: #{JSON.stringify(req.body, null, '\t')}")
|
|
|
|
return res.end()
|
|
|
|
|
2014-01-24 16:47:25 -05:00
|
|
|
query = {'mailChimp.leid':post.data.web_id}
|
|
|
|
User.findOne query, (err, user) ->
|
2014-01-24 15:23:14 -05:00
|
|
|
return errors.serverError(res) if err
|
2014-01-24 16:14:31 -05:00
|
|
|
if not user
|
2014-01-24 16:47:25 -05:00
|
|
|
badLog("could not find user for...: #{query}")
|
2014-01-24 16:22:56 -05:00
|
|
|
return errors.notFound(res)
|
2014-01-24 16:14:31 -05:00
|
|
|
|
2014-01-24 15:39:05 -05:00
|
|
|
handleProfileUpdate(user, post) if post.type is 'profile'
|
|
|
|
handleUnsubscribe(user) if post.type is 'unsubscribe'
|
2014-01-24 16:14:31 -05:00
|
|
|
|
2014-01-24 15:23:14 -05:00
|
|
|
user.updatedMailChimp = true # so as not to echo back to mailchimp
|
|
|
|
user.save (err) ->
|
2014-01-24 15:53:41 -05:00
|
|
|
return errors.serverError(res) if err
|
2014-01-24 16:22:56 -05:00
|
|
|
res.end('Success')
|
2014-01-24 15:23:14 -05:00
|
|
|
|
|
|
|
|
2014-01-24 15:39:05 -05:00
|
|
|
handleProfileUpdate = (user, post) ->
|
|
|
|
groups = post.data.merges.INTERESTS.split(', ')
|
2014-01-24 15:23:14 -05:00
|
|
|
groups = (map[g] for g in groups when map[g])
|
|
|
|
user.set 'emailSubscriptions', groups
|
|
|
|
|
|
|
|
mailChimpInfo = user.get 'mailChimp'
|
2014-01-24 16:47:25 -05:00
|
|
|
mailChimpInfo.email = post.data.email
|
|
|
|
mailChimpInfo.euid = post.data.id
|
2014-01-24 15:23:14 -05:00
|
|
|
user.set 'mailChimp', mailChimpInfo
|
2014-01-24 16:14:31 -05:00
|
|
|
|
|
|
|
badLog("Updating user object to: #{JSON.stringify(user.toObject(), null, '\t')}")
|
2014-01-24 14:48:00 -05:00
|
|
|
|
2014-01-24 15:39:05 -05:00
|
|
|
handleUnsubscribe = (user) ->
|
2014-01-24 15:23:14 -05:00
|
|
|
user.set 'emailSubscriptions', []
|
2014-01-24 16:14:31 -05:00
|
|
|
|
|
|
|
badLog("Unsubscribing user object to: #{JSON.stringify(user.toObject(), null, '\t')}")
|