Skip to content

Commit

Permalink
chore(comment):changes on comment feature:
Browse files Browse the repository at this point in the history
-remove authorization on read comment
-add orderby createdAt on fecth comments
-fetch remaining comments after deleting a coment
-fetch updated comments after updating a comment
[finishes #167271972]
  • Loading branch information
Kabalisa committed Jul 16, 2019
1 parent 88b6783 commit e9df2e9
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 101 deletions.
4 changes: 1 addition & 3 deletions controllers/article.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ class ArticleController {
.status(200)
.send({ message: 'this article has not been shared yet' });
}
const numberOfSharesOnPlatform = await ArticleHelper.numberOfSharesOnPlatform(
shares
);
const numberOfSharesOnPlatform = await ArticleHelper.numberOfSharesOnPlatform(shares);
const facebook = numberOfSharesOnPlatform[0];
const twitter = numberOfSharesOnPlatform[1];
const linkedin = numberOfSharesOnPlatform[2];
Expand Down
12 changes: 2 additions & 10 deletions controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ class UserController {
);
if (isValidPassword) {
const token = helper.generateToken(userExist.dataValues);
return res.redirect(
`${process.env.FRONTEND_URL}/articles?token=${token}&username=${
req.user.username
}`
);
return res.redirect(`${process.env.FRONTEND_URL}/articles?token=${token}&username=${req.user.username}`);
}
return res.redirect(`${process.env.FRONTEND_URL}/verify/409`);
}
Expand All @@ -51,11 +47,7 @@ class UserController {
return res.status(500).send('Internal error');
}
const token = helper.generateToken(newUser.dataValues);
return res.redirect(
`${process.env.FRONTEND_URL}/articles?token=${token}&username=${
req.user.username
}`
);
return res.redirect(`${process.env.FRONTEND_URL}/articles?token=${token}&username=${req.user.username}`);
}

/**
Expand Down
54 changes: 42 additions & 12 deletions helpers/commentHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class CommentHelper {
const { slug } = req.params;
const fetchedComments = await Comment.findAll({
where: { titleSlug: slug },
order: [['createdAt', 'DESC']],
include: [
{ model: User, as: 'author', attributes: ['username', 'bio', 'image'] },
{
Expand All @@ -93,14 +94,12 @@ class CommentHelper {
if (!fetchedComments[0]) {
return { errors: { body: ['no commemts found on this article'] } };
}
await Promise.all(
fetchedComments.map(async (currentComment) => {
const commentId = currentComment.dataValues.id;
const commentHistories = await this.getCommentsHistories(commentId);
currentComment.dataValues.histories = commentHistories;
return currentComment;
})
);
await Promise.all(fetchedComments.map(async (currentComment) => {
const commentId = currentComment.dataValues.id;
const commentHistories = await this.getCommentsHistories(commentId);
currentComment.dataValues.histories = commentHistories;
return currentComment;
}));
return fetchedComments;
}

Expand Down Expand Up @@ -164,11 +163,29 @@ class CommentHelper {
const { id } = req.params;
await Comment.destroy({ where: { titleSlug: slug, id } });
const destroyed = await Comment.findOne({ where: { titleSlug: slug, id } });
const remaining = await Comment.findAll({
where: { titleSlug: slug },
order: [['createdAt', 'DESC']],
include: [
{ model: User, as: 'author', attributes: ['username', 'bio', 'image'] },
{
model: CommentFeedback,
as: 'like',
attributes: ['feedback', 'userId']
}
],
attributes: ['id', 'createdAt', 'updatedAt', 'body']
});
if (destroyed === null) {
await CommentHistory.destroy({ where: { parentComment: id } });
return { message: `comment with id ${id} have been deleted` };
return {
message: `comment with id ${id} have been deleted`,
remainingComments: remaining
};
}
return { message: `comment with id ${id} have failed to be deleted` };
return {
message: `comment with id ${id} have failed to be deleted`
};
}

/**
Expand All @@ -187,11 +204,24 @@ class CommentHelper {
);
await this.saveCommentHistory({ body: comment, parentComment: id });

const updatedComment = await Comment.findOne({ where: { id } });
const updatedComment = await Comment.findOne({
where: { id },
include: [
{ model: User, as: 'author', attributes: ['username', 'bio', 'image'] },
{
model: CommentFeedback,
as: 'like',
attributes: ['feedback', 'userId']
}
],
attributes: ['id', 'createdAt', 'updatedAt', 'body']
});
const commentHistories = await this.getCommentsHistories(id);
updatedComment.dataValues.histories = commentHistories;

return { response: updatedComment };
const updatedComments = await this.getComments({ params: { slug } });

return { response: updatedComment, comments: updatedComments };
}

/**
Expand Down
26 changes: 12 additions & 14 deletions helpers/userHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,18 @@ class UserHelper {
where: { id: { [Sequelize.Op.ne]: userId } },
attributes: ['id', 'username', 'bio', 'image']
});
await Promise.all(
users.map(async (currentUser) => {
const conditions = {
following: userId,
follower: currentUser.dataValues.id
};
const userFollowing = await recordHelper.findRecord(
Follows,
conditions
);
currentUser.dataValues.following = !!userFollowing;
return currentUser;
})
);
await Promise.all(users.map(async (currentUser) => {
const conditions = {
following: userId,
follower: currentUser.dataValues.id
};
const userFollowing = await recordHelper.findRecord(
Follows,
conditions
);
currentUser.dataValues.following = !!userFollowing;
return currentUser;
}));

return users;
}
Expand Down
5 changes: 4 additions & 1 deletion models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ const articles = (sequelize, DataTypes) => {
);
Article.associate = (models) => {
Article.belongsTo(models.User, { as: 'author' });
Article.hasMany(models.Rating, { foreignKey: 'articleSlug', allowNull: false });
Article.hasMany(models.Rating, {
foreignKey: 'articleSlug',
allowNull: false
});
Article.hasMany(models.Like, {
foreignKey: 'titleSlug',
sourceKey: 'slug'
Expand Down
9 changes: 7 additions & 2 deletions models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ const users = (sequelize, DataTypes) => {
},
notificationSettings: {
type: DataTypes.ARRAY(DataTypes.STRING),
defaultValue: ['receiveEmail', 'receiveInApp', 'onfollowPublish', 'onArticleFavoritedInteraction']
defaultValue: [
'receiveEmail',
'receiveInApp',
'onfollowPublish',
'onArticleFavoritedInteraction'
]
},
token: {
type: DataTypes.STRING,
Expand All @@ -56,7 +61,7 @@ const users = (sequelize, DataTypes) => {
isVerified: {
type: DataTypes.BOOLEAN,
defaultValue: false
},
}
},
{}
);
Expand Down
6 changes: 1 addition & 5 deletions routes/api/comment/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ router.post(
catchErrors(commentHelper.isValid),
catchErrors(comment.addComment)
);
router.get(
'/:slug/comments',
catchErrors(Auth),
catchErrors(comment.getComments)
);
router.get('/:slug/comments', catchErrors(comment.getComments));
router.get(
'/:slug/comments/:id',
catchErrors(Auth),
Expand Down
Loading

0 comments on commit e9df2e9

Please sign in to comment.