From 4ae2ed9e4570afe3e93b7f6ef984c69148dff7e1 Mon Sep 17 00:00:00 2001 From: habinezadalvan Date: Mon, 28 Oct 2019 14:01:13 +0200 Subject: [PATCH] bug(comments of a particular article): view comments of an article [Finishes #169392403] --- src/controllers/comments.controller.js | 123 ++++++++++++++++++++++- src/controllers/user.controller.js | 8 +- src/routes/api/comment/comments.route.js | 3 + test/unit.test.js | 9 -- 4 files changed, 128 insertions(+), 15 deletions(-) delete mode 100644 test/unit.test.js diff --git a/src/controllers/comments.controller.js b/src/controllers/comments.controller.js index b7dc2e5..7b8655f 100644 --- a/src/controllers/comments.controller.js +++ b/src/controllers/comments.controller.js @@ -1,3 +1,5 @@ +import moment from 'moment'; +import _ from 'lodash'; import commentsService from '../services/comments.service'; import UserService from '../services/user.service'; import models from '../models'; @@ -6,7 +8,6 @@ import NotificationServices from '../services/notification.service'; import Util from '../helpers/util'; import StatsService from '../services/db.service'; - const util = new Util(); const { notifyUsersWhoFavorited } = NotificationServices; @@ -41,7 +42,7 @@ class Comments { articleSlug: getArticle.slug, userId: req.auth.id, body: req.body.body, - parentCommentId: req.body.parentCommentId, + parentCommentId: req.body.parentCommentId }; const createdComment = await commentsService.addComment(comment); await notifyUsersWhoFavorited(req, res, getArticle.id, req.params.slug); @@ -117,6 +118,119 @@ class Comments { return util.send(res); } + /** + * + * + * @static + * @param {*} req + * @param {*} res + * @returns {object} data + * @memberof Comments + */ + static async getAllCommentsOfArticle(req, res) { + const { offset, limit } = req.query; + const { articleSlug } = req.params; + const comments = await CommentsDb.findAll({ + where: { articleSlug }, + order: [['createdAt', 'DESC']], + offset, + limit + }); + if (comments.length === 0) { + await util.setError(404, 'No comments found'); + return util.send(res); + } + const allComments = _.map( + comments, + _.partialRight(_.pick, [ + 'id', + 'userId', + 'articleSlug', + 'body', + 'parentCommentId', + 'commentRevisions', + 'likesCount', + 'likeInfo', + 'createdAt' + ]) + ); + await Promise.all( + allComments.map(async (comment) => { + try { + const userDetails = await UserService.getOneUser(comment.userId); + const { username, image } = userDetails; + const timeAgo = moment(comment.createdAt).fromNow(); + comment.username = username; + comment.userImage = image; + comment.timeCreated = timeAgo; + return true; + } catch (error) { + throw error; + } + }) + ); + await util.setSuccess(200, 'All comments successfully retrieved', allComments); + return util.send(res); + } + + // delete a comment of a particular article. + /** + * + * + * @static + * @param {*} req + * @param {*} res + * @returns {object} data + * @memberof Comments + */ + static async deleteCommentOfAnArticle(req, res) { + const userId = req.auth.id; + const getComment = await CommentsDb.findOne({ where: { id: req.params.id } }); + if (!getComment) { + await util.setError(404, 'That article comment does not exit'); + return util.send(res); + } + const commentedUser = getComment.dataValues.userId; + if (userId !== commentedUser) { + await util.setError(403, 'Sorry, you are not allowed to delete this comment'); + return util.send(res); + } + await commentsService.deleteComment(req.params.id); + await util.setSuccess(200, 'Comment was successfully deleted'); + return util.send(res); + } + + /** + * + * + * @static + * @param {*} req + * @param {*} res + * @returns {Object} return comment updation message + * @memberof UserController + */ + static async updateParticularComment(req, res) { + const userId = req.auth.id; + const getComment = await CommentsDb.findOne({ where: { id: req.params.id } }); + if (!getComment) { + await util.setError(404, 'That article comment does not exit'); + return util.send(res); + } + const commentedUser = getComment.dataValues.userId; + if (userId !== commentedUser) { + await util.setError(403, 'Sorry, you are not allowed to update this comment'); + return util.send(res); + } + const { body } = req.body; + const commentRevisions = getComment.dataValues.body; + const updateComment = await commentsService.updateComment(req.params.id, { + body, + commentRevisions + }); + await util.setSuccess(200, 'Update is successfully', updateComment); + return util.send(res); + } + /** * * @@ -142,7 +256,10 @@ class Comments { const { body } = req.body; const commentRevisions = getComment.dataValues.body; - const updateComment = await commentsService.updateComment(req.params.id, { body, commentRevisions }); + const updateComment = await commentsService.updateComment(req.params.id, { + body, + commentRevisions + }); await util.setSuccess(200, 'Update is successfully', updateComment); return util.send(res); } diff --git a/src/controllers/user.controller.js b/src/controllers/user.controller.js index 19671aa..c7217a9 100644 --- a/src/controllers/user.controller.js +++ b/src/controllers/user.controller.js @@ -54,13 +54,14 @@ class UserController { email: theUser.email, username: theUser.username, role: theUser.role, - verified: theUser.verified + verified: theUser.verified, + image: theUser.image }; const token = await Helper.generateToken(payload); return res.status(200).send({ status: 200, message: `welcome back ${theUser.firstname}`, - token + token, }); } catch (error) { return res.status(404).send({ @@ -120,7 +121,8 @@ class UserController { id: createdUser.id, email: createdUser.email, role: createdUser.role, - verified: createdUser.verified + verified: createdUser.verified, + image: createdUser.image }; const token = await Helper.generateToken(payload); const verifyUrl = `${process.env.FRONT_END_URL}/verify?token=${token}`; diff --git a/src/routes/api/comment/comments.route.js b/src/routes/api/comment/comments.route.js index 56617c8..b963288 100644 --- a/src/routes/api/comment/comments.route.js +++ b/src/routes/api/comment/comments.route.js @@ -6,8 +6,11 @@ import confirmEmailAuth from '../../../middlewares/emailVarification.middleware' const router = express.Router(); router.post('/:slug', [auth, CommentsValidation], Comments.createComment); +router.get('/:articleSlug', Comments.getAllCommentsOfArticle); router.get('/', [auth], Comments.getComments); router.delete('/:id', [auth], Comments.deleteComment); +router.delete('/particularArticle/:id', [auth], Comments.deleteCommentOfAnArticle); +router.put('/particularArticle/:id', [auth, CommentsValidation], Comments.updateParticularComment); router.put('/:id', [auth, CommentsValidation], Comments.updateComment); router.get('/like/:id', [auth, confirmEmailAuth], Comments.getLikesComments); router.post('/like/:id', [auth, confirmEmailAuth], Comments.likeComment); diff --git a/test/unit.test.js b/test/unit.test.js deleted file mode 100644 index 656822a..0000000 --- a/test/unit.test.js +++ /dev/null @@ -1,9 +0,0 @@ -import { expect } from './test-setup'; -import { sendEmail } from '../src/helpers/verification-email'; - -describe('Users', () => { - it('should return something when email is send', async () => { - const results = await sendEmail('admin@gmail.com', 'admin', 'http://localhost:3000/api/v1/users/verify'); - expect(results[0]).to.be.an('object'); - }); -});