Skip to content

Commit

Permalink
Merge 18e138d into ccfde98
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramadhan0 committed Jul 22, 2019
2 parents ccfde98 + 18e138d commit 7c9a519
Show file tree
Hide file tree
Showing 10 changed files with 405 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import models from '../models';
import status from '../../helpers/constants/status.codes';
import paginateUser from '../../helpers/generate.pagination.details';
import models from '../../models';
import status from '../../../helpers/constants/status.codes';
import paginateUser from '../../../helpers/generate.pagination.details';

const generatePagination = paginateUser;

Expand Down
73 changes: 73 additions & 0 deletions src/api/controllers/admin/commentReportController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import models from '../../models';
import status from '../../../helpers/constants/status.codes';

const { Comment } = models;

/**
* block and unblock reported comments
*
* @export
* @class commentReportController
*/
export default class commentReportController {
/**
* block Comment
*
* @static
* @param {object} req - request body
* @param {object} res - response body
* @returns { object } - response
* @memberof commentReportController
*/
static async blockComment(req, res) {
const { id } = req.params;
const { body } = req;
const isBlocked = true;

await Comment.update({
isBlocked
},
{
where: {
id
}
});
return res.status(status.OK).json({
status: status.OK,
message: `${body}:: blocked successfully`,
data: {
body
}
});
}

/**
* unblock Comment
*
* @static
* @param {object} req - request body
* @param {object} res - response body
* @returns { object } - response
* @memberof commentReportController
*/
static async unBlockComment(req, res) {
const { id } = req.params;
const isBlocked = false;
const { body } = req;
await Comment.update({
isBlocked
},
{
where: {
id
}
});
return res.status(status.OK).json({
status: status.OK,
message: `${body}:: unblocked successfully`,
data: {
body
}
});
}
}
8 changes: 8 additions & 0 deletions src/api/migrations/20190718162333-comment-isBlocked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
up: (queryInterface, Sequelize) => queryInterface.addColumn('Comments', 'isBlocked', {
type: Sequelize.BOOLEAN,
defaultValue: false
}),

down: queryInterface => queryInterface.removeColumn('Comments', 'isBlocked')
};
4 changes: 4 additions & 0 deletions src/api/models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export default (sequelize, DataTypes) => {
parentCommentId: {
type: DataTypes.INTEGER,
allowNull: true
},
isBlocked: {
type: DataTypes.BOOLEAN,
defaultValue: false
}
},
{
Expand Down
19 changes: 18 additions & 1 deletion src/api/routes/adminRouter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Router } from 'express';
import authController from '../controllers/authController';
import adminPermissionsController from '../controllers/adminPermissionsController';
import adminPermissionsController from '../controllers/admin/adminPermissionsController';
import reportedCommentsAdminController from '../controllers/admin/commentReportController';
import ValidateUser from '../../middlewares/ValidateUser';
import validateToken from '../../middlewares/checkValidToken';
import checkUser from '../../middlewares/checkUser';
Expand All @@ -10,6 +11,8 @@ import articleController from '../controllers/articleController';
import checkIfArticleExist from '../../middlewares/getOneArticle';
import CheckIfModerator from '../../middlewares/CheckIfModerator';
import CheckIfBlocked from '../../middlewares/isArticleBlocked';
import CheckIfCommentBlocked from '../../middlewares/isCommentBlocked';
import checkIfCommentExist from '../../middlewares/checkIfCommentExist';

const adminRouter = new Router();

Expand Down Expand Up @@ -92,4 +95,18 @@ adminRouter.patch('/article/:slug/unblock',
CheckIfBlocked.checkUnBlocked,
adminPermissionsController.unBlockArticle);

adminRouter.patch('/comments/:id/block',
validateToken,
CheckIfModerator.CheckAdmins,
checkIfCommentExist.checkIfExist,
CheckIfCommentBlocked.checkBlocked,
reportedCommentsAdminController.blockComment);

adminRouter.patch('/comments/:id/unblock',
validateToken,
CheckIfModerator.CheckAdmins,
checkIfCommentExist.checkIfExist,
CheckIfCommentBlocked.checkUnBlocked,
reportedCommentsAdminController.unBlockComment);

