cubash-archive/models/category.js

81 lines
1.7 KiB
JavaScript

let randomColor = require('randomcolor')
module.exports = (sequelize, DataTypes) => {
let Category = sequelize.define('Category', {
name: {
type: DataTypes.STRING(191),
unique: true,
allowNull: false,
validate: {
notEmpty: {
msg: 'The category name can\'t be empty'
},
isString (val) {
if(typeof val !== 'string') {
throw new sequelize.ValidationError('The category name must be a string')
}
}
}
},
value: {
type: DataTypes.STRING(191),
unique: true
},
locked: {
type: DataTypes.BOOLEAN,
default: false
},
BoosterOnly: {
type: DataTypes.BOOLEAN,
default: false,
required: true
},
AdminOnly: {
type: DataTypes.BOOLEAN,
default: false,
required: true
},
color: {
type: DataTypes.STRING,
defaultValue () {
return randomColor({ luminosity: 'bright' })
},
validate: {
isString (val) {
if(typeof val !== 'string') {
throw new sequelize.ValidationError('The color must be a string')
}
}
}
}
}, {
hooks: {
beforeCreate (category) {
if(!category.name) {
throw new sequelize.ValidationError('The category name cant\'t be empty')
} else {
let underscored = category.name.trim().replace(/\s/g, '_').toUpperCase()
category.value = underscored
}
}
},
classMethods: {
// OLD
async AdminOnlyFunc(user) {
if(Category && Category.AdminOnly && !user.admin) {
throw Errors.sequelizeValidation(sequelize.Sequelize, {
error: 'Only admins can access this category'
})
} else {
return false
}
},
}
})
Category.associate = function (models) {
Category.hasMany(models.Thread)
}
return Category
}