const { Model } = require('sequelize'); const bcrypt = require("bcryptjs"); module.exports = (sequelize, DataTypes) => { class Member extends Model { /** * Helper method for defining associations. * This method is not a part of Sequelize lifecycle. * The `models/index` file will call this method automatically. */ static associate(models) { Member.belongsTo(models.User, { foreignKey: 'loginId', targetKey: 'id' }); } } User.init({ firstName: { type: DataTypes.STRING, allowNull: false }, lastName: { type: DataTypes.STRING, allowNull: false }, dateOfBirth: { type: DataTypes.DATE, allowNull: false }, height: { type: DataTypes.INTEGER }, weight: { type: DataTypes.INTEGER }, handedness: { type: DataTypes.ENUM('LeftHandedness', 'RightHandedness'), }, position: { type: DataTypes.ENUM }, preferredPosition: { type: DataTypes.ENUM } }, { sequelize, modelName: 'User', tableName: "Users", hooks: { beforeCreate: async (user) => { const salt = await bcrypt.genSalt(10); user.password = await bcrypt.hash(user.password, salt); } } }, { defaultScope: { attributes: { exclude: ['password'] }, }, scopes: { withSecretColumns: { attributes: { include: ['password'] }, }, }, }); User.prototype.validPassword = function (password) { return bcrypt.compareSync(password, this.password); }; return User; };