Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/andela/hermes-ah-backend
Browse files Browse the repository at this point in the history
…into feature/165429968-user-get-bookmark-articles
  • Loading branch information
Anayo Oleru authored and Anayo Oleru committed Apr 18, 2019
2 parents 92d498e + 1887808 commit daaa14f
Show file tree
Hide file tree
Showing 14 changed files with 1,757 additions and 2,165 deletions.
3,661 changes: 1,505 additions & 2,156 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"mocha": "^6.1.3",
"nock": "^10.0.6",
"nodemon": "^1.18.10",
"nyc": "^13.3.0",
"nyc": "^14.0.0",
"prettier": "1.16.4"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions server/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
database: process.env.DB_DATABASE_TEST,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
logging: false,
dialect,
},
production: {
Expand Down
19 changes: 18 additions & 1 deletion server/controllers/article.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ import statistic from '../helpers/statistics-storer';
import notifications from '../helpers/notifications';

const { findArticle } = serchDatabase;
const { Article, User, Reported_articles: ReportedArticle, Highlight } = model;
const {
Article,
User,
Reported_articles: ReportedArticle,
Highlight,
Like,
Bookmark,
Rating,
} = model;

/**
* @description Get Article
Expand Down Expand Up @@ -45,6 +53,15 @@ const getOneArticle = async (req, res) => {
'bio',
],
},
{
model: Like,
},
{
model: Bookmark,
},
{
model: Rating,
},
],
});

Expand Down
12 changes: 9 additions & 3 deletions server/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const loginController = async (req, res) => {
is_admin: isAdmin,
bio,
image_url: image,
is_reviewer: isReviewer,
} = user;
const verifyPassword = authHelper.comparePassword(hashedPassword, password);

Expand All @@ -44,7 +45,7 @@ const loginController = async (req, res) => {
},
});
}
const token = authHelper.encode({ id, isAdmin });
const token = authHelper.encode({ id, isAdmin, isReviewer });

return res.status(200).json({
user: {
Expand Down Expand Up @@ -83,8 +84,13 @@ const signupController = async (req, res) => {
return res.status(204).json({ errors: { body: ['User not created'] } });
}

const { id, is_admin: isAdmin, first_name: firstName } = user;
const token = authHelper.encode({ id, isAdmin });
const {
id,
is_admin: isAdmin,
first_name: firstName,
is_reviewer: isReviewer,
} = user;
const token = authHelper.encode({ id, isAdmin, isReviewer });

const verificationToken = authHelper.encode({ email });
const verificationLink = `${req.protocol}://${req.get(
Expand Down
31 changes: 30 additions & 1 deletion server/controllers/get-articles.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import models from '../models';
import validations from '../helpers/validations';
import serverError from '../helpers/server-error';

const { Article, User } = models;
const { Article, User, Reported_articles: ReportedArticles } = models;

const getArticles = async (req, res) => {
if (validations.validateArticlePage(req.params.page)) {
Expand Down Expand Up @@ -59,8 +59,37 @@ const getArticles = async (req, res) => {
});
};

const getAllReportedArticles = async (req, res) => {
try {
if (!validations.validReportedArticleQueryString(req.query)) {
return res.status(400).json({
errors: {
body: ['Invalid query string'],
},
});
}
const reportedArticles = await ReportedArticles.findAll({
where: req.query,
include: [
{
model: Article,
as: 'article',
},
],
});

return res.status(200).json({
reportedArticles,
});
} catch (error) {
return res.status(500).json({
errors: serverError(),
});
}
};
const Articles = {
getArticles,
getAllReportedArticles,
};

export default Articles;
2 changes: 2 additions & 0 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import adminController from './admin.controllers';
import bookmarkController from './bookmark.controller';
import requestController from './user-request.controllers';
import searchArticles from './search-article.controllers';
import getAllReportedArticles from './get-articles.controllers';

export default {
profileController,
Expand All @@ -28,4 +29,5 @@ export default {
bookmarkController,
requestController,
searchArticles,
getAllReportedArticles,
};
13 changes: 13 additions & 0 deletions server/helpers/validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ const draftPublishMessage = isDraft => {
return 'Article published successfully';
};

const validReportedArticleQueryString = query => {
let valid = true;
const searchableArticleField = ['status'];

Object.keys(query).forEach(item => {
if (!searchableArticleField.includes(item)) {
valid = false;
}
});

return valid;
};
const validations = {
verifyUUID,
validateArticlePage,
Expand All @@ -106,6 +118,7 @@ const validations = {
validEditableProfileBody,
compareFieldWithToken,
draftPublishMessage,
validReportedArticleQueryString,
};

export default validations;
15 changes: 15 additions & 0 deletions server/middlewares/verify-token.middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,24 @@ const verifyAdmin = (req, res, next) => {
return next();
};

const isVerifiedReviewer = (req, res, next) => {
const payload = validations.verifyAuthHeader(req);
const { isReviewer, isAdmin } = payload.userObj;
if (!isReviewer && !isAdmin) {
return res.status(403).json({
errors: {
body: ['You are not authorized to access this endpoint.'],
},
});
}
req.user = payload;
return next();
};

const tokenValidator = {
verifyToken,
verifyAdmin,
isVerifiedReviewer,
};

export default tokenValidator;
16 changes: 15 additions & 1 deletion server/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,24 @@ module.exports = (sequelize, DataTypes) => {
],
}
);
Article.associate = models =>
Article.associate = models => {
Article.belongsTo(models.User, {
foreignKey: 'user_id',
as: 'author',
});

Article.hasMany(models.Like, {
foreignKey: 'article_id',
});

Article.hasMany(models.Bookmark, {
foreignKey: 'article_id',
});

Article.hasMany(models.Rating, {
foreignKey: 'article_id',
});
};

return Article;
};
7 changes: 5 additions & 2 deletions server/models/reported_article.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,15 @@ module.exports = (sequelize, DataTypes) => {
},
});
Reported.associate = models => {
Reported.belongsTo(models.Article, {
const { Article, User } = models;
Reported.belongsTo(Article, {
foreignKey: 'reported_article_id',
as: 'article',
});
Reported.belongsTo(models.User, {
Reported.belongsTo(User, {
foreignKey: 'reporter_id',
foreignKey: 'reported_user_id',
as: 'user',
});
};

Expand Down
39 changes: 39 additions & 0 deletions server/routes/get-articles.routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import express from 'express';

import Articles from '../controllers/get-articles.controllers';
import middlewares from '../middlewares';

const { tokenValidator } = middlewares;

const router = express.Router();

Expand Down Expand Up @@ -28,4 +31,40 @@ const router = express.Router();

router.get('/articles/:page', Articles.getArticles);

/**
* @swagger
*
* /reported-articles:
* get:
* tags:
* - article
* description: a verified reviewer can get all reported articles
* produces:
* - application/json
* parameters:
* - in: query
* name: status
* description: Requires status query string
* required: true
* schema:
* $ref: '#/definitions/article'
* responses:
* 200:
* description: Success
* 400:
* description: Bad request
* 401:
* description: unauthorized
* 403:
* description: Forbidden
* 500:
* description: ran
*/
router.get(
'/reported-articles',
tokenValidator.verifyToken,
tokenValidator.isVerifiedReviewer,
Articles.getAllReportedArticles
);

export default router;
18 changes: 18 additions & 0 deletions server/seeders/20190331205705-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,23 @@ module.exports = {
is_notified: false,
research_field: 'Physics',
},
{
id: 'c89e0df9-4865-49e1-92ad-b83a2d1c4a4e',
first_name: 'Jane',
last_name: 'Doe',
title: 'Dr',
phone_number: '(934) 915-1648',
email: 'janedoe@yahoo.com',
bio: 'Worked as a lecturer...',
password:
'$2a$08$FaLCM57LR8X4apZYpKeVb.1XC082FTmkhWp3//j3TVr2XHYg.fuDK',
is_activated: true,
image_url: 'https://picsum.photos/g/200/300',
is_admin: false,
is_reviewer: true,
is_reported: false,
is_notified: false,
research_field: 'Physics',
},
]),
};
Loading

0 comments on commit daaa14f

Please sign in to comment.