Skip to content

Commit

Permalink
Merge 9d9d413 into 426640b
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramadhan0 committed Jul 15, 2019
2 parents 426640b + 9d9d413 commit e54d477
Show file tree
Hide file tree
Showing 15 changed files with 587 additions and 26 deletions.
159 changes: 157 additions & 2 deletions src/api/controllers/adminPermissionsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import models from '../models';
import status from '../../helpers/constants/status.codes';
import paginateUser from '../../helpers/generate.pagination.details';

const { User } = models;
const generatePagination = paginateUser;

const {
Article, Report, User, Reason
} = models;
/**
* containing all user's model controllers (signup, login)
*
Expand Down Expand Up @@ -62,7 +66,7 @@ export default class adminPermissions {
const { email } = req.params;
const { firstName, lastName } = req.body;

const result = await User.update({
await User.update({
email,
firstName,
lastName
Expand Down Expand Up @@ -207,4 +211,155 @@ export default class adminPermissions {
}
});
}

/**
* fetch all reported Articles
*
* @static
* @param {array} req - request array
* @param {array} res - respond array
* @returns {array} response body
* @memberof getArticles
*/
static async getReportedArticles(req, res) {
const defaultOffset = 0;
const defaultLimit = 10;
const offset = req.query.offset || defaultOffset;
const limit = req.query.limit || defaultLimit;
const paginatedArticle = await Report.findAll({
attributes: ['createdAt'],
include: [
{
model: Article,
required: true,
attributes: ['title', 'slug', 'description', 'body', 'coverImage']
},
{
model: User,
required: true,
attributes: ['username', 'email']
},
{
model: Reason,
required: true,
attributes: ['description']
}
],
offset,
limit
});
res.status(status.OK).send({
status: status.OK,
paginationDetails: generatePagination(paginatedArticle.length,
paginatedArticle,
offset,
limit),
data: paginatedArticle
});
}

/**
* fetch single reported Article
*
* @static
* @param {array} req - request array
* @param {array} res - respond array
* @returns {array} response body
* @memberof getArticles
*/
static async getReportedArticle(req, res) {
const { slug } = req.params;
const reportedArticle = await Report.findOne({
attributes: [['createdAt', 'ReportedOn']],
where: {
reportedArticleSlug: slug
},
include: [
{
model: Article,
required: true,
attributes: ['title', 'slug', 'description', 'body', 'coverImage']
},
{
model: User,
required: true,
attributes: ['username', 'email']
},
{
model: Reason,
required: true,
attributes: ['description']
}
]
});
res.status(status.OK).send({
status: status.OK,
data: reportedArticle
});
}

/**
* block Article
*
* @static
* @param {object} req - request body
* @param {object} res - response body
* @returns { object } - response
* @memberof blockArticle
*/
static async blockArticle(req, res) {
const { slug } = req.params;
const { title, body } = req;
const isBlocked = true;

await Article.update({
isBlocked
},
{
where: {
slug
}
});
return res.status(status.OK).json({
status: status.OK,
message: `${slug} blocked successfully`,
data: {
title,
slug,
body
}
});
}

/**
* unblock Article
*
* @static
* @param {object} req - request body
* @param {object} res - response body
* @returns { object } - response
* @memberof blockArticle
*/
static async unBlockArticle(req, res) {
const { slug } = req.params;
const isBlocked = 'false';
const { title, body } = req;
await Article.update({
isBlocked
},
{
where: {
slug
}
});
return res.status(status.OK).json({
status: status.OK,
message: `${slug} unblocked successfully`,
data: {
title,
slug,
body
}
});
}
}
57 changes: 41 additions & 16 deletions src/api/controllers/articleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,28 @@ export default class ArticleController {
const articles = await Article.findAndCountAll({
offset,
limit,
attributes: ['id', 'title', 'description', 'body', 'slug', 'coverImage', 'tagList', 'createdAt', 'updatedAt'],
attributes: [
'id',
'title',
'description',
'body',
'slug',
'coverImage',
'tagList',
'createdAt',
'updatedAt'
],
include: [
{
model: Category,
as: 'Category',
attributes: ['name']
}, {
},
{
model: User,
attributes: ['firstName', 'lastName', 'profileImage'],
}],

attributes: ['firstName', 'lastName', 'profileImage']
}
]
});

result.status = 200;
Expand All @@ -129,17 +140,29 @@ export default class ArticleController {
static async getArticle(req, res) {
const { slug } = req.params;
const article = await Article.findOne({
attributes: ['id', 'title', 'description', 'body', 'slug', 'coverImage', 'tagList', 'createdAt', 'updatedAt'],
attributes: [
'id',
'title',
'description',
'body',
'slug',
'coverImage',
'tagList',
'createdAt',
'updatedAt'
],
where: { slug },
include: [
{
model: Category,
as: 'Category',
attributes: ['name']
}, {
},
{
model: User,
attributes: ['firstName', 'lastName', 'profileImage'],
}]
attributes: ['firstName', 'lastName', 'profileImage']
}
]
});

if (article) {
Expand Down Expand Up @@ -225,13 +248,15 @@ export default class ArticleController {
}
});

