-
Notifications
You must be signed in to change notification settings - Fork 9
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
27 changed files
with
966 additions
and
55 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,119 @@ | ||
import models from '../db/models'; | ||
import helpers from '../helpers'; | ||
|
||
const { errorStat, successStat } = helpers; | ||
/** | ||
* @Module AdminController | ||
* @description Controls all Admin based activity | ||
*/ | ||
class AdminController { | ||
/** | ||
* @static | ||
* @description assigns roles to users | ||
* @param {*} req - Request Object | ||
* @param {*} res - Response Object | ||
* @returns {object} - Containing a message or an error | ||
* @memberof UserController | ||
*/ | ||
static async assignRole(req, res) { | ||
const { newRole: { newRole }, username } = req.body; | ||
const { role } = req.user; | ||
const user = await models.User.findOne({ where: { username } }); | ||
if (!user) return errorStat(res, 404, `No user found with username: ${username}`); | ||
if (role === 'moderator') return errorStat(res, 401, 'You cannot perform this action. Please contact an Admin'); | ||
if (role === 'admin' && (newRole === 'god' || newRole === 'admin')) return errorStat(res, 401, 'You cannot perform this action. Please contact a god'); | ||
await models.User.update({ | ||
role: newRole | ||
}, { | ||
where: { | ||
username | ||
} | ||
}); | ||
const updatedUser = await models.User.findOne({ where: { username } }); | ||
return successStat(res, 200, 'user', updatedUser); | ||
} | ||
|
||
/** | ||
* @static | ||
* @description sets user active status | ||
* @param {*} req - Request Object | ||
* @param {*} res - Response Object | ||
* @returns {object} - Containing a message or an error | ||
* @memberof UserController | ||
*/ | ||
static async setActiveStatus(req, res) { | ||
const { username } = req.params; | ||
const user = await models.User.findOne({ where: { username } }); | ||
if (!user) return errorStat(res, 404, `No user found with username: ${username}`); | ||
const { isActive } = user; | ||
const updatedUser = await models.User.update({ | ||
isActive: !isActive | ||
}, { | ||
returning: true, | ||
where: { | ||
username | ||
} | ||
}); | ||
return successStat(res, 200, 'user', updatedUser); | ||
} | ||
|
||
/** | ||
* @static | ||
* @description deletes a user from the platform | ||
* @param {*} req - Request Object | ||
* @param {*} res - Response Object | ||
* @returns {object} - Delete user | ||
*/ | ||
static async deleteAUser(req, res) { | ||
const { username } = req.params; | ||
const { role } = req.user; | ||
const userToDelete = await models.User.findOne({ where: { username } }); | ||
if (!userToDelete) return errorStat(res, 404, `No user found with username: ${username}`); | ||
if (role !== 'god') return errorStat(res, 401, 'You cannot perform this action. Please contact a god'); | ||
await models.DeletedUsers.create(userToDelete.dataValues); | ||
await models.User.destroy({ where: { username } }); | ||
return successStat(res, 200, 'message', `user with username ${username} has been deleted from Author's Haven`); | ||
} | ||
|
||
/** | ||
* @static | ||
* @description deletes an article from the platform | ||
* @param {*} req - Request Object | ||
* @param {*} res - Response Object | ||
* @returns {object} - with deleted message or error | ||
*/ | ||
static async deleteAnArticle(req, res) { | ||
const { id } = req.params; | ||
const articleToDelete = await models.Article.findOne({ where: { id } }); | ||
if (!articleToDelete) return errorStat(res, 404, 'Article not found'); | ||
await models.Article.destroy({ | ||
where: { id }, | ||
include: [ | ||
{ | ||
as: 'comment', | ||
model: models.Comment | ||
} | ||
] | ||
}); | ||
return successStat(res, 200, 'message', 'Article deleted successfully'); | ||
} | ||
|
||
/** | ||
* @static | ||
* @description deletes a commentfrom the platform | ||
* @param {*} req - Request Object | ||
* @param {*} res - Response Object | ||
* @returns {object} - with deleted message or error | ||
*/ | ||
static async deleteAComment(req, res) { | ||
const { id } = req.params; | ||
const commentToDelete = await models.Comment.findOne({ where: { id } }); | ||
if (!commentToDelete) return errorStat(res, 404, 'Comment not found'); | ||
await models.Comment.destroy({ | ||
where: { id } | ||
}); | ||
return successStat(res, 200, 'message', 'Comment deleted successfully'); | ||
} | ||
} | ||
|
||
export default AdminController; |
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
18 changes: 18 additions & 0 deletions
18
server/db/migrations/20190806122005-add-role-to-user-column.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 @@ | ||
module.exports = { | ||
up: async (queryInterface, Sequelize) => [ | ||
await queryInterface.addColumn('Users', 'role', { | ||
allowNull: false, | ||
type: Sequelize.STRING, | ||
defaultValue: 'user' | ||
}), | ||
await queryInterface.addColumn('Users', 'isActive', { | ||
allowNull: false, | ||
type: Sequelize.BOOLEAN, | ||
defaultValue: true | ||
}), | ||
], | ||
down: async queryInterface => [ | ||
await queryInterface.removeColumn('Users', 'role'), | ||
await queryInterface.removeColumn('Users', 'isActive') | ||
] | ||
}; |
55 changes: 55 additions & 0 deletions
55
server/db/migrations/20190806183542-create-deleted-users.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,55 @@ | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('DeletedUsers', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
firstname: { | ||
type: Sequelize.STRING | ||
}, | ||
lastname: { | ||
type: Sequelize.STRING | ||
}, | ||
email: { | ||
type: Sequelize.STRING | ||
}, | ||
password: { | ||
type: Sequelize.STRING | ||
}, | ||
bio: { | ||
type: Sequelize.STRING | ||
}, | ||
username: { | ||
type: Sequelize.STRING | ||
}, | ||
image: { | ||
type: Sequelize.STRING | ||
}, | ||
socialId: { | ||
type: Sequelize.STRING | ||
}, | ||
verified: { | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
role: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
isActive: { | ||
allowNull: false, | ||
type: Sequelize.BOOLEAN, | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('DeletedUsers') | ||
}; |
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,20 @@ | ||
|
||
module.exports = (sequelize, DataTypes) => { | ||
const DeletedUsers = sequelize.define('DeletedUsers', { | ||
firstname: DataTypes.STRING, | ||
lastname: DataTypes.STRING, | ||
email: DataTypes.STRING, | ||
password: DataTypes.STRING, | ||
bio: DataTypes.STRING, | ||
username: DataTypes.STRING, | ||
image: DataTypes.STRING, | ||
socialId: DataTypes.STRING, | ||
verified: DataTypes.STRING, | ||
role: DataTypes.STRING, | ||
isActive: DataTypes.BOOLEAN | ||
}, {}); | ||
DeletedUsers.associate = () => { | ||
// associations can be defined here | ||
}; | ||
return DeletedUsers; | ||
}; |
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
Oops, something went wrong.