feat(resub): Add resubscribe endpoint
This commit is contained in:
@ -1,10 +1,8 @@
|
|||||||
import request from 'request';
|
import request from 'request';
|
||||||
import { isMongoId } from 'validator';
|
import { ObjectId } from 'mongodb';
|
||||||
import dedent from 'dedent';
|
|
||||||
|
|
||||||
import constantStrings from '../utils/constantStrings.json';
|
import constantStrings from '../utils/constantStrings.json';
|
||||||
import testimonials from '../resources/testimonials.json';
|
import testimonials from '../resources/testimonials.json';
|
||||||
import { wrapHandledError } from '../utils/create-handled-error';
|
|
||||||
|
|
||||||
const githubClient = process.env.GITHUB_ID;
|
const githubClient = process.env.GITHUB_ID;
|
||||||
const githubSecret = process.env.GITHUB_SECRET;
|
const githubSecret = process.env.GITHUB_SECRET;
|
||||||
@ -18,12 +16,14 @@ module.exports = function(app) {
|
|||||||
router.get('/twitch', twitch);
|
router.get('/twitch', twitch);
|
||||||
router.get('/u/:email', unsubscribe);
|
router.get('/u/:email', unsubscribe);
|
||||||
router.get('/unsubscribe/:email', unsubscribe);
|
router.get('/unsubscribe/:email', unsubscribe);
|
||||||
router.get('/z', unsubscribeAnon);
|
router.get('/ue/:unsubscribeId', unsubscribeById);
|
||||||
router.get(
|
router.get(
|
||||||
'/the-fastest-web-page-on-the-internet',
|
'/the-fastest-web-page-on-the-internet',
|
||||||
theFastestWebPageOnTheInternet
|
theFastestWebPageOnTheInternet
|
||||||
);
|
);
|
||||||
|
router.get('/unsubscribed/:unsubscribeId', unsubscribedWithId);
|
||||||
router.get('/unsubscribed', unsubscribed);
|
router.get('/unsubscribed', unsubscribed);
|
||||||
|
router.get('/resubscribe/:unsubscribeId', resubscribe);
|
||||||
router.get('/nonprofits', nonprofits);
|
router.get('/nonprofits', nonprofits);
|
||||||
router.get('/nonprofits-form', nonprofitsForm);
|
router.get('/nonprofits-form', nonprofitsForm);
|
||||||
router.get('/pmi-acp-agile-project-managers', agileProjectManagers);
|
router.get('/pmi-acp-agile-project-managers', agileProjectManagers);
|
||||||
@ -120,56 +120,6 @@ module.exports = function(app) {
|
|||||||
res.redirect('https://twitch.tv/freecodecamp');
|
res.redirect('https://twitch.tv/freecodecamp');
|
||||||
}
|
}
|
||||||
|
|
||||||
function unsubscribeAnon(req, res, next) {
|
|
||||||
const { query: { unsubscribeId } } = req;
|
|
||||||
const isValid = unsubscribeId && isMongoId(unsubscribeId);
|
|
||||||
if (!isValid) {
|
|
||||||
throw wrapHandledError(
|
|
||||||
new Error('unsubscribeId is not a mongo ObjectId'),
|
|
||||||
{
|
|
||||||
message: dedent`
|
|
||||||
Oops... something is not right. We could not unsubscribe that email
|
|
||||||
address
|
|
||||||
`,
|
|
||||||
type: 'danger',
|
|
||||||
redirectTo: '/'
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
User.findOne({
|
|
||||||
where: { unsubscribeId }
|
|
||||||
}, (err, user) => {
|
|
||||||
if (err) { return next(err); }
|
|
||||||
if (!user || !user.email) {
|
|
||||||
throw wrapHandledError(
|
|
||||||
new Error('No user or user email to unsubscribe'),
|
|
||||||
{
|
|
||||||
message: dedent`
|
|
||||||
We couldn't find a user account to unsubscribe, are you clicking
|
|
||||||
a link from an email we sent?
|
|
||||||
`,
|
|
||||||
type: 'info',
|
|
||||||
redirectTo: '/'
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return user.update$({
|
|
||||||
sendQuincyEmail: false
|
|
||||||
}).subscribe(() => {
|
|
||||||
req.flash(
|
|
||||||
'info',
|
|
||||||
'We\'ve successfully updated your Email preferences.'
|
|
||||||
);
|
|
||||||
return res.redirect('/unsubscribed');
|
|
||||||
},
|
|
||||||
next,
|
|
||||||
() => {
|
|
||||||
return user.manualReload();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function unsubscribe(req, res, next) {
|
function unsubscribe(req, res, next) {
|
||||||
req.checkParams(
|
req.checkParams(
|
||||||
'email',
|
'email',
|
||||||
@ -212,7 +162,39 @@ module.exports = function(app) {
|
|||||||
req.flash('info', {
|
req.flash('info', {
|
||||||
msg: 'We\'ve successfully updated your Email preferences.'
|
msg: 'We\'ve successfully updated your Email preferences.'
|
||||||
});
|
});
|
||||||
return res.redirect('/unsubscribed');
|
return res.redirect('/unsubscribed/');
|
||||||
|
})
|
||||||
|
.catch(next);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function unsubscribeById(req, res, next) {
|
||||||
|
const { unsubscribeId } = req.params;
|
||||||
|
return User.find({ where: { unsubscribeId } }, (err, users) => {
|
||||||
|
if (err || !users.length) {
|
||||||
|
req.flash('info', {
|
||||||
|
msg: 'We could not find an account to unsubscribe'
|
||||||
|
});
|
||||||
|
return res.redirect('/');
|
||||||
|
|
||||||
|
}
|
||||||
|
const [ user ] = users;
|
||||||
|
return new Promise((resolve, reject) =>
|
||||||
|
user.updateAttributes({
|
||||||
|
sendQuincyEmail: false,
|
||||||
|
unsubscribeId: unsubscribeId
|
||||||
|
}, (err) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
).then(() => {
|
||||||
|
req.flash('success', {
|
||||||
|
msg: 'We\'ve successfully updated your email preferences.'
|
||||||
|
});
|
||||||
|
return res.redirect(`/unsubscribed/${queryId}`);
|
||||||
})
|
})
|
||||||
.catch(next);
|
.catch(next);
|
||||||
});
|
});
|
||||||
@ -224,6 +206,58 @@ module.exports = function(app) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function unsubscribedWithId(req, res) {
|
||||||
|
const { unsubscribeId } = req.params;
|
||||||
|
return res.render('resources/unsubscribed', {
|
||||||
|
title: 'You have been unsubscribed',
|
||||||
|
unsubscribeId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function resubscribe(req, res, next) {
|
||||||
|
const { unsubscribeId: queryId } = req.params;
|
||||||
|
return User.find({
|
||||||
|
where: {
|
||||||
|
or: [
|
||||||
|
{ unsubscribeId: queryId },
|
||||||
|
{ unsubscribeId: ObjectId(queryId).toString() },
|
||||||
|
{ unsubscribeId: ObjectId(queryId) }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
(err, users) => {
|
||||||
|
if (err || !users.length) {
|
||||||
|
req.flash('info', {
|
||||||
|
msg: 'We could not find an account to unsubscribe'
|
||||||
|
});
|
||||||
|
return res.redirect('/');
|
||||||
|
|
||||||
|
}
|
||||||
|
const [ user ] = users;
|
||||||
|
return new Promise((resolve, reject) =>
|
||||||
|
user.updateAttributes({
|
||||||
|
sendQuincyEmail: true
|
||||||
|
}, (err) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.then(() => {
|
||||||
|
req.flash('success', {
|
||||||
|
msg:
|
||||||
|
'We\'ve successfully updated your email preferences. Thank you ' +
|
||||||
|
'for resubscribing.'
|
||||||
|
});
|
||||||
|
return res.redirect('/');
|
||||||
|
})
|
||||||
|
.catch(next);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function githubCalls(req, res, next) {
|
function githubCalls(req, res, next) {
|
||||||
var githubHeaders = {
|
var githubHeaders = {
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -5,17 +5,17 @@
|
|||||||
.alert.alert-danger.fade.in
|
.alert.alert-danger.fade.in
|
||||||
button.close(type='button', data-dismiss='alert')
|
button.close(type='button', data-dismiss='alert')
|
||||||
i.fas.fa-times-circle
|
i.fas.fa-times-circle
|
||||||
for msg in messages.danger
|
for danger in messages.danger
|
||||||
div!= msg
|
div!= typeof danger === 'string' ? danger : danger.msg
|
||||||
if messages.info
|
if messages.info
|
||||||
.alert.alert-info.fade.in
|
.alert.alert-info.fade.in
|
||||||
button.close(type='button', data-dismiss='alert')
|
button.close(type='button', data-dismiss='alert')
|
||||||
i.fas.fa-times-circle
|
i.fas.fa-times-circle
|
||||||
for msg in messages.info
|
for info in messages.info
|
||||||
div!= msg
|
div!= typeof info === 'string' ? info : info.msg
|
||||||
if messages.success
|
if messages.success
|
||||||
.alert.alert-success.fade.in
|
.alert.alert-success.fade.in
|
||||||
button.close(type='button', data-dismiss='alert')
|
button.close(type='button', data-dismiss='alert')
|
||||||
i.fas.fa-times-circle
|
i.fas.fa-times-circle
|
||||||
for msg in messages.success
|
for success in messages.success
|
||||||
div!= msg
|
div!= typeof success === 'string' ? success : success.msg
|
@ -4,3 +4,6 @@ block content
|
|||||||
.panel-body.text-center
|
.panel-body.text-center
|
||||||
h1 You have successfully been unsubscribed.
|
h1 You have successfully been unsubscribed.
|
||||||
h2 Whatever you do, keep coding! :)
|
h2 Whatever you do, keep coding! :)
|
||||||
|
if unsubscribeId
|
||||||
|
.col-xs-12.col-md-8.col-md-offset-2
|
||||||
|
a.btn.btn-primary.btn-lg.btn-block(href='/resubscribe/#{unsubscribeId}') You can click here to resubscribe
|
Reference in New Issue
Block a user