fix(user): Add update new-email placeholder property

This commit is contained in:
Mrugesh Mohapatra
2017-10-23 21:02:00 +05:30
parent 9ccfe4f72c
commit 5e61fd53d1
2 changed files with 16 additions and 9 deletions

View File

@ -560,15 +560,17 @@ module.exports = function(User) {
} }
); );
User.prototype.requestUpdateEmail = function requestUpdateEmail(email) { User.prototype.requestUpdateEmail = function requestUpdateEmail(
const ownEmail = email === this.email; emailUpdateNew
if (!isEmail('' + email)) { ) {
const ownEmail = emailUpdateNew === this.email;
if (!isEmail('' + emailUpdateNew)) {
return Observable.throw(createEmailError()); return Observable.throw(createEmailError());
} }
// email is already associated and verified with this account // email is already associated and verified with this account
if (ownEmail && this.emailVerified) { if (ownEmail && this.emailVerified) {
return Observable.throw(new Error( return Observable.throw(new Error(
`${email} is already associated with this account.` `${emailUpdateNew} is already associated with this account.`
)); ));
} }
@ -583,23 +585,25 @@ module.exports = function(User) {
`); `);
} }
return Observable.fromPromise(User.doesExist(null, email)) return Observable.fromPromise(User.doesExist(null, emailUpdateNew))
.flatMap(exists => { .flatMap(exists => {
// not associated with this account, but is associated with another // not associated with this account, but is associated with another
if (!ownEmail && exists) { if (!ownEmail && exists) {
return Promise.reject( return Promise.reject(
new Error(`${email} is already associated with another account.`) new Error(
`${emailUpdateNew} is already associated with another account.`
)
); );
} }
const emailVerified = false; const emailVerified = false;
return this.update$({ return this.update$({
email, emailUpdateNew,
emailVerified, emailVerified,
emailVerifyTTL: new Date() emailVerifyTTL: new Date()
}) })
.do(() => { .do(() => {
this.email = email; this.emailUpdateNew = emailUpdateNew;
this.emailVerified = emailVerified; this.emailVerified = emailVerified;
this.emailVerifyTTL = new Date(); this.emailVerifyTTL = new Date();
}); });
@ -607,7 +611,7 @@ module.exports = function(User) {
.flatMap(() => { .flatMap(() => {
const mailOptions = { const mailOptions = {
type: 'email', type: 'email',
to: email, to: emailUpdateNew,
from: getEmailSender(), from: getEmailSender(),
subject: 'freeCodeCamp - Email Update Requested', subject: 'freeCodeCamp - Email Update Requested',
protocol: getProtocol(), protocol: getProtocol(),

View File

@ -16,6 +16,9 @@
} }
} }
}, },
"emailUpdateNew":{
"type": "string"
},
"emailVerifyTTL": { "emailVerifyTTL": {
"type": "date" "type": "date"
}, },