import sequelize from 'sequelize'; import bcrypt from 'bcrypt'; import { v4 as uuidv4 } from 'uuid'; const { DataTypes, Model } = sequelize; export default class User extends Model { static initialize(sequelize) { User.init( { id: { type: DataTypes.UUID, primaryKey: true }, isAdmin: { type: DataTypes.BOOLEAN, defaultValue: false, allowNull: false }, role: { type: DataTypes.STRING(20), defaultValue: 'expert', allowNull: false, validate: { isIn: [['expert', 'technical']] }, }, login: { type: DataTypes.STRING, allowNull: false, unique: 'login', }, password: { type: DataTypes.STRING, set(val) { this.setDataValue('password', bcrypt.hashSync(val, bcrypt.genSaltSync())); }, }, passwordForAdmin: { type: DataTypes.STRING, }, iat: { type: DataTypes.BIGINT }, sessionId: { type: DataTypes.UUID }, firstName: { type: DataTypes.STRING, allowNull: true, validate: { len: [1, 50] } }, middleName: { type: DataTypes.STRING, allowNull: true, validate: { len: [0, 50] } }, lastName: { type: DataTypes.STRING, allowNull: true, validate: { len: [1, 50] } }, }, { sequelize, modelName: 'User', tableName: 'users', } ); User.beforeCreate(user => { user.id = uuidv4(); }); } fullName() { return this.firstName + ' ' + this.lastName; } validatePassword(password) { return bcrypt.compareSync(password, this.password); } }