Skip to content

Commit

Permalink
Merge 3668cf5 into da24335
Browse files Browse the repository at this point in the history
  • Loading branch information
ebzeal authored Jan 27, 2019
2 parents da24335 + 3668cf5 commit aa78f56
Show file tree
Hide file tree
Showing 13 changed files with 315 additions and 95 deletions.
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import express from 'express';
import cors from 'cors';
import env from 'dotenv';
import volleyLogger from 'volleyball';
import expressValidator from 'express-validator';
import swaggerUi from 'swagger-ui-express';
import passport from 'passport';
Expand All @@ -17,7 +16,7 @@ const swaggerDocument = YAML.load('swagger.yaml');
const app = express();

app.use(cors());
// app.use(volleyLogger);

app.use(expressValidator());
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
Expand Down
147 changes: 147 additions & 0 deletions server/controllers/ReportArticleController.js
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;
2 changes: 1 addition & 1 deletion server/middlewares/AuthMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class AuthMiddleware {
res,
401,
'failure',
'Token is invalid, You need to log in again'
'You need to log in again'
);
}

Expand Down
18 changes: 18 additions & 0 deletions server/middlewares/validations/ReportArticleValidations.js
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;
4 changes: 4 additions & 0 deletions server/migrations/20190113050147-create-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export default {
type: Sequelize.UUID,
allowNull: false
},
complaint: {
type: Sequelize.TEXT,
allowNull: true
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
Expand Down
16 changes: 10 additions & 6 deletions server/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,13 @@ export default (sequelize, DataTypes) => {
);
Article.associate = (models) => {
const {
User, Tag, Comment, ArticleLikesDislike, Bookmark, Rating, Share
User, Tag, Comment, ArticleLikesDislike, Bookmark, Rating, Share, Report
} = models;
Article.belongsTo(User, {
foreignKey: 'userId',
onDelete: 'CASCADE',
as: 'author'
});
Article.belongsToMany(User, {
through: 'Report',
as: 'reports',
foreignKey: 'articleId'
});
Article.belongsToMany(User, {
through: 'Rating',
as: 'ratings',
Expand All @@ -65,6 +60,15 @@ export default (sequelize, DataTypes) => {
foreignKey: 'articleId',
as: 'comments'
});
Article.hasMany(Report, {
foreignKey: 'articleId',
as: 'report'
});
Article.belongsToMany(User, {
through: 'Report',
as: 'reports',
foreignKey: 'articleId',
});
Article.hasMany(ArticleLikesDislike, {
foreignKey: 'articleId',
as: 'likes'
Expand Down
38 changes: 24 additions & 14 deletions server/models/report.js
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;
};
Loading

0 comments on commit aa78f56

Please sign in to comment.