return response[0]._options.isNewRecord === false ? errorSender(statusCodes.BAD_REQUEST,
res,
'Message',
`Sorry, You can not report this ${slug} with the same reason twice, Thanks `) : res.status(statusCodes.CREATED).json({
status: statusCodes.CREATED,
message: `Report for ${slug} is successfully submitted, Thanks`
});
return response[0]._options.isNewRecord === false
? errorSender(statusCodes.BAD_REQUEST,
res,
'Message',
`Sorry, You can not report this ${slug} with the same reason twice, Thanks `)
: res.status(statusCodes.CREATED).json({
status: statusCodes.CREATED,
message: `Report for ${slug} is successfully submitted, Thanks`
});
} catch (SequelizeForeignKeyConstraintError) {
errorSender(statusCodes.NOT_FOUND,
res,
Expand Down
8 changes: 8 additions & 0 deletions src/api/migrations/20190707124518-add-isBlocked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
up: (queryInterface, Sequelize) => queryInterface.addColumn('Articles', 'isBlocked', {
type: Sequelize.BOOLEAN,
defaultValue: false
}),

down: queryInterface => queryInterface.removeColumn('Articles', 'isBlocked')
};
4 changes: 4 additions & 0 deletions src/api/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default (sequelize, DataTypes) => {
allowNull: false,
type: DataTypes.INTEGER,
references: { model: 'Categories', key: 'id' }
},
isBlocked: {
type: DataTypes.BOOLEAN,
defaultValue: false
}
},
{
Expand Down
1 change: 1 addition & 0 deletions src/api/models/reasons.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default (sequelize, DataTypes) => {
Reasons.associate = ({ Report }) => {
Reasons.hasMany(Report, {
foreignKey: 'reasonId',
sourceKey: 'id',
onDelete: 'cascade'
});
};
Expand Down
6 changes: 3 additions & 3 deletions src/api/models/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export default (sequelize, DataTypes) => {
});
Report.removeAttribute('id');
Report.associate = ({ User, Article, Reason }) => {
Report.belongsTo(User, { foreignKey: 'userId' });
Report.belongsTo(Article, { foreignKey: 'reportedArticleSlug' });
Report.belongsTo(Reason, { foreignKey: 'reasonId' });
Report.belongsTo(User, { foreignKey: 'userId', targetKey: 'id' });
Report.belongsTo(Article, { foreignKey: 'reportedArticleSlug', targetKey: 'slug' });
Report.belongsTo(Reason, { foreignKey: 'reasonId', targetKey: 'id' });
};
return Report;
};
12 changes: 10 additions & 2 deletions src/api/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,32 @@ export default (sequelize, DataTypes) => {
onDelete: 'CASCADE',
hooks: true
});

User.hasMany(models.Rating, {
foreignKey: 'userId'
});
User.hasMany(models.Highlight, {
foreignKey: 'userId',
onDelete: 'CASCADE',
onDelete: 'CASCADE'
});
User.hasMany(models.Following, {
foreignKey: 'follower'
});
User.hasMany(models.Following, {
foreignKey: 'following'
});

User.hasMany(models.Report, {
foreignKey: 'userId',
sourceId: 'id',
onDelete: 'CASCADE',
hooks: true
});
};

User.findByEmail = (email) => {
const queryResult = User.findOne({ where: { email } });

return queryResult;
};
return User;
Expand Down
38 changes: 38 additions & 0 deletions src/api/routes/adminRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import validateToken from '../../middlewares/checkValidToken';
import checkUser from '../../middlewares/checkUser';
import checkIfAdmin from '../../middlewares/IsAdmin';
import checkIfBlocked from '../../middlewares/isBlocked';
import articleController from '../controllers/articleController';
import checkIfArticleExist from '../../middlewares/getOneArticle';
import CheckIfModerator from '../../middlewares/CheckIfModerator';
import CheckIfBlocked from '../../middlewares/isArticleBlocked';

const adminRouter = new Router();

Expand Down Expand Up @@ -54,4 +58,38 @@ adminRouter.patch('/users/:email/unblock',
checkIfBlocked.checkUnBlocked,
adminPermissionsController.unBlockUser);

adminRouter.get('/articles',
validateToken,
CheckIfModerator.CheckAdmins,
articleController.getArticles);

adminRouter.get('/article/:slug',
validateToken,
CheckIfModerator.CheckAdmins,
articleController.getArticle);

adminRouter.get('/articles/reported',
validateToken,
CheckIfModerator.CheckAdmins,
adminPermissionsController.getReportedArticles);

adminRouter.get('/article/:slug/reported',
validateToken,
CheckIfModerator.CheckAdmins,
adminPermissionsController.getReportedArticle);

adminRouter.patch('/article/:slug/block',
validateToken,
CheckIfModerator.CheckAdmins,
checkIfArticleExist.getArticle,
CheckIfBlocked.checkBlocked,
adminPermissionsController.blockArticle);

adminRouter.patch('/article/:slug/unblock',
validateToken,
CheckIfModerator.CheckAdmins,
checkIfArticleExist.getArticle,
CheckIfBlocked.checkUnBlocked,
adminPermissionsController.unBlockArticle);

export default adminRouter;
Loading

0 comments on commit e54d477

Please sign in to comment.