fix: update to use shortid for sub resub

pull/17973/head
Mrugesh Mohapatra 2018-08-03 01:28:49 +05:30 committed by Stuart Taylor
parent 99b9e9e3ba
commit 4511e33716
4 changed files with 18 additions and 21 deletions

View File

@ -14,8 +14,8 @@ import { isEmail } from 'validator';
import path from 'path';
import loopback from 'loopback';
import _ from 'lodash';
import { ObjectId } from 'mongodb';
import jwt from 'jsonwebtoken';
import generate from 'nanoid/generate';
import { fixCompletedChallengeItem } from '../utils';
import { themes } from '../utils/themes';
@ -34,6 +34,8 @@ import {
const log = debugFactory('fcc:models:user');
const BROWNIEPOINTS_TIMEOUT = [1, 'hour'];
const nanoidCharSet =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const createEmailError = redirectTo => wrapHandledError(
new Error('email format is invalid'),
@ -237,7 +239,7 @@ module.exports = function(User) {
user.externalId = uuid();
}
if (!user.unsubscribeId) {
user.unsubscribeId = new ObjectId();
user.unsubscribeId = generate(nanoidCharSet, 20);
}
if (!user.progressTimestamps) {
@ -296,7 +298,7 @@ module.exports = function(User) {
}
if (!user.unsubscribeId) {
user.unsubscribeId = new ObjectId();
user.unsubscribeId = generate(nanoidCharSet, 20);
}
})
.ignoreElements();

9
package-lock.json generated
View File

@ -5901,7 +5901,7 @@
},
"event-stream": {
"version": "3.3.4",
"resolved": "https://registry.npmjs.org/event-stream/-/event-stream-3.3.4.tgz",
"resolved": "http://registry.npmjs.org/event-stream/-/event-stream-3.3.4.tgz",
"integrity": "sha1-SrTJoPWlTbkzi0w02Gv86PSzVXE=",
"dev": true,
"requires": {
@ -12719,6 +12719,11 @@
"dev": true,
"optional": true
},
"nanoid": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-1.1.0.tgz",
"integrity": "sha512-iOCqgXieGrk8/wDt1n9rZS2KB1dYVssemY0NTWjfzVr+1t1gAmdTp1u2+YHppKro3Bk5S+Gs+xmYCfpuXauYXQ=="
},
"nanomatch": {
"version": "1.2.6",
"resolved": "https://registry.npmjs.org/nanomatch/-/nanomatch-1.2.6.tgz",
@ -16834,7 +16839,7 @@
},
"should-equal": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/should-equal/-/should-equal-0.3.1.tgz",
"resolved": "http://registry.npmjs.org/should-equal/-/should-equal-0.3.1.tgz",
"integrity": "sha1-vY6pemdI45+tR2o75v1y68LnK/A=",
"dev": true,
"requires": {

View File

@ -102,6 +102,7 @@
"mongodb": "^3.0.4",
"morgan": "^1.6.1",
"mousetrap": "~1.6.0",
"nanoid": "^1.1.0",
"node-emoji": "^1.4.1",
"nodemailer": "^2.1.0",
"nodemailer-ses-transport": "^1.5.1",

View File

@ -1,5 +1,4 @@
import request from 'request';
import { ObjectId } from 'mongodb';
import constantStrings from '../utils/constantStrings.json';
import testimonials from '../resources/testimonials.json';
@ -162,7 +161,7 @@ module.exports = function(app) {
req.flash('info', {
msg: 'We\'ve successfully updated your Email preferences.'
});
return res.redirect('/unsubscribed/');
return res.redirect('/unsubscribed');
})
.catch(next);
});
@ -176,13 +175,11 @@ module.exports = function(app) {
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
sendQuincyEmail: false
}, (err) => {
if (err) {
reject(err);
@ -194,7 +191,7 @@ module.exports = function(app) {
req.flash('success', {
msg: 'We\'ve successfully updated your email preferences.'
});
return res.redirect(`/unsubscribed/${queryId}`);
return res.redirect(`/unsubscribed/${unsubscribeId}`);
})
.catch(next);
});
@ -215,16 +212,8 @@ module.exports = function(app) {
}
function resubscribe(req, res, next) {
const { unsubscribeId: queryId } = req.params;
return User.find({
where: {
or: [
{ unsubscribeId: queryId },
{ unsubscribeId: ObjectId(queryId).toString() },
{ unsubscribeId: ObjectId(queryId) }
]
}
},
const { unsubscribeId } = req.params;
return User.find({ where: { unsubscribeId } },
(err, users) => {
if (err || !users.length) {
req.flash('info', {