export default adminRouter;
22 changes: 22 additions & 0 deletions src/api/seeders/20190718163410-reported-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default {
up: queryInterface => queryInterface.bulkInsert('ReportComments',
[
{
userId: 12,
reportedCommentId: 5,
reasonId: 2,
createdAt: new Date(),
updatedAt: new Date()
},
{
userId: 13,
reportedCommentId: 6,
reasonId: 1,
createdAt: new Date(),
updatedAt: new Date()
}
],
{}),

down: queryInterface => queryInterface.bulkDelete('ReportComments', null, {})
};
73 changes: 73 additions & 0 deletions src/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,79 @@
}
}
},
"/admin/comment/block/:id": {
"patch": {
"tags": ["Admin"],
"summary": "allow Admin and moderator to block a comment a moderetor",
"description": "",
"produces": ["application/json"],
"parameters": [
{
"in": "path",
"name": "token",
"description": "must be admin or moderator",
"required": true
},
{
"in": "path",
"name": "email",
"description": "block comment",
"required": true
}
],
"responses": {
"200": {
"description": "comment blocked successfully"
},
"401": {
"description": "Please you must be the Admin in order to modify it, Thanks"
},
"403": {
"description": "comment is already a blocked"
},
"default": {
"description": "Something went wrong"
}
}
}
},

"/admin/comment/unblock/:id": {
"patch": {
"tags": ["Admin"],
"summary": "allow Admin and moderator to unblock a comment a moderetor",
"description": "",
"produces": ["application/json"],
"parameters": [
{
"in": "path",
"name": "token",
"description": "must be admin or moderator",
"required": true
},
{
"in": "path",
"name": "email",
"description": "unblock comment",
"required": true
}
],
"responses": {
"200": {
"description": "comment unblocked successfully"
},
"401": {
"description": "Please you must be the Admin in order to modify it, Thanks"
},
"403": {
"description": "comment is not blocked"
},
"default": {
"description": "Something went wrong"
}
}
}
},

"externalDocs": {
"description": "Find out more about Swagger",
Expand Down
39 changes: 39 additions & 0 deletions src/middlewares/checkIfCommentExist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import models from '../api/models';
import statusCode from '../helpers/constants/status.codes';
import sendError from '../helpers/error.sender';

const { Comment } = models;
/**
* check Comment middleware
*
* @export
* @class checkIfCommentExist
*/
class checkIfCommentExist {
/**
* this is a middleware which checks if comment exist.
*
* @author R
* @static
* @param {object} req the request
* @param {object} res the response to be sent
* @param { object } next the next route controller to be called
* @memberof checkIfCommentExist
* @returns {Object} res
*/
static async checkIfExist(req, res, next) {
const { id } = req.params;
const result = await Comment.findOne({
where: {
id
}
});
if (result) req.article = result.dataValues;

return result
? next()
: sendError(statusCode.NOT_FOUND, res, 'id', `Comment with id ${id} is not found, Thanks`);
}
}

export default checkIfCommentExist;
68 changes: 68 additions & 0 deletions src/middlewares/isCommentBlocked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import models from '../api/models';
import status from '../helpers/constants/status.codes';
import sendError from '../helpers/error.sender';

const { Comment } = models;
/**
* check if Comment is blocked
*
* @export
* @class checkIfBlocked
*/
class checkIfBlocked {
/**
* this is a middleware which checks if a Comment is blocked.
*
* @static
* @param {object} req the request
* @param {object} res the response
* @param { object } next the next route controller to be called
* @memberof checkComment
* @returns {Object} res
*/
static async checkBlocked(req, res, next) {
const { id } = req.params;

const result = await Comment.findOne({
where: {
id,
isBlocked: false
}
});

if (result) {
req.body = result.dataValues.body;
return next();
}
sendError(status.BAD_REQUEST, res, 'isBlocked', `${id} is already blocked`);
}

/**
* this is a middleware which checks if a Comment is not blocked.
*
* @static
* @param {object} req the request
* @param {object} res the response
* @param { object } next the next route controller to be called
* @memberof checkComment
* @returns {Object} res
*/
static async checkUnBlocked(req, res, next) {
const { id } = req.params;

const result = await Comment.findOne({
where: {
id,
isBlocked: true
}
});

if (result) {
req.body = result.dataValues.body;
return next();
}
sendError(status.BAD_REQUEST, res, 'isBlocked', `${id} is not blocked`);
}
}

export default checkIfBlocked;
Loading

0 comments on commit 7c9a519

Please sign in to comment.