-
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.
- Loading branch information
Showing
13 changed files
with
315 additions
and
95 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
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,147 @@ | ||
import db from '../models'; | ||
import response from '../helpers/response'; | ||
|
||
const { Article, Report, User } = db; | ||
|
||
/** | ||
* | ||
* | ||
* @class ReportArticleController | ||
*/ | ||
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 | ||
*/ | ||
static async reportArticle(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) { | ||
return response(res, 403, 'failed', '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); | ||
} | ||
} catch (error) { | ||
return response( | ||
res, 500, 'failure', 'server error', | ||
{ message: error }, 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'] | ||
}, | ||
{ | ||
model: User, | ||
attributes: ['userName', 'img'], | ||
} | ||
], | ||
attributes: [ | ||
'complaint' | ||
] | ||
}); | ||
|
||
return response(res, 200, 'success', `There are ${allReportedArticle.count} reported articles`, null, allReportedArticle.rows); | ||
} 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 | ||
*/ | ||
static async getReportsOnArticle(req, res) { | ||
try { | ||
const { slug } = req.params; | ||
|
||
const getArticle = await Article.findOne({ | ||
where: { | ||
slug | ||
} | ||
}); | ||
if (!getArticle) { | ||
return response(res, 404, 'failure', 'Article not found', null, null); | ||
} | ||
|
||
const allReportedArticle = await Report.findAndCountAll({ | ||
include: [ | ||
{ | ||
model: User, | ||
attributes: ['userName', 'img'], | ||
} | ||
], | ||
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, 500, 'failure', 'server error', | ||
{ message: error }, null | ||
); | ||
} | ||
} | ||
} | ||
|
||
export default ReportArticleController; |
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
18 changes: 18 additions & 0 deletions
18
server/middlewares/validations/ReportArticleValidations.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { checkSchema } from 'express-validator/check'; | ||
|
||
const reportArticleSchema = checkSchema({ | ||
complaint: { | ||
in: 'body', | ||
customSanitizer: { | ||
options: complaint => complaint.trim() | ||
}, | ||
isLength: { | ||
options: { | ||
min: 2 | ||
}, | ||
errorMessage: 'complaint is too short' | ||
} | ||
} | ||
}); | ||
|
||
export default reportArticleSchema; |
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 |
---|---|---|
@@ -1,17 +1,27 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Report = sequelize.define( | ||
'Report', | ||
{ | ||
articleId: { | ||
type: DataTypes.UUID, | ||
allowNull: false | ||
}, | ||
userId: { | ||
type: DataTypes.UUID, | ||
allowNull: false | ||
} | ||
export default (sequelize, DataTypes) => { | ||
const Report = sequelize.define('Report', { | ||
articleId: { | ||
type: DataTypes.UUID, | ||
allowNull: false | ||
}, | ||
{} | ||
); | ||
userId: { | ||
type: DataTypes.UUID, | ||
allowNull: false | ||
}, | ||
complaint: { | ||
type: DataTypes.TEXT, | ||
allowNull: true | ||
} | ||
}, {}); | ||
Report.associate = (models) => { | ||
Report.belongsTo(models.User, { | ||
foreignKey: 'userId', | ||
onDelete: 'CASCADE' | ||
}); | ||
Report.belongsTo(models.Article, { | ||
foreignKey: 'articleId', | ||
onDelete: 'CASCADE' | ||
}); | ||
}; | ||
return Report; | ||
}; |
Oops, something went wrong.