-
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.
[ft -#16370397] enable the admin to read the reported comments
- Loading branch information
Frank Mutabazi
authored and
Frank Mutabazi
committed
Jul 19, 2019
1 parent
ccfde98
commit 5c1776e
Showing
5 changed files
with
174 additions
and
4 deletions.
There are no files selected for viewing
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,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(); | ||
}); | ||
}); | ||
}); |