Skip to content

Commit

Permalink
feat: implement comment edit track history
Browse files Browse the repository at this point in the history
  • Loading branch information
Mireille Niwemuhuza authored and Mireille Niwemuhuza committed Jun 26, 2019
1 parent 8790552 commit 2621ebe
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 31 deletions.
81 changes: 60 additions & 21 deletions src/api/controllers/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,18 @@ export default class comments {
const { userId } = findComment[0].dataValues;
const { id, firstName } = req.user;
if (userId === id) {
const oldComment = findComment[0].dataValues.comment;
await models.Comment.update(
{
comment
},
{ where: { id: commentId } }
).then(() => {
).then(async () => {
await models.CommentsHistory.create({
userId: id,
editedComment: oldComment,
commentId: findComment[0].dataValues.id
});
return res.status(200).json({
message: 'Your comment has been edited',
data: {
Expand Down Expand Up @@ -129,28 +135,30 @@ export default class comments {
}
});
const { userId } = findComment[0].dataValues;
if (nestedComments[0]) {
await models.Comment.update(
{
comment:
'This comment has been deleted!'
},
{ where: { id: commentId } }
).then(() => {
return res.status(200).json({
message: 'Comment deleted'
if (userId === id || roles.includes('moderator' || 'admin')) {
if (nestedComments[0]) {
await models.Comment.update(
{
comment:
'This comment has been deleted!'
},
{ where: { id: commentId } }
).then(() => {
return res.status(200).json({
message: roles.includes('moderator' || 'admin') ? 'Comment deleted by moderator' : 'Comment deleted!'
});
});
});
} else if (userId === id || roles.includes('moderator' || 'admin')) {
await models.Comment.destroy({
where: {
id: commentId
}
}).then(() => {
return res.status(200).json({
message: roles.includes('moderator' || 'admin') ? 'Comment deleted by moderator' : 'Comment deleted!'
} else {
await models.Comment.destroy({
where: {
id: commentId
}
}).then(() => {
return res.status(200).json({
message: 'Comment deleted!'
});
});
});
}
}
return res.status(403).json({
message: `Dear ${firstName}, You do not have the right to delete this comment!`
Expand Down Expand Up @@ -333,4 +341,35 @@ export default class comments {
}
});
}

/**
* @description - Users should be able to track edit history
* @param {Object} req - Request Object
* @param {Object} res - Response Object
* @returns {Object} - Response object
*/
static async commentHistory(req, res) {
const { commentId } = req.params;
const { id } = req.user;
const findHistory = await models.CommentsHistory.findAll({
where: {
commentId
}
});
if (findHistory.length === 0) {
return res.status(404).json({
message: 'No edit history for this comment!'
});
}
if (findHistory[0].dataValues.userId !== id) {
return res.status(403).json({
message: 'You do not have the right to view this history!'
});
}
return res.status(200).json({
data: {
findHistory
}
});
}
}
6 changes: 5 additions & 1 deletion src/api/routes/articlesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const { bookmark } = bookmarkController;

const {
createComment, editComment, deleteComment, getComment, commentAcomment,
likeComment, dislikeComment, countLikes, countDislikes
likeComment, dislikeComment, countLikes, countDislikes, commentHistory
} = commentsController;
const { checkComment, checkParameter, articleExists } = comment;
const { liked, disliked } = checkLikesandDislikes;
Expand Down Expand Up @@ -74,5 +74,9 @@ articlesRouter.post('/comments/:commentId/dislike', verifyToken, checkParameter,
articlesRouter.get('/comments/:commentId/dislikes', checkParameter, countDislikes);
articlesRouter.get('/comments/:commentId/likes', checkParameter, countLikes);

// get comment edit history

articlesRouter.get('/comments/:commentId/history', verifyToken, checkParameter, commentHistory);


export default articlesRouter;
44 changes: 44 additions & 0 deletions src/sequelize/migrations/20190625184332-create-comments-history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@


module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('CommentsHistories', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
allowNull: false,
type: Sequelize.INTEGER,
references: {
model: 'Users',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
editedComment: {
type: Sequelize.STRING
},
commentId: {
allowNull: false,
type: Sequelize.INTEGER,
references: {
model: 'Comments',
key: 'id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('CommentsHistories')
};
8 changes: 8 additions & 0 deletions src/sequelize/models/commentshistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = (sequelize, DataTypes) => {
const CommentsHistory = sequelize.define('CommentsHistory', {
userId: DataTypes.INTEGER,
editedComment: DataTypes.STRING,
commentId: DataTypes.INTEGER
}, {});
return CommentsHistory;
};
32 changes: 30 additions & 2 deletions swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1135,13 +1135,19 @@
}
}
},
"/articles/comments/{commentId}/likes": {
"/articles/comments/{commentId}/history": {
"get": {
"tags": [
"comment"
],
"description": "Authors Haven visitors and users should be able see all likes on a comment",
"description": "Authors Haven users should be able see all track their comments edit history",
"parameters": [
{
"name": "token",
"in": "header",
"description": "The user token",
"required": true
},
{
"name": "commentId",
"in": "path",
Expand All @@ -1158,6 +1164,28 @@
}
}
},
"/articles/comments/{commentId}/likes": {
"get": {
"tags": [
"comment"
],
"description": "Authors Haven visitors and users should be able see all likes on a comment",
"parameters": [
{
"name": "commentId",
"in": "path",
"description": "Comment Id",
"required": true
}
],
"produces": [
"application/json"
],
"responses": {
"200": {}
}
}
},
"/profiles":{
"get": {
"tags": [
Expand Down
55 changes: 48 additions & 7 deletions test/comments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dotenv.config();
let userOneToken,
userTwoToken,
adminToken,
adminToken1,
userOneObject,
userTwoObject,
adminObject,
Expand Down Expand Up @@ -59,7 +60,7 @@ describe('Comments', () => {
email: 'admin@luffy.co',
password: process.env.TEST_USER_PSW,
confirmPassword: process.env.TEST_USER_PSW,
isAdmin: process.env.IS_ADMIN
roles: ['admin'],
};

testAdmin = await models.User.create(adminObject);
Expand Down Expand Up @@ -124,6 +125,17 @@ describe('Comments', () => {
done();
});
});
it('should notify the use if the comment to track does not exist', (done) => {
chai
.request(server)
.get(`/api/articles/comments/${commentId}/history`)
.set('token', userOneToken)
.end((err, res) => {
expect(res.status).to.equal(404);
expect(res.body).to.be.an('object');
done();
});
});
it('should let a user edit a comment', (done) => {
chai
.request(server)
Expand All @@ -138,6 +150,28 @@ describe('Comments', () => {
done();
});
});
it('should let a user track edit history', (done) => {
chai
.request(server)
.get(`/api/articles/comments/${commentId}/history`)
.set('token', userOneToken)
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body).to.be.an('object');
done();
});
});
it('should let only the owner of the comment track it', (done) => {
chai
.request(server)
.get(`/api/articles/comments/${commentId}/history`)
.set('token', userTwoToken)
.end((err, res) => {
expect(res.status).to.equal(403);
expect(res.body).to.be.an('object');
done();
});
});
it('should let only the owner of the comment edit it!', (done) => {
chai
.request(server)
Expand Down Expand Up @@ -280,12 +314,19 @@ describe('Comments', () => {
it('should let the admin delete any comment!', (done) => {
chai
.request(server)
.delete(`/api/articles/comments/${commentTwoId}`)
.set('token', adminToken)
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body).to.be.an('object');
done();
.post('/api/auth/login')
.send({ email: 'superuser@gmail.com', password: process.env.SUPER_ADMIN_PSW })
.end(async (err, res) => {
adminToken1 = res.body.data.token;
chai
.request(server)
.delete(`/api/articles/comments/${commentTwoId}`)
.set('token', adminToken1)
.end((err, res) => {
expect(res.status).to.equal(200);
expect(res.body).to.be.an('object');
done();
});
});
});
it('should let the user get an article with its comments!', (done) => {
Expand Down

0 comments on commit 2621ebe

Please sign in to comment.