-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: merge pull request #1 from AEGEE/add-categories
Add categories
- Loading branch information
Showing
16 changed files
with
708 additions
and
18 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,75 @@ | ||
const { Category } = require('../models'); | ||
const errors = require('./errors'); | ||
const helpers = require('./helpers'); | ||
|
||
exports.createCategory = async (req, res) => { | ||
if (!req.permissions.manage_discounts) { | ||
return errors.makeForbiddenError(res, 'You are not allowed to create integrations.'); | ||
} | ||
|
||
const category = await Category.create(req.body); | ||
|
||
return res.json({ | ||
success: true, | ||
data: category | ||
}); | ||
}; | ||
|
||
exports.listAllCategories = async (req, res) => { | ||
const categories = await Category.findAll({}); | ||
|
||
return res.json({ | ||
success: true, | ||
data: categories | ||
}); | ||
}; | ||
|
||
exports.findCategory = async (req, res, next) => { | ||
const isNumber = helpers.isNumber(req.params.category_id); | ||
|
||
if (!isNumber) { | ||
return errors.makeBadRequestError(res, 'The category ID is not a number.'); | ||
} | ||
|
||
const category = await Category.findOne({ where: { id: Number(req.params.category_id) } }); | ||
|
||
if (!category) { | ||
return errors.makeNotFoundError(res, 'The category is not found.'); | ||
} | ||
|
||
req.category = category; | ||
return next(); | ||
}; | ||
|
||
exports.getCategory = async (req, res) => { | ||
return res.json({ | ||
success: true, | ||
data: req.category | ||
}); | ||
}; | ||
|
||
exports.updateCategory = async (req, res) => { | ||
if (!req.permissions.manage_discounts) { | ||
return errors.makeForbiddenError(res, 'You are not allowed to update category.'); | ||
} | ||
|
||
await req.category.update(req.body); | ||
|
||
return res.json({ | ||
success: true, | ||
data: req.category | ||
}); | ||
}; | ||
|
||
exports.deleteCategory = async (req, res) => { | ||
if (!req.permissions.manage_discounts) { | ||
return errors.makeForbiddenError(res, 'You are not allowed to delete category.'); | ||
} | ||
|
||
await req.category.destroy(); | ||
|
||
return res.json({ | ||
success: true, | ||
data: req.category | ||
}); | ||
}; |
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
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,27 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('categories', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
name: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
discounts: { | ||
allowNull: false, | ||
type: Sequelize.JSONB | ||
}, | ||
created_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updated_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('categories') | ||
}; |
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,38 @@ | ||
const Joi = require('joi'); | ||
const { Sequelize, sequelize } = require('../lib/sequelize'); | ||
|
||
const categoriesSchema = Joi.array().min(1).items(Joi.object().keys({ | ||
icon: Joi.string().trim().required(), | ||
name: Joi.string().trim().required(), | ||
shortDescription: Joi.string().trim().required(), | ||
longDescription: Joi.string().trim().required(), | ||
})); | ||
|
||
const Category = sequelize.define('category', { | ||
name: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
defaultValue: '', | ||
validate: { | ||
notEmpty: { msg: 'Name should be set.' }, | ||
}, | ||
}, | ||
discounts: { | ||
type: Sequelize.JSONB, | ||
allowNull: false, | ||
defaultValue: '', | ||
validate: { | ||
isValid(categoriesValue) { | ||
const { error, value } = Joi.validate(categoriesValue, categoriesSchema); | ||
if (error) { | ||
throw error; | ||
} | ||
|
||
// eslint-disable-next-line no-param-reassign | ||
categoriesValue = value; | ||
} | ||
}, | ||
} | ||
}, { underscored: true, tableName: 'categories' }); | ||
|
||
module.exports = Category; |
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
const Integration = require('./Integration'); | ||
const Code = require('./Code'); | ||
const Category = require('./Category'); | ||
|
||
Integration.hasMany(Code); | ||
Code.belongsTo(Integration); | ||
|
||
module.exports = { | ||
Integration, | ||
Code | ||
Code, | ||
Category | ||
}; |
Oops, something went wrong.