Skip to content

Commit

Permalink
#ft-162414269- Users can report articles (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebzeal authored and seunkoko committed Jan 30, 2019
1 parent 92d9d35 commit 7fd689f
Show file tree
Hide file tree
Showing 18 changed files with 936 additions and 183 deletions.
23 changes: 13 additions & 10 deletions server/controllers/LikesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import db from '../models';
import response from '../helpers/response';

const {
Article, ArticleLikesDislike, Comment, CommentLike
Article, ArticleLikesDislike, User, Comment, CommentLike
} = db;

/**
Expand Down Expand Up @@ -85,26 +85,29 @@ class LikesController {
where: {
articleId: theArticle.dataValues.id,
reaction: 'like'
}
},
include: [
{
model: User,
as: 'Liked By',
attributes: ['id', 'userName', 'img'],
}
],
attributes: [
]
});
if (!allLikes) {
return response(res, 404, 'failure', 'No likes for this Article', null, null);
}
return response(
res,
200,
'success',
`There are ${allLikes.count} Likes for this article`,
null,
null
);
return response(res, 200, 'success', `There are ${allLikes.count} Likes for this article`, null, allLikes.rows);
} catch (error) {
return res.status(401).json({
data: { status: 'failure', message: error }
});
}
}


/**
*
* @description Method to likes and unlike a comment
Expand Down
254 changes: 186 additions & 68 deletions server/controllers/ReportArticleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ const { Article, Report, User } = db;
*/
class ReportArticleController {
/**
* @descripion Allow users to report articles that violate terms and agreement
* @static
* @param {*}req Express Request Object
* @param {*}res Express Response object
* @returns {object} Json response
* @memberof ReportArticleController
*/
* @descripion Allow users to report articles that violate terms and agreement
* @static
* @param {*}req Express Request Object
* @param {*}res Express Response object
* @returns {object} Json response
* @memberof ReportArticleController
*/
static async reportArticle(req, res) {
try {
const { userId } = req.user;
Expand All @@ -32,14 +32,7 @@ class ReportArticleController {
return response(res, 404, 'failure', 'Article not found', null, null);
}
if (userId === getArticle.dataValues.userId) {
return response(
res,
403,
'failure',
'You cannot report your own article. Do you want to Delete the article?',
null,
null
);
return response(res, 403, 'failure', 'You cannot report your own article. Do you want to Delete the article?', null, null);
}
const reported = await Report.findOne({
where: {
Expand All @@ -49,82 +42,123 @@ class ReportArticleController {
});

if (reported) {
return response(
res,
403,
'failed',
'You just reported this article! Do you wish to cancel the report or modify your complaint?',
null,
null
);
return response(res, 403, 'failure', 'You just reported this article! Do you wish to cancel the report or modify your complaint?', null, null);
}
const submitReport = await Report.create({
userId,
articleId: getArticle.dataValues.id,
complaint
});
if (submitReport) {
return response(
res,
201,
'success',
'Your report has been logged and will be acted on',
null,
null
);
return response(res, 201, 'success', 'Your complaint has been lodged successfully', null, null);
}
} catch (error) {
return response(res, 500, 'failure', 'server error', { message: error }, null);
return response(
res, 500, 'failure', 'server error',
{ message: 'Something went wrong on the server' }, null
);
}
}

/**
*
* @description Gets all reported articles
* @static
* @param {*} req
* @param {*} res
* @returns {object} Json response
* @memberof ReportArticleController
*/
* @descripion Allow users to edit complaints on report articles that violate terms and agreement
* @static
* @param {*}req Express Request Object
* @param {*}res Express Response object
* @returns {object} Json response
* @memberof ReportArticleController
*/
static async reportArticleUpdate(req, res) {
try {
const { userId } = req.user;
const { complaint } = req.body;
const { slug } = req.params;

const getArticle = await Article.findOne({
where: {
slug
}
});
if (!getArticle) {
return response(res, 404, 'failure', 'Article not found', null, null);
}
if (userId === getArticle.dataValues.userId) {
return response(res, 403, 'failure', 'You cannot report your own article. Do you want to Delete the article?', null, null);
}
const reported = await Report.findOne({
where: {
articleId: getArticle.dataValues.id,
userId
}
});


if (reported) {
const submitReport = await reported.update({
complaint
});
if (submitReport) {
return response(res, 201, 'success', 'Your complaint has been modified and will be acted on', null, null);
}
} else {
return response(res, 403, 'failure', 'You cannot modify this report', null, null);
}
} catch (error) {
return response(
res, 500, 'failure', 'server error',
{ message: 'Something went wrong on the server' }, null
);
}
}


/**
*
* @description Gets all reported articles
* @static
* @param {*} req
* @param {*} res
* @returns {object} Json response
* @memberof ReportArticleController
*/
static async getReportedArticles(req, res) {
try {
const allReportedArticle = await Report.findAndCountAll({
include: [
{
model: Article,
attributes: ['title', 'userId']
attributes: ['id', 'title']
},
{
model: User,
attributes: ['userName', 'img']
as: 'Complainant',
attributes: ['id', 'userName', 'img'],
}
],
attributes: ['complaint']
attributes: [
'complaint'
]
});

return response(res, 200, 'success', `There are ${allReportedArticle.count} reported articles`, null, allReportedArticle.rows);
} catch (error) {
return response(
res,
200,
'success',
`There are ${allReportedArticle.count} reported articles`,
null,
allReportedArticle.rows
res, 500, 'failure', 'server error',
{ message: 'Something went wrong on the server' }, null
);
} catch (error) {
return response(res, 500, 'failure', 'server error', { message: error }, null);
}
}


/**
*
* @description Gets all reports for articles
* @static
* @param {*} req
* @param {*} res
* @returns {object} Json response
* @memberof ReportArticleController
*/
*
* @description Gets all reports for articles
* @static
* @param {*} req
* @param {*} res
* @returns {object} Json response
* @memberof ReportArticleController
*/
static async getReportsOnArticle(req, res) {
try {
const { slug } = req.params;
Expand All @@ -142,25 +176,109 @@ class ReportArticleController {
include: [
{
model: User,
attributes: ['userName', 'img']
as: 'Complainant',
attributes: ['id', 'userName', 'img'],
}
],
attributes: ['complaint'],
attributes: [
'complaint'
],
where: {
articleId: getArticle.dataValues.id
}
});

return response(res, 200, 'success', `There are ${allReportedArticle.count} reports against this articles`, null, allReportedArticle.rows);
} catch (error) {
return response(
res,
200,
'success',
`There are ${allReportedArticle.count} reports against this articles`,
null,
allReportedArticle.rows
res, 500, 'failure', 'server error',
{ message: 'Something went wrong on the server' }, null
);
}
}


/**
*
* @description Gets all reports by user
* @static
* @param {*} req
* @param {*} res
* @returns {object} Json response
* @memberof ReportArticleController
*/
static async getAllReportsByUser(req, res) {
try {
const { userId } = req.user;

const allReportedArticle = await Report.findAndCountAll({
include: [
{
model: Article,
attributes: ['id', 'title']
}
],
attributes: [
'complaint'
],
where: {
userId
}
});

return response(res, 200, 'success', `You have made ${allReportedArticle.count} reports`, null, allReportedArticle.rows);
} catch (error) {
return response(
res, 500, 'failure', 'server error',
{ message: 'Something went wrong on the server' }, null
);
}
}

/**
* @description For user to delete logged reports
* @static
* @param {*} req Express Object
* @param {*} res Express Object
* @returns {Object} Json Response
* @memberof ReportArticleController
*/
static async deleteReportOnArticle(req, res) {
try {
const { userId } = req.user;
const { slug } = req.params;

const getArticle = await Article.findOne({
where: {
slug
}
});
if (!getArticle) {
return response(res, 404, 'failure', 'Article not found', null, null);
}
const reported = await Report.findOne({
where: {
articleId: getArticle.dataValues.id
}
});
if (!reported) {
return response(res, 404, 'failure', 'Report not found', null, null);
}

if (userId !== reported.dataValues.userId) {
return response(res, 403, 'failure', 'You cannot delete someone else\'s report?', null, null);
}
if (reported) {
const submitReport = await reported.destroy();
if (submitReport) {
return response(res, 201, 'success', 'Your report has been cancelled', null, null);
}
}
} catch (error) {
return response(res, 500, 'failure', 'server error', { message: error }, null);
return response(
res, 500, 'failure', 'server error',
{ message: error }, null
);
}
}
}
Expand Down
Loading

0 comments on commit 7fd689f

Please sign in to comment.