Files
bowlingManager/backend/models/User.js
T
2025-04-24 12:01:51 +09:00

83 lines
1.7 KiB
JavaScript

const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');
const bcrypt = require('bcrypt');
const User = sequelize.define('User', {
id: {
type: DataTypes.INTEGER.UNSIGNED,
primaryKey: true,
autoIncrement: true
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
password: {
type: DataTypes.STRING,
allowNull: false
},
name: {
type: DataTypes.STRING,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true
}
},
role: {
type: DataTypes.ENUM('admin', 'clubadmin', 'user'),
allowNull: false,
defaultValue: 'user'
},
status: {
type: DataTypes.ENUM('active', 'inactive', 'suspended'),
allowNull: false,
defaultValue: 'active'
},
lastLoginAt: {
type: DataTypes.DATE,
allowNull: true
},
createdAt: {
type: DataTypes.DATE,
allowNull: false
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false
}
}, {
tableName: 'Users',
timestamps: true,
hooks: {
beforeCreate: async (user) => {
if (user.password) {
const salt = await bcrypt.genSalt(10);
user.password = await bcrypt.hash(user.password, salt);
}
},
beforeUpdate: async (user) => {
if (user.changed('password')) {
const salt = await bcrypt.genSalt(10);
user.password = await bcrypt.hash(user.password, salt);
}
}
}
});
// 비밀번호 검증 메소드
User.prototype.validatePassword = async function(password) {
return await bcrypt.compare(password, this.password);
};
User.associate = (models) => {
User.hasMany(models.Notification, { foreignKey: 'userId' });
};
module.exports = User;