Skip to content

Commit

Permalink
feat(reportArticles): endpoint to make reports on articles
Browse files Browse the repository at this point in the history
- Create model for reports and associate to users and articles.
- Write tests for the model and features.
- Define endpoints for:
	- Users to report an article
	- Admin to see reports made on articles
	- Admin to block and unblock articles
	- Admin to view all blocked articles
- Update swagger documentation for comments and article reports

[Delivers #159987728]
  • Loading branch information
emekafredy committed Sep 14, 2018
1 parent f5eeef4 commit 6605ecf
Show file tree
Hide file tree
Showing 16 changed files with 1,677 additions and 462 deletions.
224 changes: 224 additions & 0 deletions server/controllers/adminHandleReports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import {
Article, User, ReportsOnArticle
} from '../models';

/**
*
*
* @class Comments
*/
class AdminRolesController {
/**
* @description get all reports made by users on all articles
* @static
* @param {object} req express request object
* @param {object} res express response object
* @param {object} next next middleware
* @returns {next} next calls next
* @memberof AdminRolesController
*/
static getAllReports(req, res, next) {
ReportsOnArticle.findAll({
include: [{
model: Article,
as: 'article',
attributes: {
exclude: ['authorId', 'likeDislikeId', 'timeToRead', 'createdAt', 'updatedAt']
},
include: [{
model: User,
as: 'author',
attributes: ['username', 'email', 'bio', 'image']
}]
}]
})
.then(reports => res.status(200).json({
reports
}))
.catch(next);
}

/**
* @description get a single report made by a user on an article
* @static
* @param {object} req express request object
* @param {object} res express response object
* @param {object} next next middleware
* @returns {next} next calls next
* @memberof AdminRolesController
*/
static getSingleReport(req, res, next) {
const id = req.params.reportId;
ReportsOnArticle.findOne({
where: { id },
include: [{
model: Article,
as: 'article',
attributes: {
exclude: ['authorId', 'likeDislikeId', 'timeToRead', 'createdAt', 'updatedAt']
},
include: [{
model: User,
as: 'author',
attributes: ['username', 'email', 'bio', 'image']
}]
}]
})
.then(report => res.status(200).json({
report
}))
.catch(next);
}

/**
* @description get a single report made by a user on an article
* @static
* @param {object} req express request object
* @param {object} res express response object
* @param {object} next next middleware
* @returns {next} next calls next
* @memberof AdminRolesController
*/
static getReportsForSingleArticle(req, res, next) {
const slug = req.params.article_slug;

Article.findOne({
where: { slug },
include: [{
model: ReportsOnArticle,
as: 'reports',
attributes: {
exclude: ['userId', 'createdAt', 'updatedAt']
},
}, {
model: User,
as: 'author',
attributes: ['username', 'email', 'bio', 'image']
}]
})
.then(report => res.status(200).json({
report
}))
.catch(next);
}

/**
* @description block an article that violates terms
* @static
* @param {object} req express request object
* @param {object} res express response object
* @param {object} next next middleware
* @returns {next} next calls next
* @memberof AdminRolesController
*/
static blockArticle(req, res, next) {
const slug = req.params.article_slug;

Article.findOne({
where: {
slug,
displayStatus: true
}
})
.then((article) => {
if (!article) {
return res.status(404).json({
status: 'error',
error: {
message: 'Article does not exist'
}
});
}
return article.update({
displayStatus: false
})
.then(() => res.status(200).json({
status: 'success',
message: 'Article has been successfully blocked'
})).catch(next);
}).catch(next);
}

/**
* @description get list of blocked articles
* @static
* @param {object} req express request object
* @param {object} res express response object
* @param {object} next next middleware
* @returns {next} next calls next
* @memberof AdminRolesController
*/
static getBlockedArticles(req, res, next) {
Article.findAll({
where: {
displayStatus: false
}
})
.then(blockedArticles => res.status(200).json({
status: 'success',
blockedArticles
})).catch(next);
}

/**
* @description get a single blocked article
* @static
* @param {object} req express request object
* @param {object} res express response object
* @param {object} next next middleware
* @returns {next} next calls next
* @memberof AdminRolesController
*/
static getABlockedArticle(req, res, next) {
const slug = req.params.article_slug;
Article.findOne({
where: {
slug,
displayStatus: false
}
})
.then(blockedArticle => res.status(200).json({
status: 'success',
blockedArticle
})).catch(next);
}

/**
* @description unblock an article
* @static
* @param {object} req express request object
* @param {object} res express response object
* @param {object} next next middleware
* @returns {next} next calls next
* @memberof AdminRolesController
*/
static unblockArticle(req, res, next) {
const slug = req.params.article_slug;

Article.findOne({
where: {
slug,
displayStatus: false
}
})
.then((article) => {
if (!article) {
return res.status(404).json({
status: 'error',
error: {
message: 'Article does not exist'
}
});
}
return article.update({
displayStatus: true
})
.then(() => res.status(200).json({
status: 'success',
message: 'Article has been successfully unblocked'
})).catch(next);
}).catch(next);
}
}

export default AdminRolesController;
86 changes: 82 additions & 4 deletions server/controllers/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import articleValidation from '../utils/articles';
import paginateArticle from '../utils/articlesPaginate';
import articleNotification from '../utils/articleNotify';
import Search from './search';
import ReportInputValidation from '../utils/validateReportInput';

const {
Article, User, Tag, Comment
Article, User, Tag, Comment, ReportsOnArticle
} = db;

/**
Expand Down Expand Up @@ -155,6 +156,9 @@ class ArticleController {
const offset = (currentPage - 1) * limit;

return Article.findAndCountAll({
where: {
displayStatus: true,
},
include: [{
model: User,
as: 'author',
Expand Down Expand Up @@ -194,7 +198,10 @@ class ArticleController {
*/
static getSpecific(req, res) {
return Article.findOne({
where: { slug: req.params.article_slug },
where: {
slug: req.params.article_slug,
displayStatus: true,
},
include: [{
model: User,
as: 'author',
Expand Down Expand Up @@ -230,7 +237,10 @@ class ArticleController {
*/
static update(req, res) {
return Article.findOne({
where: { slug: req.params.article_slug },
where: {
slug: req.params.article_slug,
displayStatus: true,
},
include: [{
model: User,
as: 'author',
Expand Down Expand Up @@ -288,7 +298,12 @@ class ArticleController {
* @description deletes an article object with given slug in param.
*/
static delete(req, res) {
return Article.findOne({ where: { slug: req.params.article_slug } })
return Article.findOne({
where: {
slug: req.params.article_slug,
displayStatus: true
}
})
.then((article) => {
if (!article) {
return res.status(404).json({
Expand Down Expand Up @@ -346,6 +361,69 @@ class ArticleController {
});
}
}

/**
* @static
* @param {object} req
* @param {object} res
* @param {object} next
* @return {json} res
* @description deletes an article object with given slug in param.
*/
static reportOnArticle(req, res, next) {
const slug = req.params.article_slug;
const { userId, userName } = req;
const { reasonForReport, reportBody } = req.body;

const { error, isValid } = ReportInputValidation.validateArticleReportInput(req.body);
if (!isValid) {
return res.status(400).json({
error,
status: 'error'
});
}

Article.findOne({
where: { slug }
})
.then((article) => {
if (!article) {
return res.status(404).json({
status: 'error',
errors: {
message: 'Article does not exist',
}
});
}
return ReportsOnArticle.findOne({
where: { userId }
})
.then((found) => {
if (found) {
return res.status(400).json({
status: 'error',
error: {
message: 'We already have your report on this article'
}
});
}
return ReportsOnArticle.create({
userId,
username: userName,
articleId: article.id,
reasonForReport,
reportBody,
})
.then(report => res.status(200).json({
status: 'success',
report,
message: 'Thanks for the feedback. We will look into this.'
}))
.catch(next);
}).catch(next);
})
.catch(next);
}
}


Expand Down
Loading

0 comments on commit 6605ecf

Please sign in to comment.