Skip to content

Commit

Permalink
Merge 084cfc4 into ff5c6ac
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiamakaObitube committed Aug 23, 2019
2 parents ff5c6ac + 084cfc4 commit 5840a26
Show file tree
Hide file tree
Showing 9 changed files with 1,595 additions and 1,409 deletions.
2,851 changes: 1,466 additions & 1,385 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"author": "Andela Simulations Programme",
"license": "MIT",
"dependencies": {
"@sendgrid/mail": "^6.4.0",
"body-parser": "^1.18.3",
"cors": "^2.8.4",
"dotenv": "^6.0.0",
Expand Down Expand Up @@ -46,6 +47,7 @@
"@babel/polyfill": "^7.4.4",
"@babel/preset-env": "^7.5.5",
"@babel/register": "^7.5.5",
"babel-eslint": "^10.0.2",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"coveralls": "^3.0.6",
Expand Down
29 changes: 26 additions & 3 deletions src/controllers/UserController.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
import db from '../models';
import db from '../models/User';
import { Sequelize, sequelize } from '../models/index';
import SendEmail from '../utils/HelperMethods';

const Users = db(sequelize, Sequelize.DataTypes);

// eslint-disable-next-line import/prefer-default-export
export const signUp = async (req, res) => {
try {
const user = await db.User.create(req.body);
res.status(201).json({ user });
const isVerified = false;
const {
email, firstName, lastName, password
} = req.body;
const user = await Users.create({
email,
firstName,
lastName,
password,
isVerified,
});
const url = 'localhost:3000/;';
SendEmail(email, firstName, url);
res.status(201).json({
success: true,
message: 'Your account has been successfully created. An email has been sent to you with detailed instructions on how to activate it.',
data: user
});
} catch (err) {
const status = 400;
const error = err.errors[0].message;
res.status(status).json({ status, error });
}
};

export const verifiedUser = async (req, res) => {
}
43 changes: 30 additions & 13 deletions src/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
import { Sequelize, sequelize } from '../models/index';
import db from '../models/User';
import { Sequelize, sequelize } from '../models';
import SendEmail from '../utils/HelperMethods';

const Users = require('../models/User')(sequelize, Sequelize.DataTypes);
const Users = db(sequelize, Sequelize.DataTypes);

const timeStamp = Date.now().toString();
const id = Math.round(parseInt(timeStamp, 10) / 1000);
// eslint-disable-next-line import/prefer-default-export
export const signUp = async (req, res) => {
try {
const isVerified = false;

module.exports = {
create(req, res) {
const { name, email, password } = req.body;
return Users.create({
name,
const {
email, firstName, lastName, password
} = req.body;
const user = await Users.create({
email,
firstName,
lastName,
password,
id
})
.then(user => res.status(201).send(user))
.catch(error => res.status(400).send(error));
isVerified,
});
const url = 'localhost:3000/;';
SendEmail(email, firstName, url);
res.status(201).json({
success: true,
message: 'Your account has been successfully created. An email has been sent to you with detailed instructions on how to activate it.',
data: user
});
} catch (err) {
const status = 400;
const error = err.errors[0].message;
res.status(status).json({ status, error });
}
};

export const verifiedUser = async (req, res) => {
}
3 changes: 3 additions & 0 deletions src/migrations/20190814125500-create-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ module.exports = {
password: {
type: Sequelize.STRING
},
isVerified: {
type: Sequelize.BOOLEAN
},

createdAt: {
allowNull: false,
Expand Down
10 changes: 8 additions & 2 deletions src/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ export default (sequelize, DataTypes) => {
msg: 'Password field cannot be empty.'
}
}
}
},
isVerified: DataTypes.BOOLEAN,
},
{
classMethods: {
associate: (models) => {
associate: models => {
User.hasMany(models.Requests, {
onDelete: 'CASCADE',
foreignKey: 'UserId'
Expand All @@ -58,6 +59,11 @@ export default (sequelize, DataTypes) => {
foreignKey: 'UserId',
onDelete: 'CASCADE'
});
User.hasOne(models.VerifyUser, {
foreignKey: 'userId',
as: 'verifiedUser',
onDelete: 'CASCADE',
});
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,4 @@ Object.keys(db).forEach(modelName => {
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

export default db;
export { sequelize, Sequelize };
37 changes: 37 additions & 0 deletions src/utils/HelperMethods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import sgMail from '@sendgrid/mail';

const sendEMail = (email, firstName, url) => {
sgMail.setApiKey(process.env.SENDGRID_API_KEY);

const msg = {
to: email,
from: process.env.PROJECT_EMAIL,
subject: 'Verify Your Email',
text: 'Welcome to Barefoot Nomad, please verify your email below',
html: `'<div style="width: 90%; margin: 5em auto; box-shadow: 0 0 10px rgba(0,0,0,.9);">
<div>
<div>
<div style="background-color: #2084ba; height: 3rem; width: 100%">
<h2 style="text-align: center; color: white; padding-top: 10px;">Barefoot Nomad</h2>
</div>
<h4 style="text-align: center">Hi! ${firstName}</h4>
</div>
<div style=" padding: 0px 20px 20px 20px">
<div>
<p>Please verify that your email <strong>${email}</strong> when you signed up.</p>
<p>Click on the button below to verify.</p>
<button style="color: white; background-color: #2084ba; border: none; border-radius: 10px; text-align: center; padding: 10px;"><a href="${url}" style="text-decoration: none; color: white;">Verify Account</a></button>
</div>
<div>
<h3 style="text-align: center">Thank you</h3>
<h3 style="text-align: center">Please do not reply, this is an autogenerated email.</h3>
</div>
</div>
</div>
</div>`,
};
sgMail.send(msg);
return true;
};

export default sendEMail;
24 changes: 22 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
node-environment-flags "^1.0.5"
v8flags "^3.1.1"

"@babel/parser@^7.4.3", "@babel/parser@^7.4.4", "@babel/parser@^7.5.5":
"@babel/parser@^7.0.0", "@babel/parser@^7.4.3", "@babel/parser@^7.4.4", "@babel/parser@^7.5.5":
version "7.5.5"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.5.5.tgz#02f077ac8817d3df4a832ef59de67565e71cca4b"
integrity sha512-E5BN68cqR7dhKan1SfqgPGhQ178bkVKpXTPEXnFJBrEt8/DKRZlybmy+IgYLTeN7tp1R5Ccmbm2rBk17sHYU3g==
Expand Down Expand Up @@ -663,7 +663,7 @@
"@babel/parser" "^7.4.4"
"@babel/types" "^7.4.4"

"@babel/traverse@^7.1.0", "@babel/traverse@^7.4.3", "@babel/traverse@^7.4.4", "@babel/traverse@^7.5.5":
"@babel/traverse@^7.0.0", "@babel/traverse@^7.1.0", "@babel/traverse@^7.4.3", "@babel/traverse@^7.4.4", "@babel/traverse@^7.5.5":
version "7.5.5"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.5.5.tgz#f664f8f368ed32988cd648da9f72d5ca70f165bb"
integrity sha512-MqB0782whsfffYfSjH4TM+LMjrJnhCNEDMDIjeTpl+ASaUvxcjoiVCo/sM1GhS1pHOXYfWVCYneLjMckuUxDaQ==
Expand Down Expand Up @@ -926,6 +926,18 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f"
integrity sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==

babel-eslint@^10.0.2:
version "10.0.2"
resolved "https://registry.yarnpkg.com/babel-eslint/-/babel-eslint-10.0.2.tgz#182d5ac204579ff0881684b040560fdcc1558456"
integrity sha512-UdsurWPtgiPgpJ06ryUnuaSXC2s0WoSZnQmEpbAH65XZSdwowgN5MvyP7e88nW07FYXv72erVtpBkxyDVKhH1Q==
dependencies:
"@babel/code-frame" "^7.0.0"
"@babel/parser" "^7.0.0"
"@babel/traverse" "^7.0.0"
"@babel/types" "^7.0.0"
eslint-scope "3.7.1"
eslint-visitor-keys "^1.0.0"

babel-plugin-dynamic-import-node@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/babel-plugin-dynamic-import-node/-/babel-plugin-dynamic-import-node-2.3.0.tgz#f00f507bdaa3c3e3ff6e7e5e98d90a7acab96f7f"
Expand Down Expand Up @@ -1867,6 +1879,14 @@ eslint-plugin-import@^2.18.2:
read-pkg-up "^2.0.0"
resolve "^1.11.0"

eslint-scope@3.7.1:
version "3.7.1"
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-3.7.1.tgz#3d63c3edfda02e06e01a452ad88caacc7cdcb6e8"
integrity sha1-PWPD7f2gLgbgGkUq2IyqzHzctug=
dependencies:
esrecurse "^4.1.0"
estraverse "^4.1.1"

eslint-scope@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-4.0.3.tgz#ca03833310f6889a3264781aa82e63eb9cfe7848"
Expand Down

0 comments on commit 5840a26

Please sign in to comment.