From 95186cc5aead87e2e5d20052b867f54f60bfafdd Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Fri, 24 Jan 2014 12:23:14 -0800 Subject: [PATCH] Much improved the mailchimp webhook. --- server/routes/mail.coffee | 69 +++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/server/routes/mail.coffee b/server/routes/mail.coffee index 8fc9fc095..f73349f43 100644 --- a/server/routes/mail.coffee +++ b/server/routes/mail.coffee @@ -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() \ No newline at end of file + 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')}") \ No newline at end of file