Skip to content

Commit

Permalink
s
Browse files Browse the repository at this point in the history
  • Loading branch information
Onyeka Mmakwe authored and Onyeka Mmakwe committed Jan 23, 2019
1 parent 74a3ccb commit 3f41608
Show file tree
Hide file tree
Showing 16 changed files with 718 additions and 64 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ script:
- npm test
after_success:
- npm run coverage

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"nyc": "^13.1.0",
"rimraf": "^2.6.3",
"sequelize-cli": "^5.4.0",
"sinon": "^7.2.2"
"sinon": "^7.2.2",
"sinon-chai": "^3.3.0"
},
"nyc": {
"require": [
Expand All @@ -97,4 +98,4 @@
"/server/test/*.js`"
]
}
}
}
23 changes: 19 additions & 4 deletions server/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import MailManager from '../helpers/MailManager';
import db from '../models';
import PasswordManager from '../helpers/PasswordManager';


const { User } = db;

/**
Expand All @@ -21,7 +22,11 @@ class UserController {
static async forgotPassword(req, res) {
try {
const { email } = req.body;
const user = await User.findOne({ where: { email } });
const user = await User.findOne({
where: {
email
}
});
if (!user) {
return res.status(404).json({
status: 'failure',
Expand Down Expand Up @@ -50,6 +55,7 @@ class UserController {
statusCode: 200,
message: 'Kindly check your mail to reset your password'
}

});
} catch (error) {
res.status(500).send({
Expand Down Expand Up @@ -102,8 +108,14 @@ class UserController {
}

await User.update(
{ password: PasswordManager.hashPassword(newPassword) },
{ where: { email } }
{
password: PasswordManager.hashPassword(newPassword)
},
{
where: {
email
}
}
);

res.status(200).json({
Expand All @@ -114,7 +126,10 @@ class UserController {
}
});
} catch (error) {
if (error.name === 'TokenExpiredError' || error.name === 'JsonWebTokenError') {
if (
error.name === 'TokenExpiredError'
|| error.name === 'JsonWebTokenError'
) {
return res.status(401).send({
status: 'failure',
data: {
Expand Down
231 changes: 231 additions & 0 deletions server/controllers/followController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
import db from '../models';
import response from '../helpers/response';

const { User, Follow } = db;

/**
*
*
* @class followContoller
*/
class followContoller {
/**
*
* @description Method to follow user
* @static
* @param {*} req
* @param {*} res
* @returns {object} Json response
* @memberof followContoller
*/
static async followUser(req, res) {
try {
const { userName } = req.params;
const user = await User.findOne({
where: {
userName
}
});

if (!user) {
return response(res, 404, 'failure', 'User not found');
}

const userId = user.id;
const followersId = req.user.userId;

if (user.id === req.user.userId) {
return response(res, 400, 'failure', 'You cannot follow yourself');
}

Follow.findOrCreate({
where: { userId, followersId },
attributes: ['id', 'followersId', 'userId']
}).spread((follow, created) => {
if (created) {
return response(
res,
201,
'success',
`You are now following ${user.userName}`
);
}
return response(
res,
400,
'failure',
`You are already following ${user.userName}`
);
});
} catch (error) {
response(res, 500, 'failure', error.name);
}
}

/**
*
* @description Method to unfollow user
* @static
* @param {*} req
* @param {*} res
* @returns {object} Json response
* @memberof followContoller
*/
static async unfollowUser(req, res) {
try {
const { userName } = req.params;

const user = await User.findOne({
where: {
userName
}
});

if (!user) {
return response(res, 404, 'failure', 'User not found');
}

const userId = user.id;

const followersId = req.user.userId;

if (user.id === req.user.userId) {
return response(res, 400, 'failure', 'You cannot unfollow yourself');
}

const userUnfollow = await Follow.findOne({
where: { userId, followersId }
});

if (!userUnfollow) {
return response(
res,
400,
'failure',
`You are not following ${user.userName}`
);
}

userUnfollow.destroy();
response(res, 200, 'success', `You unfollowed ${user.userName}`);
} catch (error) {
response(res, 500, 'failure', error.name);
}
}

/**
*
* @description Method to get all followers
* @static
* @param {*} req
* @param {*} res
* @returns {object} Json response
* @memberof followContoller
*/
static async getFollowers(req, res) {
try {
const { userName } = req.params;
const user = await User.findOne({
where: { userName }
});

if (!user) {
return response(res, 404, 'failure', 'User not found');
}

const userId = user.dataValues.id;

const followers = await Follow.findAll({
where: { userId },
include: [
{
model: User,
as: 'followingUser',
attributes: { exclude: ['email', 'password', 'isVerified', 'notifySettings', 'roleId', 'authTypeId', 'createdAt', 'updatedAt'] }
}
],

});

if (followers.length === 0) {
return response(res, 200, 'success', 'You currenly have no followers');
}

const payload = {
followers: followers.map(follower => follower.followingUser),
get followCount() {
return this.followers.length;
}
};

response(
res,
200,
'success',
'Followers returned successfully',
null,
payload
);
} catch (error) {
response(res, 500, 'failure', error.name);
}
}

/**
*
* @description Method to get all following
* @static
* @param {*} req
* @param {*} res
* @returns {object} Json response
* @memberof followContoller
*/
static async getFollowing(req, res) {
try {
const { userName } = req.params;
const user = await User.findOne({
where: { userName }
});

if (!user) {
return response(res, 404, 'failure', 'User not found');
}

const userId = user.dataValues.id;

const following = await Follow.findAll({
where: { followersId: userId },
attributes: [],
include: [
{
model: User,
as: 'followedUser',
attributes: { exclude: ['email', 'password', 'isVerified', 'notifySettings', 'roleId', 'authTypeId', 'createdAt', 'updatedAt'] }
}
]
});

if (following.length === 0) {
return response(res, 200, 'success', 'You are not following anyone');
}
const payload = {
following: following.map(followingUser => followingUser.followedUser),
get followCount() {
return this.following.length;
}
};

response(
res,
200,
'success',
'Following returned successfully',
null,
payload
);
} catch (error) {
response(res, 500, 'failure', error.name);
}
}
}
export default followContoller;
21 changes: 11 additions & 10 deletions server/helpers/response.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/**
*
* @description Method to send response in a generic format.
* @param {*} res Express Response object
* @param {number} code HTTP response status code
* @param {string} status 'success' || 'failure'
* @param {string} message Message to user
* @param {object} error (optional) Error object
* @param {object} payload (optional) Payload data to return with the response
* @returns {object} Json response
*/
*
* @description Method to send response in a generic format.
* @param {*} res Express Response object
* @param {number} code HTTP response status code
* @param {string} status 'success' || 'failure'
* @param {string} message Message to user
* @param {object} error (optional) Error object
* @param {object} payload (optional) Payload data to return with the response
* @returns {object} Json response
*/

export default (res, code, status, message, error, payload) => {
res.status(code).json({
Expand Down
22 changes: 13 additions & 9 deletions server/middlewares/AuthMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class AuthMiddleware {
const { authorization } = req.headers;
if (!authorization) {
return response(
res, 401, 'failure', 'authentication error',
{ message: 'You are not logged in.' },
null
res,
401,
'failure',
'You are not logged in.'
);
}

Expand All @@ -35,16 +36,19 @@ class AuthMiddleware {
const { name } = error;
if (name === 'TokenExpiredError' || name === 'JsonWebTokenError') {
return response(
res, 401, 'failure', 'authentication error',
{ message: 'Token is invalid, You need to log in again' },
null
res,
401,
'failure',
'Token is invalid, You need to log in again'
);
}

return response(
res, 500, 'failure',
'server error',
{ message: 'Something went wrong on the server' }, null
res,
500,
'failure',
'An error occured on the server',
error
);
}
}
Expand Down
Loading

0 comments on commit 3f41608

Please sign in to comment.