mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-23 23:58:02 -05:00
Much improved the mailchimp webhook.
This commit is contained in:
parent
b2366c50af
commit
95186cc5ae
1 changed files with 40 additions and 29 deletions
|
@ -5,42 +5,53 @@ errors = require '../commons/errors'
|
|||
request = require 'request'
|
||||
|
||||
badLog = (text) ->
|
||||
options.text = text
|
||||
console.log text
|
||||
request.post 'http://requestb.in/1brdpaz1', { form: {log: text} }
|
||||
|
||||
module.exports.setupRoutes = (app) ->
|
||||
app.all '/mail/webhook', (req, res) ->
|
||||
post = req.body
|
||||
unless post['data[email]'] is 'sderickson+test@gmail.com'
|
||||
|
||||
unless post.type in ['unsubscribe', 'profile']
|
||||
badLog("Bad post type: #{post.type}")
|
||||
return res.end()
|
||||
|
||||
unless post['data[email]']
|
||||
badLog("Ignoring because no email: #{JSON.stringify(req.body, null, '\t')}")
|
||||
return res.end()
|
||||
return handleProfileUpdate(post, res) if post.type is 'profile'
|
||||
return handleUnsubscribe(post, res) if post.type is 'unsubscribe'
|
||||
console.log 'unrecognized...', post
|
||||
return res.end()
|
||||
|
||||
handleProfileUpdate: (data, res) ->
|
||||
User.findOne {'mailChimp.euid':data['data[id]']}, (err, user) ->
|
||||
return errors.serverError(res) if err
|
||||
|
||||
groups = data['data[merges][INTERESTS]'].split(', ')
|
||||
groups = (map[g] for g in groups when map[g])
|
||||
user.set 'emailSubscriptions', groups
|
||||
|
||||
mailChimpInfo = user.get 'mailChimp'
|
||||
mailChimpInfo.email = data['data[email]']
|
||||
user.set 'mailChimp', mailChimpInfo
|
||||
|
||||
badLog("Updating user object to: #{JSON.stringify(user.toObject(), null, '\t')}")
|
||||
res.end()
|
||||
unless post['data[email]'] is 'sderickson@gmail.com'
|
||||
badLog("Ignoring because this is a test: #{JSON.stringify(req.body, null, '\t')}")
|
||||
return res.end()
|
||||
|
||||
|
||||
handleUnsubscribe: (data) ->
|
||||
User.findOne {'mailChimp.euid':data['data[id]']}, (err, user) ->
|
||||
return errors.serverError(res) if err
|
||||
|
||||
user.set 'emailSubscriptions', []
|
||||
user.set 'mailChimp', undefined
|
||||
User.findOne {'mailChimp.euid':post['data[id]']}, (err, user) ->
|
||||
return errors.serverError(res) if err
|
||||
if not user
|
||||
badLog("could not find user for...: #{{'mailChimp.euid':post['data[id]']}}")
|
||||
return res.end()
|
||||
|
||||
handleProfileUpdate(post, user) if post.type is 'profile'
|
||||
handleUnsubscribe(post, user) if post.type is 'unsubscribe'
|
||||
|
||||
badLog("Unsubscribing user object to: #{JSON.stringify(user.toObject(), null, '\t')}")
|
||||
res.end()
|
||||
res.end()
|
||||
user.updatedMailChimp = true # so as not to echo back to mailchimp
|
||||
user.save (err) ->
|
||||
badLog("Error updating profile: #{error.message or error}") if err
|
||||
res.end()
|
||||
|
||||
|
||||
handleProfileUpdate = (data, user) ->
|
||||
groups = data['data[merges][INTERESTS]'].split(', ')
|
||||
groups = (map[g] for g in groups when map[g])
|
||||
user.set 'emailSubscriptions', groups
|
||||
|
||||
mailChimpInfo = user.get 'mailChimp'
|
||||
mailChimpInfo.email = data['data[email]']
|
||||
user.set 'mailChimp', mailChimpInfo
|
||||
|
||||
badLog("Updating user object to: #{JSON.stringify(user.toObject(), null, '\t')}")
|
||||
|
||||
handleUnsubscribe = (data, user) ->
|
||||
user.set 'emailSubscriptions', []
|
||||
|
||||
badLog("Unsubscribing user object to: #{JSON.stringify(user.toObject(), null, '\t')}")
|
Loading…
Reference in a new issue