Skip to content

Commit

Permalink
Merge 6d9e360 into f477660
Browse files Browse the repository at this point in the history
  • Loading branch information
Cavdy committed Jul 27, 2019
2 parents f477660 + 6d9e360 commit 244d8f6
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 5 deletions.
127 changes: 124 additions & 3 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { User, Follow, UserArchive } from '../db/models';
import findItem from '../helpers/findItem';
import findItem from '../helpers/findItem';

/**
* @description This class handles user requests
* @class UserController
Expand Down Expand Up @@ -197,7 +198,7 @@ class UserController {
* @returns {object} - response object
* @memberof UserController
*/
static async changeAccessLevel(req, res) {
static async changeAccessLevel(req, res) {
try {
const { userRole } = req.body;
const { userId } = req.params;
Expand Down Expand Up @@ -247,6 +248,126 @@ class UserController {
});
}
}
}

/**
*
*@description Get user followers
* @static
* @param {object} req
* @param {object} res
* @returns {object} res
* @memberof UserController
*/
static async getUserFollowers(req, res) {
try {
let offset = 0;

const { userId } = req.params;
const { currentPage, limit } = req.query; // page number
const defaultLimit = limit || 10; // number of records per page

offset = currentPage ? defaultLimit * (currentPage - 1) : 0;


const { count, rows: followers } = await Follow.findAndCountAll({
where: {
followee: userId
},
raw: true,
attributes: ['follower'],
include: [
{
model: User,
as: 'followers',
attributes: ['id', 'firstName', 'lastName', 'userName', 'imageUrl']
}
],
limit: defaultLimit,
offset
});

const pages = Math.ceil(count / limit) || 1;

return res.status(200).json({
status: 200,
message: 'All followers fetched successfully',
data: [
{
followers,
totalRating: count,
currentPage,
limit,
totalPages: pages
}
]
});
} catch (error) {
return res.status(500).json({
status: 500,
message: error.message,
});
}
}

/**
*
*@description Get user followers
* @static
* @param {object} req
* @param {object} res
* @returns {object} res
* @memberof UserController
*/
static async getUserFollowee(req, res) {
try {
let offset = 0;

const { userId } = req.params;
const { currentPage, limit } = req.query; // page number
const defaultLimit = limit || 10; // number of records per page

offset = currentPage ? defaultLimit * (currentPage - 1) : 0;


const { count, rows: followees } = await Follow.findAndCountAll({
where: {
follower: userId
},
raw: true,
attributes: ['followee'],
include: [
{
model: User,
as: 'following',
attributes: ['id', 'firstName', 'lastName', 'userName', 'imageUrl']
}
],
limit: defaultLimit,
offset
});

const pages = Math.ceil(count / limit) || 1;

return res.status(200).json({
status: 200,
message: 'All followees fetched successfully',
data: [
{
followees,
totalRating: count,
currentPage,
limit,
totalPages: pages
}
]
});
} catch (error) {
return res.status(500).json({
status: 500,
message: error.message,
});
}
}
}

export default UserController;
9 changes: 8 additions & 1 deletion src/db/models/follow.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ module.exports = (sequelize, DataTypes) => {
Follow.associate = (models) => {
// associations can be defined here
Follow.belongsTo(models.User, {
foreignKey: 'id',
foreignKey: 'followee',
as: 'following',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Follow.belongsTo(models.User, {
foreignKey: 'follower',
as: 'followers',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Expand Down
2 changes: 1 addition & 1 deletion src/db/seeders/20190701155734-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = {
bio: '#ghost',
isVerified: true,
role: 'admin',
password: 'password',
password: hash,
verificationToken: '',
imageUrl: 'image.png'
}, {
Expand Down
10 changes: 10 additions & 0 deletions src/routes/userRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,15 @@ router.delete('/:userId',
findItem.findUser,
UserController.deleteUser
);
router.get(
'/followers/:userId',
verify,
UserController.getUserFollowers
);
router.get(
'/followees/:userId',
verify,
UserController.getUserFollowee
);

export default router;
26 changes: 26 additions & 0 deletions test/follow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ describe('Testing Follow System Controller', () => {
},
);

it(
'user should get followers',
async () => {
const response = await chai.request(app)
.get(`${followRoute}followers/1`)
.set('token', token)
.send();
expect(response).to.be.an('object');
expect(response).to.have.status(200);
expect(response.body).to.have.property('message');
},
);

it(
'user should get followees',
async () => {
const response = await chai.request(app)
.get(`${followRoute}followees/1`)
.set('token', token)
.send();
expect(response).to.be.an('object');
expect(response).to.have.status(200);
expect(response.body).to.have.property('message');
},
);

it(
'user should not follow themselves',
async () => {
Expand Down

0 comments on commit 244d8f6

Please sign in to comment.