Skip to content

Commit

Permalink
Merge 7ec883a into ea90a88
Browse files Browse the repository at this point in the history
  • Loading branch information
encodedBicoding committed Aug 9, 2019
2 parents ea90a88 + 7ec883a commit 46e7a8e
Show file tree
Hide file tree
Showing 27 changed files with 966 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"start": "node build/index.js",
"dev": "export DEBUG=dev && nodemon --exec babel-node server/index.js",
"dev": "export DEBUG=dev && NODE_ENV=production && nodemon --exec babel-node server/index.js",
"debug": "export DEBUG=dev && nodemon --exec babel-node server/index.js --inspect",
"clean": "rm -rf build && mkdir build && npm run copy-docs",
"build": "npm run clean && npm run migrate && babel -d ./build ./server",
Expand Down
119 changes: 119 additions & 0 deletions server/controllers/adminController.js
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;
12 changes: 11 additions & 1 deletion server/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ class UserController {
});
mail.sendMail();
return successStat(res, 201, 'user', {
id: user.id, token, username, firstname, lastname, email,
id: user.id,
token,
username,
firstname,
lastname,
email,
role: user.role,
isActive: user.isActive
});
}

Expand Down Expand Up @@ -84,6 +91,9 @@ class UserController {
email,
bio: user.bio,
image: user.image,
role: user.role,
isActive: user.isActive

});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
up: async (queryInterface, Sequelize) => [
await queryInterface.addColumn('Articles', 'readTime', {
type: Sequelize.INTEGER,
defaultValue: 0,
allowNull: false
}),
],
Expand Down
18 changes: 18 additions & 0 deletions server/db/migrations/20190806122005-add-role-to-user-column.js
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 server/db/migrations/20190806183542-create-deleted-users.js
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')
};
1 change: 0 additions & 1 deletion server/db/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.STRING,
unique: true
},
readTime: DataTypes.INTEGER,
favorited: DataTypes.BOOLEAN,
favoriteCounts: DataTypes.INTEGER,
image: DataTypes.STRING,
Expand Down
20 changes: 20 additions & 0 deletions server/db/models/deletedusers.js
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;
};
5 changes: 5 additions & 0 deletions server/db/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ module.exports = (sequelize, DataTypes) => {
image: DataTypes.STRING,
socialId: DataTypes.STRING,
verified: DataTypes.BOOLEAN,
role: {
type: DataTypes.STRING,
defaultValue: 'user'
},
isActive: DataTypes.BOOLEAN,
newPostEmailSub: DataTypes.BOOLEAN,
}, {});
User.associate = (models) => {
Expand Down
24 changes: 24 additions & 0 deletions server/db/seeders/20190731122700-demo-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module.exports = {
password: '$2a$06$loVt9DxXF97PGJxkjyfJj.PVHNz5FUjNhU4yXIzTK4HQ2EesmuoPi',
image: null,
bio: null,
role: 'user',
isActive: true,
newPostEmailSub: true,
createdAt: new Date(),
updatedAt: new Date(),
Expand All @@ -20,6 +22,8 @@ module.exports = {
password: '$2a$06$loVt9DxXF97PGJxkjyfJj.PVHNz5FUjNhU4yXIzTK4HQ2EesmuoPi',
image: null,
bio: null,
role: 'user',
isActive: false,
newPostEmailSub: false,
createdAt: new Date(),
updatedAt: new Date(),
Expand All @@ -32,6 +36,8 @@ module.exports = {
password: '$2a$06$loVt9DxXF97PGJxkjyfJj.PVHNz5FUjNhU4yXIzTK4HQ2EesmuoPi',
image: null,
bio: null,
role: 'admin',
isActive: true,
newPostEmailSub: true,
createdAt: new Date(),
updatedAt: new Date(),
Expand All @@ -44,6 +50,8 @@ module.exports = {
password: '$2a$06$loVt9DxXF97PGJxkjyfJj.PVHNz5FUjNhU4yXIzTK4HQ2EesmuoPi',
image: null,
bio: null,
role: 'moderator',
isActive: true,
newPostEmailSub: true,
createdAt: new Date(),
updatedAt: new Date(),
Expand All @@ -56,6 +64,8 @@ module.exports = {
password: '$2a$06$loVt9DxXF97PGJxkjyfJj.PVHNz5FUjNhU4yXIzTK4HQ2EesmuoPi',
image: null,
bio: null,
role: 'god',
isActive: true,
newPostEmailSub: true,
createdAt: new Date(),
updatedAt: new Date(),
Expand All @@ -68,6 +78,8 @@ module.exports = {
password: '$2a$06$loVt9DxXF97PGJxkjyfJj.PVHNz5FUjNhU4yXIzTK4HQ2EesmuoPi',
image: null,
bio: null,
role: 'user',
isActive: true,
newPostEmailSub: false,
createdAt: new Date(),
updatedAt: new Date(),
Expand All @@ -80,6 +92,8 @@ module.exports = {
password: '$2a$06$loVt9DxXF97PGJxkjyfJj.PVHNz5FUjNhU4yXIzTK4HQ2EesmuoPi',
image: null,
bio: null,
role: 'user',
isActive: true,
newPostEmailSub: true,
createdAt: new Date(),
updatedAt: new Date(),
Expand All @@ -92,6 +106,8 @@ module.exports = {
password: '$2a$06$loVt9DxXF97PGJxkjyfJj.PVHNz5FUjNhU4yXIzTK4HQ2EesmuoPi',
image: null,
bio: null,
role: 'user',
isActive: true,
newPostEmailSub: false,
createdAt: new Date(),
updatedAt: new Date(),
Expand All @@ -104,6 +120,8 @@ module.exports = {
password: '$2a$06$loVt9DxXF97PGJxkjyfJj.PVHNz5FUjNhU4yXIzTK4HQ2EesmuoPi',
image: null,
bio: null,
role: 'user',
isActive: true,
newPostEmailSub: false,
createdAt: new Date(),
updatedAt: new Date(),
Expand All @@ -116,6 +134,8 @@ module.exports = {
password: '$2a$06$loVt9DxXF97PGJxkjyfJj.PVHNz5FUjNhU4yXIzTK4HQ2EesmuoPi',
image: null,
bio: null,
role: 'user',
isActive: true,
newPostEmailSub: false,
createdAt: new Date(),
updatedAt: new Date(),
Expand All @@ -128,6 +148,8 @@ module.exports = {
password: '$2a$06$loVt9DxXF97PGJxkjyfJj.PVHNz5FUjNhU4yXIzTK4HQ2EesmuoPi',
image: null,
bio: null,
role: 'user',
isActive: true,
newPostEmailSub: false,
createdAt: new Date(),
updatedAt: new Date(),
Expand All @@ -140,6 +162,8 @@ module.exports = {
password: '$2a$06$loVt9DxXF97PGJxkjyfJj.PVHNz5FUjNhU4yXIzTK4HQ2EesmuoPi',
image: null,
bio: null,
role: 'god',
isActive: true,
newPostEmailSub: false,
createdAt: new Date(),
updatedAt: new Date(),
Expand Down
1 change: 0 additions & 1 deletion server/db/seeders/20190801215421-articles-seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ module.exports = {
uuid: faker.random.number({ max: '300' }),
slug: faker.lorem.words(),
image: faker.lorem.sentence(),
readTime: faker.random.number({ min: 1, max: 10 }),
authorId: faker.random.number({
min: 1,
max: 12
Expand Down
Loading

0 comments on commit 46e7a8e

Please sign in to comment.