Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#163901873] Send appropriate message to the user #62

Merged
merged 1 commit into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions server/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,32 +165,29 @@ class UserController {
fullName, userName, email, password
} = req.body;

const foundUser = await User.findOne({ where: { email } });
const foundUser = await User.findOne({
where: {
[Op.or]: [{ email }, { userName }]
}
});

if (foundUser) {
if (foundUser.isVerified === false) {
const initialToken = TokenManager.sign({ userEmail: foundUser.email }, '24h');
const sentEmail = await MailManager.sendVerificationEmail({
createdUser: foundUser,
token: initialToken
if (foundUser.userName === userName) {
return res.status(409).send({
status: 'failure',
data: {
statusCode: 409,
message: 'Username has already been taken'
}
});
if (sentEmail) {
return res.status(200).send({
status: 'success',
data: {
statusCode: 200,
message:
'You had started your registration process earlier. '
+ 'Kindly check your email to complete your registration'
}
});
}
}
if (foundUser.isVerified === true) {
return res.status(200).send({
status: 'success',

if (foundUser.email === email) {
return res.status(409).send({
status: 'failure',
data: {
message: "You have already been registered on Author's Haven. Kindly proceed to login"
statusCode: 409,
message: 'Email has already been taken'
}
});
}
Expand Down Expand Up @@ -299,9 +296,22 @@ class UserController {
const { email } = req.body;
const foundUser = await User.findOne({ where: { email } });

if (!foundUser) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this if(!foundUser) {} statement necessary? If the user doesn't exist yet that means the new user can then sign up with the data not give 404 error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this comment, rafmme. Actually, this check is necessary because I need to make sure this user is in the database record or not. This is because only a user whose record has been previously created should be able to get a new verification email.

return res.status(404).send({
status: 'failure',
data: {
statusCode: 404,
message: 'This email address does not exist. Kindly sign up'
}
});
}

const token = TokenManager.sign({ userEmail: email }, '24h');

await MailManager.sendVerificationEmail({ createdUser: foundUser, token });
await MailManager.sendVerificationEmail({
createdUser: foundUser,
token
});

return res.status(200).send({
status: 'success',
Expand All @@ -311,7 +321,7 @@ class UserController {
}
});
} catch (err) {
res.status(400).send({
res.status(500).send({
status: 'failure',
data: {
error: err
Expand Down
8 changes: 6 additions & 2 deletions server/helpers/MailManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class MailManager {
* @memberof MailManager
*/
static sendMailNotification(message) {
return sgMail.send(message);
return process.env.NODE_ENV === 'test'
? Promise.resolve()
: sgMail.send(message);
}

/**
Expand All @@ -38,7 +40,9 @@ class MailManager {
subject: "Welcome to Author's Haven! Confirm your email",
html: verifyEmailTemplate(createdUser, token)
};
return sgMail.send(message);
return process.env.NODE_ENV === 'test'
? Promise.resolve()
: sgMail.send(message);
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/helpers/emailTemplates/newArticleTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const newArticleTemplate = (follower, article, userName) => `<!DOCTYPE html>
article.slug
}>new article</a> .
</p>
<a class="reset-btn" href=https://neon-ah-frontend-staging.herokuapp.com//articles/${
<a class="reset-btn" href=https://neon-ah-frontend-staging.herokuapp.com/articles/${
article.slug
}>
Read Article
Expand Down
Loading