fix(User.updateEmail): Reduce code logic. defer promises
This commit is contained in:
committed by
mrugesh mohapatra
parent
5e38ae4347
commit
b6f621fee3
@ -22,8 +22,13 @@ import {
|
|||||||
const debug = debugFactory('fcc:user:remote');
|
const debug = debugFactory('fcc:user:remote');
|
||||||
const BROWNIEPOINTS_TIMEOUT = [1, 'hour'];
|
const BROWNIEPOINTS_TIMEOUT = [1, 'hour'];
|
||||||
|
|
||||||
const createEmailError = () => new Error(
|
const createEmailError = redirectTo => wrapHandledError(
|
||||||
'Please check to make sure the email is a valid email address.'
|
new Error('email format is invalid'),
|
||||||
|
{
|
||||||
|
type: 'info',
|
||||||
|
message: 'Please check to make sure the email is a valid email address.',
|
||||||
|
redirectTo
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
function destroyAll(id, Model) {
|
function destroyAll(id, Model) {
|
||||||
@ -518,54 +523,58 @@ module.exports = function(User) {
|
|||||||
`);
|
`);
|
||||||
};
|
};
|
||||||
|
|
||||||
User.prototype.requestUpdateEmail = function requestUpdateEmail(
|
User.prototype.requestUpdateEmail = function requestUpdateEmail(newEmail) {
|
||||||
newEmail
|
return Observable.defer(() => {
|
||||||
) {
|
const ownEmail = newEmail === this.email;
|
||||||
const ownEmail = newEmail === this.email;
|
if (!isEmail('' + newEmail)) {
|
||||||
if (!isEmail('' + newEmail)) {
|
debug('invalid email:', newEmail );
|
||||||
debug('invalid email:', newEmail );
|
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) {
|
throw wrapHandledError(
|
||||||
return Observable.throw(new Error(
|
new Error('email is already associated with account'),
|
||||||
`${newEmail} is already associated with this account.`
|
{
|
||||||
));
|
type: 'info',
|
||||||
}
|
message: `${newEmail} is already associated with this account.`
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const minutesLeft = getWaitPeriod(this.emailVerifyTTL);
|
const minutesLeft = getWaitPeriod(this.emailVerifyTTL);
|
||||||
if (ownEmail && minutesLeft > 0) {
|
if (ownEmail && minutesLeft > 0) {
|
||||||
const timeToWait = minutesLeft ?
|
const timeToWait = minutesLeft ?
|
||||||
`${minutesLeft} minute${minutesLeft > 1 ? 's' : ''}` :
|
`${minutesLeft} minute${minutesLeft > 1 ? 's' : ''}` :
|
||||||
'a few seconds';
|
'a few seconds';
|
||||||
debug('request before wait time : ' + timeToWait);
|
|
||||||
return Observable.of(dedent`
|
|
||||||
Please wait ${timeToWait} to resend an authentication link.
|
|
||||||
`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Observable.fromPromise(User.doesExist(null, newEmail))
|
debug('request before wait time : ' + timeToWait);
|
||||||
|
|
||||||
|
return Observable.of(dedent`
|
||||||
|
Please wait ${timeToWait} to resend an authentication link.
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
// defer prevents the promise from firing prematurely
|
||||||
|
return Observable.defer(() => User.doesExist(null, newEmail));
|
||||||
|
})
|
||||||
.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 (!exists) {
|
||||||
return Promise.reject(
|
throw wrapHandledError(
|
||||||
new Error(
|
new Error('email already in use'),
|
||||||
`${newEmail} is already associated with another account.`
|
{
|
||||||
)
|
type: 'info',
|
||||||
|
message: `${newEmail} is already associated with another account.`
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const emailVerified = false;
|
const emailVerified = false;
|
||||||
return this.update$({
|
const data = {
|
||||||
newEmail,
|
newEmail,
|
||||||
emailVerified,
|
emailVerified,
|
||||||
emailVerifyTTL: new Date()
|
emailVerifyTTL: new Date()
|
||||||
})
|
};
|
||||||
.do(() => {
|
return this.update$(data).do(() => Object.assign(this, data));
|
||||||
this.newEmail = newEmail;
|
|
||||||
this.emailVerified = emailVerified;
|
|
||||||
this.emailVerifyTTL = new Date();
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
.flatMap(() => {
|
.flatMap(() => {
|
||||||
const mailOptions = {
|
const mailOptions = {
|
||||||
|
Reference in New Issue
Block a user