-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from andela/ft-reported-comment-admin-167370397
#[16370397] Admin/Moderator should be able to view reported comments and block them.
- Loading branch information
Showing
8 changed files
with
232 additions
and
8 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
...controllers/adminPermissionsController.js → ...llers/admin/adminPermissionsController.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module.exports = { | ||
up: queryInterface => queryInterface.bulkInsert('ReportComments', | ||
[ | ||
{ | ||
userId: 1, | ||
reportedCommentId: 6, | ||
reasonId: 2, | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, | ||
{ | ||
userId: 1, | ||
reportedCommentId: 6, | ||
reasonId: 2, | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
} | ||
], | ||
{}), | ||
|
||
down: queryInterface => queryInterface.bulkDelete('ReportComments', null, {}) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import generateToken from '../tokens/generate.token'; | ||
/** | ||
* A function to generate a token for an admin. | ||
* | ||
* @returns {object} token - The object that contains | ||
* the generated token | ||
*/ | ||
export default () => { | ||
const user = { | ||
username: 'BurindiAlain12', | ||
email: 'alain12@gmail.com', | ||
id: 12 | ||
}; | ||
const authToken = generateToken(user, process.env.TOKEN_KEY); | ||
return authToken; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import app from '../src/index'; | ||
import status from '../src/helpers/constants/status.codes'; | ||
import generateToken from '../src/helpers/tests/adminSignIn'; | ||
|
||
|
||
const userToken = generateToken(); | ||
|
||
const { expect } = chai; | ||
|
||
chai.use(chaiHttp); | ||
|
||
describe('test the controller for an admin to get reported comment', () => { | ||
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'); | ||
expect(res.body.data[0]).to.have.property('createdAt').eql(res.body.data[0].createdAt); | ||
expect(res.body.data[0]).to.have.property('Comment').to.be.an('object'); | ||
expect(res.body.data[0].Comment).to.have.property('id').eql(res.body.data[0].Comment.id); | ||
expect(res.body.data[0].Comment).to.have.property('body').eql(res.body.data[0].Comment.body); | ||
expect(res.body.data[0].Comment).to.have.property('articleSlug').eql(res.body.data[0].Comment.articleSlug); | ||
expect(res.body.data[0]).to.have.property('User').to.be.an('object'); | ||
expect(res.body.data[0].User).to.have.property('username').eql(res.body.data[0].User.username); | ||
expect(res.body.data[0].User).to.have.property('email').eql(res.body.data[0].User.email); | ||
expect(res.body.data[0]).to.have.property('Reason').to.be.an('object'); | ||
expect(res.body.data[0].Reason).to.have.property('description').eql(res.body.data[0].Reason.description); | ||
|
||
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(); | ||
}); | ||
}); | ||
|
||
it('Admin should be able to get all reported comments based on a particular commentId', (done) => { | ||
const id = 6; | ||
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.data).to.have.property('id').eql(res.body.data.id); | ||
expect(res.body.data).to.have.property('body').eql(res.body.data.body); | ||
expect(res.body.data).to.have.property('userId').eql(res.body.data.userId); | ||
expect(res.body.data).to.have.property('articleSlug').eql(res.body.data.articleSlug); | ||
expect(res.body.data).to.have.property('Reasons').to.be.an('array'); | ||
expect(res.body.data.Reasons[0]).to.have.property('id').eql(res.body.data.Reasons[0].id); | ||
expect(res.body.data.Reasons[0]).to.have.property('description').eql(res.body.data.Reasons[0].description); | ||
expect(res.body.data.Reasons[0]).to.have.property('ReportComments').to.be.an('object'); | ||
expect(res.body.data.Reasons[0].ReportComments).to.have.property('userId').eql(res.body.data.Reasons[0].ReportComments.userId); | ||
done(); | ||
}); | ||
}); | ||
}); |