From 906092c65a60305cc55796775a68014243a8e5b4 Mon Sep 17 00:00:00 2001 From: jean salvi <38810299+salviosage@users.noreply.github.com> Date: Thu, 24 Oct 2019 11:57:35 +0200 Subject: [PATCH] #169314391 refactor Article(s) and rest password (#69) * refactor (article(s), reset password) Start Finish #169314391 * fix travis --- .travis.yml | 2 +- package.json | 114 ++++++++++++------------- src/controllers/articles.controller.js | 77 +++++++++++------ src/controllers/user.controller.js | 2 +- 4 files changed, 109 insertions(+), 86 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5e1f147..f0f5b8f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: node_js node_js: - - 'stable' + - '10.16.0' cache: directories: diff --git a/package.json b/package.json index 7637e0a..3fcf1d4 100644 --- a/package.json +++ b/package.json @@ -17,65 +17,65 @@ "author": "Andela Simulations Programme", "license": "MIT", "dependencies": { - "@babel/cli": "^7.5.5", - "@babel/core": "^7.5.5", - "@babel/node": "^7.5.5", - "@babel/plugin-transform-runtime": "^7.5.5", - "@babel/polyfill": "^7.4.4", - "@babel/preset-env": "^7.5.5", - "@babel/register": "^7.5.5", - "@babel/runtime": "^7.5.5", - "@sendgrid/mail": "^6.4.0", - "bcrypt": "^3.0.6", - "body-parser": "^1.19.0", - "cloudinary": "^1.14.0", - "connect-multiparty": "^2.2.0", - "cors": "^2.8.5", - "dotenv": "^6.2.0", - "express": "^4.17.1", - "express-async-errors": "^3.1.1", - "express-session": "^1.15.6", - "joi": "^14.3.1", - "jsonwebtoken": "^8.5.1", - "lodash": "^4.17.15", - "method-override": "^2.3.10", - "moment": "^2.24.0", - "multer": "^1.4.2", - "nyc": "^14.1.1", - "open": "^6.4.0", - "passport": "^0.4.0", - "passport-facebook": "^3.0.0", - "passport-google-oauth": "^2.0.0", - "passport-twitter": "^1.0.4", - "pg": "^7.12.0", - "pg-hstore": "^2.3.3", - "pusher": "^2.2.2", - "request": "^2.87.0", - "sequelize": "^5.11.0", - "sequelize-cli": "^5.5.0", - "slug": "^1.1.0", - "swagger-jsdoc": "^3.3.0", - "swagger-ui-express": "^4.0.7", - "uniqid": "^5.0.3", - "validator": "^11.1.0", - "winston": "^3.2.1" + "@babel/cli": "7.5.5", + "@babel/core": "7.5.5", + "@babel/node": "7.5.5", + "@babel/plugin-transform-runtime": "7.5.5", + "@babel/polyfill": "7.4.4", + "@babel/preset-env": "7.5.5", + "@babel/register": "7.5.5", + "@babel/runtime": "7.5.5", + "@sendgrid/mail": "6.4.0", + "bcrypt": "3.0.6", + "body-parser": "1.19.0", + "cloudinary": "1.14.0", + "connect-multiparty": "2.2.0", + "cors": "2.8.5", + "dotenv": "6.2.0", + "express": "4.17.1", + "express-async-errors": "3.1.1", + "express-session": "1.15.6", + "joi": "14.3.1", + "jsonwebtoken": "8.5.1", + "lodash": "4.17.15", + "method-override": "2.3.10", + "moment": "2.24.0", + "multer": "1.4.2", + "nyc": "14.1.1", + "open": "6.4.0", + "passport": "0.4.0", + "passport-facebook": "3.0.0", + "passport-google-oauth": "2.0.0", + "passport-twitter": "1.0.4", + "pg": "7.12.0", + "pg-hstore": "2.3.3", + "pusher": "2.2.2", + "request": "2.87.0", + "sequelize": "5.11.0", + "sequelize-cli": "5.5.0", + "slug": "1.1.0", + "swagger-jsdoc": "3.3.0", + "swagger-ui-express": "4.0.7", + "uniqid": "5.0.3", + "validator": "11.1.0", + "winston": "3.2.1" }, "devDependencies": { - "@babel/cli": "^7.5.5", - "@babel/core": "^7.5.5", - "@babel/node": "^7.5.5", - "@babel/preset-env": "^7.5.5", - "@babel/register": "^7.5.5", - "chai": "^4.2.0", - "chai-http": "^4.3.0", - "coveralls": "^3.0.5", - "eslint": "^6.1.0", - "eslint-config-airbnb-base": "^13.2.0", - "eslint-plugin-import": "^2.18.2", - "mocha": "^6.2.0", - "nodemon": "^1.19.1", - "sinon": "^7.4.1", - "sinon-chai": "^3.3.0" + "@babel/cli": "7.5.5", + "@babel/core": "7.5.5", + "@babel/node": "7.5.5", + "@babel/preset-env": "7.5.5", + "@babel/register": "7.5.5", + "chai": "4.2.0", + "chai-http": "4.3.0", + "coveralls": "3.0.5", + "eslint": "6.1.0", + "eslint-config-airbnb-base": "13.2.0", + "eslint-plugin-import": "2.18.2", + "mocha": "6.2.0", + "nodemon": "1.19.1", + "sinon": "7.4.1", + "sinon-chai": "3.3.0" }, "nyc": { "exclude": [ diff --git a/src/controllers/articles.controller.js b/src/controllers/articles.controller.js index 548a296..f8f25c6 100644 --- a/src/controllers/articles.controller.js +++ b/src/controllers/articles.controller.js @@ -8,10 +8,12 @@ import Userservice from '../services/user.service'; import articleService from '../services/article.service'; import Helper from '../helpers/helper'; import NotificationServices from '../services/notification.service'; -import cloudinaryHelper from '../helpers/cloudinaryHelper'; +// import cloudinaryHelper from '../helpers/cloudinaryHelper'; import OpenUrlHelper from '../helpers/share.article.helper'; import Util from '../helpers/util'; import statsService from '../services/db.service'; +import likeService from '../services/likes.service'; +import RateService from '../services/rate.service'; const { notifyViaEmailAndPush } = NotificationServices; const util = new Util(); @@ -98,6 +100,7 @@ class Articles { articles, _.partialRight(_.pick, [ 'authorId', + 'id', 'slug', 'title', 'description', @@ -116,13 +119,27 @@ class Articles { allArticles.map(async (article) => { try { const userDetails = await Userservice.getOneUser(article.authorId); - const { username, image } = userDetails; + const { + username, firstname, lastname, image + } = userDetails; + const user = { + username, + firstname, + lastname, + image + }; + const rating = await RateService.getArticleRatingStatistic(article.slug); + let claps = await likeService.getAllAClaps(req.params.Article); + claps = Object.values(claps)[0]; const readTime = Helper.calculateReadTime(article.body); const timeAgo = moment(article.createdAt).fromNow(); article.readtime = readTime; article.username = username; article.userImage = image; article.timeCreated = timeAgo; + article.claps = claps; + article.rating = rating.dataValues.rating; + article.author = user; return true; } catch (error) { throw error; @@ -173,6 +190,7 @@ class Articles { _.partialRight(_.pick, [ 'authorId', 'slug', + 'id', 'title', 'description', 'body', @@ -229,8 +247,10 @@ class Articles { } const article = _.pick(findArticle, [ + 'authorId', 'slug', 'title', + 'id', 'description', 'body', 'taglist', @@ -240,10 +260,26 @@ class Articles { 'images', 'views' ]); + const userDetails = await Userservice.getOneUser(article.authorId); + const { + username, firstname, lastname, image + } = userDetails; + const user = { + username, + firstname, + lastname, + image + }; const timeAgo = moment(article.createdAt).fromNow(); const readTime = Helper.calculateReadTime(article.body); + const rating = await RateService.getArticleRatingStatistic(article.slug); + let claps = await likeService.getAllAClaps(req.params.Article); + claps = Object.values(claps)[0]; article.readtime = readTime; article.createdAt = timeAgo; + article.rating = rating.dataValues.rating; + article.claps = claps; + article.author = user; if (req.auth) { const { description } = article; const readerId = req.auth.id; @@ -293,25 +329,18 @@ class Articles { where: { slug: req.params.slug } }); if (!findArticle) { - return res.status(200).json({ - status: 200, - message: 'That article does not exist!' - }); + util.setError(404, 'That article does not exist!'); + return util.send(res); } if (req.auth.id !== findArticle.authorId) { - return res.status(403).json({ - status: 403, - message: - 'Sorry you can not DELETE an article that does not belong to you.' - }); + util.setError(403, 'Sorry you can not DELETE an article that does not belong to you.'); + return util.send(res); } await db.destroy({ where: { slug: req.params.slug } }); - return res.status(200).json({ - status: 200, - message: 'Article was deleted succussfully!' - }); + util.setSuccess(200, 'Article was deleted succussfully!'); + return util.send(res); } /** @@ -328,16 +357,12 @@ class Articles { where: { slug: req.params.slug } }); if (!findArticle) { - return res - .status(200) - .json({ status: 200, message: 'That article does not exist' }); + util.setError(404, 'That article does not exist!'); + return util.send(res); } if (req.auth.id !== findArticle.authorId) { - return res.status(403).json({ - status: 403, - message: - 'Sorry you can not UPDATE an article that does not belong to you.' - }); + util.setError(401, 'Sorry you can not UPDATE an article that does not belong to you.'); + return util.send(res); } const { title, body, description } = req.body; const updatedArticle = await articleService.updateArticle(req.params.slug, { @@ -347,10 +372,8 @@ class Articles { description, taglist: req.body.taglist.split(' ') }); - return res.status(200).json({ - status: 200, - updatedArticle - }); + util.setSuccess(200, 'Article Updated successfully!', updatedArticle); + return util.send(res); } /** diff --git a/src/controllers/user.controller.js b/src/controllers/user.controller.js index cf29bda..19671aa 100644 --- a/src/controllers/user.controller.js +++ b/src/controllers/user.controller.js @@ -412,7 +412,7 @@ class UserController { const token = await Helper.generateToken(payload, (60 * 60)); // create password reset link - const resetUrl = `${process.env.FRONT_END_URL}/reset?token=${token}`; + const resetUrl = `${process.env.FRONT_END_URL}/resetpassword?token=${token}`; // send email to user email address const emailSent = await sendPasswordResetEmailHelper.sendEmail(user.email, user.username, resetUrl);