'use strict'; const { isValidPassword, generateHashPassword } = require('../../helpers/security.helper'); module.exports = function (sequelize, DataTypes) { const User = sequelize.define('User', { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true, }, email: { type: DataTypes.STRING, allowNull: false, unique: true, validate: { isEmail: true } }, password: { type: DataTypes.STRING, allowNull: false, }, role: { type: DataTypes.TINYINT, allowNull: false, defaultValue: 0, }, }, { tableName: 'user', freezeTableName: true, timestamps: true, }); User.associate = function (models) { /*User.Categories = User.belongsToMany(models.Category, { through: models.UserCategory, foreignKey: 'UserId' });*/ //User.Comments = User.hasMany(models.UserComment, { foreignKey: 'UserId' }); //User.Reactions = User.hasMany(models.UserReaction, { foreignKey: 'UserId' }); //User.User = User.belongsTo(models.User, { foreignKey: 'userId' }); }; User.beforeCreate(async function (model, options) { const encrypted = await generateHashPassword(model.password) model.password = encrypted; return model; }); // Instance Methods // InventoryLevel.prototype.someMethod = function () {...} User.prototype.comparePassword = async function (candidatePassword) { const user = this; if (user.password) { return await isValidPassword(user.password, candidatePassword) } else { return false; } } return User; };