From cb15de32f5cd05e587b175dbe42ccc4bcfb9e74f Mon Sep 17 00:00:00 2001 From: Stuart Taylor Date: Mon, 28 May 2018 22:04:46 +0100 Subject: [PATCH] chore(unsubscribe): Merge backup/master unsubscribe handler in to staging (#17253) --- server/boot/randomAPIs.js | 72 +++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/server/boot/randomAPIs.js b/server/boot/randomAPIs.js index 8e79973ccd..d3ac16c190 100644 --- a/server/boot/randomAPIs.js +++ b/server/boot/randomAPIs.js @@ -12,9 +12,8 @@ module.exports = function(app) { router.get('/api/github', githubCalls); router.get('/chat', chat); router.get('/twitch', twitch); - router.get('/unsubscribe/:email', unsubscribeAll); - router.get('/unsubscribe-notifications/:email', unsubscribeAll); - router.get('/unsubscribe-quincy/:email', unsubscribeAll); + router.get('/u/:email', unsubscribe); + router.get('/unsubscribe/:email', unsubscribe); router.get('/submit-cat-photo', submitCatPhoto); router.get( '/the-fastest-web-page-on-the-internet', @@ -121,30 +120,51 @@ module.exports = function(app) { res.redirect('https://twitch.tv/freecodecamp'); } - function unsubscribeAll(req, res, next) { - req.checkParams('email', 'Must send a valid email').isEmail(); - var query = { email: req.params.email }; - var params = { - sendQuincyEmail: false, - sendMonthlyEmail: false, - sendNotificationEmail: false - }; - return User.updateAll(query, params, function(err, info) { - if (err) { return next(err); } - if (info.count === 0) { - req.flash( - 'info', - 'Email address not found. ' + - 'Please update your Email preferences from your profile.' - ); - return res.redirect('/map'); - } else { - req.flash( - 'info', - 'We\'ve successfully updated your Email preferences.' - ); - return res.redirect('/unsubscribed'); + function unsubscribe(req, res, next) { + req.checkParams( + 'email', + `"${req.params.email}" isn't a valid email address.` + ).isEmail(); + const errors = req.validationErrors(true); + if (errors) { + req.flash('error', { msg: errors.email.msg }); + return res.redirect('/'); + } + return User.find({ + where: { + email: req.params.email } + }, (err, users) => { + if (err) { return next(err); } + if (!users.length) { + req.flash('info', { + msg: 'Email address not found. Please update your Email ' + + 'preferences from your settings.' + }); + return res.redirect('/'); + } + + const updates = users.map(user => { + return new Promise((resolve, reject) => + user.updateAttributes({ + sendQuincyEmail: false + }, (err) => { + if (err) { + reject(err); + } else { + resolve(); + } + }) + ); + }); + return Promise.all(updates) + .then(() => { + req.flash('info', { + msg: 'We\'ve successfully updated your Email preferences.' + }); + return res.redirect('/unsubscribed/' + req.params.email); + }) + .catch(next); }); }