Skip to content

Commit

Permalink
Merge bdde6e6 into e219388
Browse files Browse the repository at this point in the history
  • Loading branch information
minega25 committed Nov 10, 2019
2 parents e219388 + bdde6e6 commit 8f55c4b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 39 deletions.
84 changes: 79 additions & 5 deletions src/controllers/report.comtroller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import 'dotenv/config';
import _ from 'lodash';
import moment from 'moment';
import Helper from '../helpers/helper';
import models from '../models';
import Userservice from '../services/user.service';
import reportService from '../services/report.service';
import articleService from '../services/article.service';
import RateService from '../services/rate.service';
import likeService from '../services/likes.service';
import Util from '../helpers/util';

const util = new Util();
Expand Down Expand Up @@ -30,12 +37,75 @@ class Report {
if (req.offset >= counter) {
req.offset = 0;
}
const { offset, limit } = req;
const reports = await reportService.getAllReport(offset, limit);
const reports = await reportService.getAllReport();
const reportedArticlesObj = [];
await Promise.all(reports.map(async (report) => {
const article = await articleService.getOneArticle(report.articleSlug);
article.reportReason = report.reason;
reportedArticlesObj.push(article);
}));
const allReportedArticles = _.map(
reportedArticlesObj,
_.partialRight(_.pick, [
'authorId',
'slug',
'reportReason',
'title',
'id',
'description',
'body',
'taglist',
'favorited',
'favoritedcount',
'flagged',
'images',
'views',
'createdAt'
])
);
let myreportedArticles = allReportedArticles;
if (req.auth.role !== 'admin') {
myreportedArticles = allReportedArticles
.filter(report => report.authorId === req.auth.id);
}
await Promise.all(
myreportedArticles.map(async (article) => {
try {
const userDetails = await Userservice.getOneUser(article.authorId);
const {
username, firstname, lastname, image
} = userDetails;
const user = {
username,
firstname,
lastname,
image
};
const rating = await RateService.getArticleRatingStatistic(
article.slug
);
let claps = await likeService.getAllAClaps(article.slug);
claps = Object.values(claps)[0];
const readTime = Helper.calculateReadTime(article.body);
const timeAgo = moment(article.createdAt).fromNow();
article.readtime = readTime;
article.username = username;
article.userImage = image;
article.timeCreated = timeAgo;
article.claps = claps;
article.rating = rating.dataValues.rating;
article.author = user;
return true;
} catch (error) {
throw error;
}
})
);
const response = {
count: counter,
reports,
count: myreportedArticles.length,
myreportedArticles,
};

util.setSuccess(200, 'Reports retrieved successfully', response);
return util.send(res);
}
Expand All @@ -60,10 +130,14 @@ class Report {
req.offset = 0;
}
const { offset, limit } = req;
const reportedArticles = [];
const reports = await reportService.getMyReport(offset, limit, req.auth.id,);
await Promise.all(reports.map(async (report) => {
reportedArticles.push(await articleService.getOneArticle(report.articleSlug));
}));
const response = {
count: counter,
yourReport: reports,
yourReport: reportedArticles,
};
util.setSuccess(200, 'Your reports retrieved successfully', response);
return util.send(res);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/reports/reports.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const router = express.Router();
router.post('/:Article', [auth, confirmEmailAuth, reportValidator, reportMiddleware], reportController.reportArticle);
router.delete('/:reportId', [auth, confirmEmailAuth], reportController.deleteReport);
router.get('/', [auth, confirmEmailAuth, checkQuery], reportController.getMyReport);
router.get('/all', [auth, confirmEmailAuth, admin, checkQuery], reportController.getAllReport);
router.get('/all', [auth, confirmEmailAuth, checkQuery], reportController.getAllReport);
router.get('/:Article', [auth, confirmEmailAuth, admin, checkQuery], reportController.getReportsForArticle);


Expand Down
6 changes: 2 additions & 4 deletions src/services/report.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ class ReportService {
* @returns {object} data
* @memberof articleService
*/
static async getAllReport(offset, limit) {
static async getAllReport() {
try {
return await db.findAll({
attributes: {
exclude: ['updatedAt']
},
order: [
['createdAt', 'DESC']
],
offset,
limit,
]
});
} catch (error) {
throw error;
Expand Down
29 changes: 0 additions & 29 deletions test/reporting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,6 @@ describe('/Report an article', () => {
done();
});
});

it('admin should see if there is no report made yet', (done) => {
chai
.request(server)
.get('/api/v1/reports/all')
.set('Authorization', admintoken)
.end((error, res) => {
expect(res).to.be.an('object');
expect(res.status).to.equal(404);
expect(res.body).to.have.keys('message', 'status');
expect(res.body.message).to.deep.equal('No reports found');
done();
});
});
it('should report an article ', (done) => {
chai
.request(server)
Expand Down Expand Up @@ -221,21 +207,6 @@ describe('/Report an article', () => {
done();
});
});
it('admin should see all reports ', (done) => {
chai
.request(server)
.get('/api/v1/reports/all/?page=2&&limit=5')
.set('Authorization', admintoken)
.end((error, res) => {
expect(res).to.be.an('object');
expect(res.status).to.equal(200);
expect(res.body).to.have.keys('message', 'status', 'data');
expect(res.body.message).to.deep.equal('Reports retrieved successfully');
expect(res.body.data).to.have.keys('count', 'reports');
expect(res.body.data.count).to.deep.equal(3);
done();
});
});
it('admin should see the report for a pecific Article ', (done) => {
chai
.request(server)
Expand Down

0 comments on commit 8f55c4b

Please sign in to comment.