Skip to content

Commit

Permalink
feat(admin-edit-category): editing categories by admin and super admin
Browse files Browse the repository at this point in the history
- create the endpoint for deleting category by admin and super admin

[Delivers #164693379]
  • Loading branch information
chingsley committed Mar 19, 2019
1 parent 6a5aab3 commit 9cb7b8c
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 262 deletions.
33 changes: 27 additions & 6 deletions controllers/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class CategoryController {
* @param {object} req - HTTP Request
* @param {object} res - HTTP Response
* @memberof CategoryController
* @returns {object} Class instance
* @returns {object} response object
*/
static async createCategory(req, res) {
const {
Expand All @@ -39,10 +39,7 @@ export default class CategoryController {
errors: [error.errors[0].message]
});
}
return res.status(500).json({
success: false,
errors: ['Internal server error']
});
return res.status(500).json({ success: false, error: [error.message] });
}
}

Expand All @@ -52,7 +49,7 @@ export default class CategoryController {
* @param {Object} req - the request object
* @param {Object} res - the response object
* @memberof CategoryController
* @returns {Object} class instance
* @returns {Object} response object
*/
static async updateCategory(req, res) {
const { body: { category } } = req;
Expand All @@ -69,4 +66,28 @@ export default class CategoryController {
return res.status(500).json({ success: false, error: [error.message] });
}
}

/**
* @description - Delete a category
* @static
* @param {object} req - the request object
* @param {object} res - the response object
* @returns {object} - response object
*/
static async deleteCategory(req, res) {
const { params: { id } } = req;
try {
await Category.destroy({
where: {
id
}
});
return res.status(200).json({
success: true,
message: 'Category deleted.',
});
} catch (err) {
return res.status(500).json({ success: false, errors: [err.message] });
}
}
}
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import dotenv from 'dotenv';
import swaggerUI from 'swagger-ui-express';
import routes from './routes/index';
import doc from './doc.json';
import CreateSuperAdmin from './seeders/createSuperAdmin';

const { registerSuperAdmin } = CreateSuperAdmin;

// read .env config
dotenv.config();
Expand Down Expand Up @@ -70,6 +73,8 @@ app.use((err, res) => {
});
});

registerSuperAdmin();

// finally, let's start our server...
app.listen(process.env.PORT, () => {
// eslint-disable-next-line no-console
Expand Down
File renamed without changes.
47 changes: 0 additions & 47 deletions migrations/20190318190114-seed_super_admin.js

This file was deleted.

13 changes: 11 additions & 2 deletions routes/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import addImages from '../middleware/addImage';
import generateSlug from '../middleware/generateSlug';
import ArticleController from '../controllers/articles';
import CategoryController from '../controllers/category';
import verifyCategory from '../middleware/verifyCategory';
import verifyCategoryId from '../middleware/verifyCategoryId';
import {
validateSignup,
validateLogin,
Expand Down Expand Up @@ -122,10 +122,19 @@ apiRoutes.patch(
Auth.authorizeAdmin,
validateCategory,
returnValidationErrors,
verifyCategory,
verifyCategoryId,
CategoryController.updateCategory
);

apiRoutes.delete(
'/category/:id',
Auth.verifyUser,
isUserVerified,
Auth.authorizeAdmin,
verifyCategoryId,
CategoryController.deleteCategory
);

apiRoutes.post(
'/requestpasswordreset',
validateEmail,
Expand Down
43 changes: 43 additions & 0 deletions seeders/createSuperAdmin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import dotenv from 'dotenv';
import models from '../models';

dotenv.config();
const { User } = models;

const {
SUPER_ADMIN_EMAIL,
SUPER_ADMIN_PASSWORD,
SUPER_ADMIN_NAME,
SUPER_ADMIN_USERNAME,
SUPER_ADMIN_ROLE
} = process.env;

/**
* @description This is a class for registering a super user by default
* @class CreateSuperAdmin
*/
export default class CreateSuperAdmin {
/**
* @description This is a method that registers the super admin
* @static
* @returns {object} - object representing response image
* @memberof CreateSuperAdmin
*/
static async registerSuperAdmin() {
try {
await User.findOrCreate({
where: { $or: [{ username: SUPER_ADMIN_EMAIL }, { email: SUPER_ADMIN_USERNAME }] },
defaults: {
email: SUPER_ADMIN_EMAIL,
password: SUPER_ADMIN_PASSWORD,
name: SUPER_ADMIN_NAME,
username: SUPER_ADMIN_USERNAME,
role: SUPER_ADMIN_ROLE,
verified: true
}
});
} catch (error) {
throw new Error('Could not register super admin.');
}
}
}
40 changes: 17 additions & 23 deletions test/article.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,19 @@ describe('ARTICLES', () => {
});

describe('Create an article by an authenticated and verified user', () => {
before(() => { updateVerifiedStatus(user2.email); });
before(async () => { await updateVerifiedStatus(user2.email); });

it('Should create a new article.', (done) => {
chai
it('Should create a new article.', async () => {
const res = await chai
.request(app)
.post('/api/v1/articles')
.set('authorization', token)
.send(article1)
.end((err, res) => {
const { status, body: { message, success, article: { slug } } } = res;
articleSlug = slug;
expect(status).to.be.equal(201);
expect(success).to.be.equal(true);
expect(message).to.be.equal('New article created successfully');
done(err);
});
.send(article1);
const { status, body: { message, success, article: { slug } } } = res;
articleSlug = slug;
expect(status).to.be.equal(201);
expect(success).to.be.equal(true);
expect(message).to.be.equal('New article created successfully');
});

it('Should not create if title is not set.', (done) => {
Expand Down Expand Up @@ -217,20 +214,17 @@ describe('/PUT articles slug', () => {
});
});

before(() => { updateVerifiedStatus(myUser.email); });
it('should return an error if the user is not the owner of the article', (done) => {
chai
before(async () => { await updateVerifiedStatus(myUser.email); });
it('should return an error if the user is not the owner of the article', async () => {
const res = await chai
.request(app)
.put(`/api/v1/articles/${articleSlug}`)
.set('authorization', userToken2)
.send(article2)
.end((err, res) => {
expect(res).to.have.status(403);
expect(res.body).to.have.property('success').equal(false);
expect(res.body.errors).to.be.an('Array');
expect(res.body.errors[0]).to.be.equal('You are unauthorized to perform this action');
done(err);
});
.send(article2);
expect(res).to.have.status(403);
expect(res.body).to.have.property('success').equal(false);
expect(res.body.errors).to.be.an('Array');
expect(res.body.errors[0]).to.be.equal('You are unauthorized to perform this action');
});
});

Expand Down
Loading

0 comments on commit 9cb7b8c

Please sign in to comment.