const { DataTypes } = require('sequelize'); const { sequelize } = require('../config/db'); const bcrypt = require('bcryptjs'); const User = sequelize.define('user', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, name: { type: DataTypes.STRING, allowNull: false }, email: { type: DataTypes.STRING, allowNull: false, unique: true, validate: { isEmail: true } }, password: { type: DataTypes.STRING, allowNull: false }, profilePicUrl: { type: DataTypes.STRING, defaultValue: '/uploads/default-avatar.jpg' }, notificationsEnabled: { type: DataTypes.BOOLEAN, defaultValue: 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); } } }, tableName: 'users' // Explicitly set lowercase table name }); // Method to check if password matches User.prototype.matchPassword = async function(enteredPassword) { return await bcrypt.compare(enteredPassword, this.password); }; module.exports = User;