Skip to content

Commit

Permalink
Merge fc258dd into afcae3c
Browse files Browse the repository at this point in the history
  • Loading branch information
sojida committed Apr 17, 2019
2 parents afcae3c + fc258dd commit c2527e5
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 7 deletions.
12 changes: 11 additions & 1 deletion server/controllers/profile.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ const patchProfile = async (req, res) => {
}
};

const controller = { getUserProfile, getProfileByField, patchProfile };
const suggestedResearchers = (req, res) =>
res.status(200).json({
suggestion: req.suggestions,
});

const controller = {
getUserProfile,
getProfileByField,
patchProfile,
suggestedResearchers,
};

export default controller;
10 changes: 10 additions & 0 deletions server/helpers/aurhors-id-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const getAuthorsIdFromArticle = (req, obj) => {
const allUserIds = obj.map(item => item.Article.author.id);

const uniqueIds = [...new Set(allUserIds)];

// returning all ids without the id of the user making the request
return uniqueIds.filter(item => item !== req.user.userObj.id);
};

export default getAuthorsIdFromArticle;
134 changes: 134 additions & 0 deletions server/middlewares/profile.middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import Joi from 'joi';
import joiFormater from '../helpers/joi-formater';
import profileSchema from '../joiSchema/profileSchema';
import validations from '../helpers/validations';
import models from '../models';
import getAuthorsId from '../helpers/aurhors-id-helper';
import serverError from '../helpers/server-error';

const { Like, User, Article, Comment, Statistic, Bookmark } = models;

const middleware = {
validateUpdateProfile(req, res, next) {
Expand Down Expand Up @@ -35,6 +40,135 @@ const middleware = {

return next();
},

async getAuthorOfArticleUserLiked(req, res, next) {
try {
req.suggestions = {};
const userId = req.user.userObj.id;

const likes = await Like.findAll({
where: {
user_id: userId,
},
include: [
{
model: Article,
attributes: ['title'],
include: [
{
model: User,
as: 'author',
attributes: ['id', 'first_name', 'last_name'],
},
],
},
],
});

req.suggestions.likes = getAuthorsId(req, likes);
return next();
} catch (error) {
return res.status(500).json({
errors: serverError(),
});
}
},

async getAuthorOfArticleUserBookmarked(req, res, next) {
try {
const userId = req.user.userObj.id;

const bookmarks = await Bookmark.findAll({
where: {
user_id: userId,
},
include: [
{
model: Article,
attributes: ['title'],
include: [
{
model: User,
as: 'author',
attributes: ['id', 'first_name', 'last_name'],
},
],
},
],
});

req.suggestions.bookmarks = getAuthorsId(req, bookmarks);
return next();
} catch (err) {
return res.status(500).json({
errors: serverError(),
});
}
},

async getAuthorOfArticleUserCommented(req, res, next) {
try {
const userId = req.user.userObj.id;

const comments = await Comment.findAll({
where: {
user_id: userId,
},
include: [
{
model: Article,
attributes: ['title'],
include: [
{
model: User,
as: 'author',
attributes: ['id', 'first_name', 'last_name'],
},
],
},
],
});

req.suggestions.comments = getAuthorsId(req, comments);
return next();
} catch (error) {
return res.status(500).json({
errors: serverError(),
});
}
},

async getAuthorOfUserStatistics(req, res, next) {
try {
const userId = req.user.userObj.id;

const statistics = await Statistic.findAll({
where: {
user_id: userId,
},
include: [
{
model: Article,
attributes: ['title'],
include: [
{
model: User,
as: 'author',
attributes: ['id', 'first_name', 'last_name'],
},
],
},
],
});

req.suggestions.statistics = getAuthorsId(req, statistics);
return next();
} catch (error) {
return res.status(500).json({
errors: serverError(),
});
}
},
};

export default middleware;
4 changes: 2 additions & 2 deletions server/models/bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ module.exports = (sequelize, DataTypes) => {
});
Bookmark.associate = models => {
Bookmark.belongsTo(models.Article, {
foreignKey: 'user_id',
foreignKey: 'article_id',
});
Bookmark.belongsTo(models.User, {
foreignKey: 'id',
foreignKey: 'user_id',
});
};
return Bookmark;
Expand Down
4 changes: 2 additions & 2 deletions server/models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ module.exports = (sequelize, DataTypes) => {
const { Article, User, Comment_history } = models;

Comment.belongsTo(Article, {
foreignKey: 'user_id',
foreignKey: 'article_id',
});
Comment.belongsTo(User, {
foreignKey: 'id',
foreignKey: 'user_id',
});

Comment.hasMany(Comment_history, {
Expand Down
4 changes: 2 additions & 2 deletions server/models/like.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ module.exports = (sequelize, DataTypes) => {
});
Like.associate = models => {
Like.belongsTo(models.Article, {
foreignKey: 'user_id',
foreignKey: 'article_id',
});
Like.belongsTo(models.User, {
foreignKey: 'id',
foreignKey: 'user_id',
});
};
return Like;
Expand Down
10 changes: 10 additions & 0 deletions server/routes/profile.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,14 @@ router.patch(
profileController.patchProfile
);

router.get(
'/suggested/researchers',
tokenValidator.verifyToken,
profileMiddleware.getAuthorOfArticleUserLiked,
profileMiddleware.getAuthorOfArticleUserBookmarked,
profileMiddleware.getAuthorOfArticleUserCommented,
profileMiddleware.getAuthorOfUserStatistics,
profileController.suggestedResearchers
);

export default router;
15 changes: 15 additions & 0 deletions tests/profile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,19 @@ describe('PROFILE', () => {
done();
});
});

it('should respond with an array of suggested followers', done => {
chai
.request(app)
.get(`/api/v1/suggested/researchers`)
.set('Authorization', userBToken)
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.suggestion).to.be.a('object');
expect(res.body.suggestion.comments).to.be.a('array');
expect(res.body.suggestion.likes).to.be.a('array');
expect(res.body.suggestion.bookmarks).to.be.a('array');
done();
});
});
});

0 comments on commit c2527e5

Please sign in to comment.