Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

166816223 user can report article #38

Merged
merged 1 commit into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions controllers/articles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,5 +424,86 @@ export default {
error: 'Something went wrong',
});
}
},

flagArticle: async (req, res) => {
const { params: { slug }, body: { flag } } = req;

const article = await db.Article.findOne({
where: { slug }
});
if (!article) {
return res.status(404).send({
message: 'Article does not exist'
});
}
const result = await article.update({
flagged: JSON.parse(flag)
});

return res.status(200).send({
article: result
});
},

showArticleReports: async (req, res) => {
const { params: { slug } } = req;

const article = await db.Article.findOne({
where: { slug },
include: [{
model: db.Report,
as: 'reports',
include: [{
model: db.User,
as: 'reporter',
attributes: ['id', 'firstName', 'lastName', 'email', 'image', 'username']
}]
}]
});

if (!article) {
return res.status(404).send({
message: 'Article does not exist'
});
}
return res.status(200).send({
article,
});
},

deleteReportedArticle: async (req, res) => {
const { params: { slug } } = req;

const article = await db.Article.findOne({
where: {
slug
}
});
if (!article) {
return res.status(404).send({
message: 'Article does not exist'
});
}
const reports = await db.Report.findAll({
where: {
articleId: article.id
}
});
if (!reports.length) {
return res.status(404).send({
message: 'sorry you can\'t delete an article that was not reported'
});
}

await deleteImage(article.slug);
await db.Article.destroy({
where: {
id: article.id,
}
});
return res.status(200).send({
message: 'deleted successfully',
});
}
};
54 changes: 54 additions & 0 deletions controllers/reports/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import db from '../../db/models';

export default {
reportArticle: async (req, res) => {
const {
user, body: {
articleId, message
}
} = req;

FrankChinedu marked this conversation as resolved.
Show resolved Hide resolved
const articleExist = await db.Article.findOne({
where: {
id: articleId
}
});

if (!articleExist) {
return res.status(404).send({
message: 'Article does not exist'
});
}

await db.Report.create({
articleId,
userId: user.id,
message,
});

return res.status(200).send({
message: 'thank you for the report'
});
},

getReportedArticles: async (req, res) => {
const articles = await db.Report.findAll({
include: [
{
model: db.Article,
as: 'articles'
},
{
model: db.User,
as: 'reporter',
attributes: ['firstName', 'lastName', 'email', 'image', 'id', 'username']
},
],

});

return res.status(200).send({
articles
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface
.addColumn('Articles', 'flagged', {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false
}),
down: queryInterface => queryInterface.removeColumn('Articles', 'flagged'),
};
43 changes: 43 additions & 0 deletions db/migrations/20190716140622-create-reports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Reports', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
message: {
type: Sequelize.TEXT
},
userId: {
allowNull: false,
type: Sequelize.INTEGER,
required: true,
onDelete: 'CASCADE',
references: {
model: 'Users',
key: 'id',
as: 'user',
}
},
articleId: {
allowNull: false,
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
references: {
model: 'Articles',
key: 'id',
as: 'article',
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('Reports'),
};
6 changes: 6 additions & 0 deletions db/models/Article.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = (sequelize, DataTypes) => {
image: DataTypes.STRING,
rating: DataTypes.FLOAT,
readTime: DataTypes.STRING,
flagged: DataTypes.BOOLEAN,
},
{}
);
Expand Down Expand Up @@ -56,6 +57,11 @@ module.exports = (sequelize, DataTypes) => {
as: 'comment',
cascade: true
});
Article.hasMany(models.Report, {
foreignKey: 'articleId',
as: 'reports',
cascade: true
});
};
return Article;
};
19 changes: 19 additions & 0 deletions db/models/Reports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = (sequelize, DataTypes) => {
const Report = sequelize.define('Report', {
message: DataTypes.TEXT,
userId: DataTypes.INTEGER,
articleId: DataTypes.INTEGER
}, {});
Report.associate = (models) => {
Report.belongsTo(models.Article, {
foreignKey: 'articleId',
as: 'articles'
});

Report.belongsTo(models.User, {
foreignKey: 'userId',
as: 'reporter'
});
};
return Report;
};
5 changes: 5 additions & 0 deletions db/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ module.exports = (sequelize, DataTypes) => {
as: 'bookmarks',
cascade: true,
});
User.hasMany(models.Report, {
foreignKey: 'userId',
as: 'reports',
cascade: true,
});
};

User.prototype.passwordsMatch = function match(password) {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
"seed": "sequelize db:seed:all",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"heroku-postbuild": " npm run migrate",
"dev:test": "NODE_ENV=test npm run rollback:migration && npm test"
"dev:test": "NODE_ENV=test npm run rollback:migration && npm test"
},
"nyc": {
"exclude": [
"**/*.spec.js",
"middlewares/passport.js",
"utils/index.js"
"utils/index.js",
"db/faker.js"
]
},
"husky": {
Expand Down Expand Up @@ -86,4 +87,4 @@
"swagger-ui-express": "^4.0.6",
"yamljs": "^0.3.0"
}
}
}
24 changes: 24 additions & 0 deletions routes/v1/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,28 @@ router.get(
Comment.getCommentHistory
);

router.patch(
'/flag/:slug',
Middleware.authenticate,
Middleware.isblackListedToken,
Middleware.isAdmin,
Validation.flagArticle,
Article.flagArticle
);

router.delete(
'/reported/:slug',
Middleware.authenticate,
Middleware.isblackListedToken,
Middleware.isAdmin,
Article.deleteReportedArticle
);

router.get(
'/reported/:slug',
Middleware.authenticate,
Middleware.isblackListedToken,
Middleware.isAdmin,
Article.showArticleReports
);
export default router;
2 changes: 2 additions & 0 deletions routes/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import notification from './notifications';
import members from './members';
import role from './role';
import search from './search';
import report from './reports';

const router = express.Router();

Expand All @@ -19,5 +20,6 @@ router.use('/notifications', notification);
router.use('/members', members);
router.use('/role', role);
router.use('/search', search);
router.use('/report', report);

export default router;
25 changes: 25 additions & 0 deletions routes/v1/reports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import express from 'express';
import Middleware from '../../middlewares';
import Report from '../../controllers/reports';
import Validation from '../../validators/articles';


const router = express.Router();

router.post(
'/',
Middleware.authenticate,
Middleware.isblackListedToken,
Validation.reportArticle,
Report.reportArticle
);

router.get(
'/articles',
Middleware.authenticate,
Middleware.isblackListedToken,
Middleware.isAdmin,
Report.getReportedArticles
);

export default router;
1 change: 1 addition & 0 deletions tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ export const createArticleTag = async tag => db.ArticleTag.create(tag);
export const editComment = async editedComment => db.CommentHistory.create(editedComment);

export const createCommentHistory = async editedComment => db.CommentHistory.create(editedComment);
export const createReport = async report => db.Report.create(report);
Loading