Skip to content

Commit

Permalink
[ft -#16370397] enable the admin to read the reported comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Mutabazi authored and Frank Mutabazi committed Jul 19, 2019
1 parent ccfde98 commit 5c1776e
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 4 deletions.
102 changes: 102 additions & 0 deletions src/api/controllers/reportedCommentAdminController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import models from '../models';
import status from '../../helpers/constants/status.codes';
import paginateUser from '../../helpers/generate.pagination.details';

const generatePagination = paginateUser;

const {
Comment, ReportedComment, User, Reason
} = models;

/**
* contains all method for an admin to read/delete/Block/unblock a comment
*
* @export
* @class AdminReportedCommentController
*/
class AdminReportedCommentController {
/**
* This function gets all the reported comment that are stored in the database
*
* @author Frank Mutabazi
* @static
* @param {Object} req the request
* @param {Object} res the response to be sent
* @memberof AdminReportedCommentController
* @returns {Object} res
*/
static async getAllReportedComments(req, res) {
const defaultOffset = 0;
const defaultLimit = 10;
const offset = req.query.offset || defaultOffset;
const limit = req.query.limit || defaultLimit;
const result = await ReportedComment.findAll({
attributes: ['createdAt'],
include: [
{
model: Comment,
required: true,
attributes: ['id', 'body', 'articleSlug']
},
{
model: User,
required: true,
attributes: ['username', 'email']
},
{
model: Reason,
required: true,
attributes: ['description']
}
],
offset,
limit
});
return res.status(status.OK).send({
status: status.OK,
paginationDetails: generatePagination(result.length, result, offset, limit),
data: result
});
}

/**
* fetch all reported Comments on a single comment
*
* @author Frank Mutabazi
* @static
* @param {object} req - request object
* @param {object} res - respond object
* @returns {array} response body
* @memberof AdminReportedCommentController
*/
static async singleReportedComment(req, res) {
const { id } = req.params;
const response = await Comment.findOne({
where: {
id
},
attributes: ['id', 'body', 'userId', 'articleSlug'],
include: [
{
model: Reason,
required: true,
attributes: ['id', 'description'],
through: {
model: ReportedComment,
attributes: ['userId']
}
}
]
});

return response ? res.status(status.OK).send({
status: status.OK,
data: response
}) : res.status(status.OK).send({
status: status.OK,
message: 'No reports for this comment.'
});
}
}

export default AdminReportedCommentController;
5 changes: 5 additions & 0 deletions src/api/models/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export default (sequelize, DataTypes) => {
onDelete: 'CASCADE',
hooks: true
});

Comment.belongsToMany(models.Reason, {
foreignKey: 'reportedCommentId',
through: 'ReportComments'
});
};
return Comment;
};
7 changes: 3 additions & 4 deletions src/api/models/reasons.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ export default (sequelize, DataTypes) => {
}
},
{});
Reasons.associate = ({ Report, ReportedComment }) => {
Reasons.associate = ({ Report, Comment }) => {
Reasons.hasMany(Report, {
foreignKey: 'reasonId',
sourceKey: 'id',
onDelete: 'cascade'
});

Reasons.hasMany(ReportedComment, {
Reasons.belongsToMany(Comment, {
foreignKey: 'reasonId',
sourceKey: 'id',
onDelete: 'cascade'
through: 'ReportComments'
});
};
return Reasons;
Expand Down
11 changes: 11 additions & 0 deletions src/api/routes/adminRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import articleController from '../controllers/articleController';
import checkIfArticleExist from '../../middlewares/getOneArticle';
import CheckIfModerator from '../../middlewares/CheckIfModerator';
import CheckIfBlocked from '../../middlewares/isArticleBlocked';
import AdminReportedComment from '../controllers/reportedCommentAdminController';

const adminRouter = new Router();

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

adminRouter.get('/comments/reported',
validateToken,
CheckIfModerator.CheckAdmins,
AdminReportedComment.getAllReportedComments);

adminRouter.get('/comments/:id/reported',
validateToken,
CheckIfModerator.CheckAdmins,
AdminReportedComment.singleReportedComment);

export default adminRouter;
53 changes: 53 additions & 0 deletions tests/admin.reported.comment.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import app from '../src/index';
import status from '../src/helpers/constants/status.codes';


let userToken = '';

const { expect } = chai;
const password = 'password23423';

chai.use(chaiHttp);

describe('test the controller for an admin to get reported comment', () => {
it('should login the moderetor to get the token', (done) => {
chai
.request(app)
.post('/api/users/login')
.send({
email: 'alain13@gmail.com',
password
})
.end((err, res) => {
res.should.have.status(status.OK);
userToken = res.body.user.token;
done();
});
});

it('Admin should be able to get all reported comments ', (done) => {
chai.request(app)
.get('/api/admin/comments/reported')
.set('Authorization', userToken)
.end((err, res) => {
expect(res.body).to.have.property('status').eql(status.OK);
expect(res.body).to.have.property('paginationDetails');
expect(res.body.data).to.be.an('array');
done();
});
});

it('Admin should be able to get all reported comments and respond when there are any reports', (done) => {
const id = 4;
chai.request(app)
.get(`/api/admin/comments/${id}/reported`)
.set('Authorization', userToken)
.end((err, res) => {
expect(res.body).to.have.property('status').eql(status.OK);
expect(res.body).to.have.property('message').eql('No reports for this comment.');
done();
});
});
});

0 comments on commit 5c1776e

Please sign in to comment.