'use strict'; const bcrypt = require('bcrypt'); const { Model } = require('sequelize'); module.exports = (sequelize, DataTypes) => { class User 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) { /* define association here */ } } User.init({ user_id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false}, user_username: {type: DataTypes.STRING, allowNull: false}, user_lastname: {type: DataTypes.STRING}, user_firstname: {type: DataTypes.STRING}, user_email: {type: DataTypes.STRING}, user_adresse: {type: DataTypes.STRING}, user_ville: {type: DataTypes.STRING}, user_region: {type: DataTypes.STRING}, user_cp: {type: DataTypes.STRING}, user_pays: {type: DataTypes.STRING}, user_tel: {type: DataTypes.STRING}, user_mobile: {type: DataTypes.STRING}, user_role: {type: DataTypes.INTEGER} }, { sequelize, timestamps: false, modelName: 'User', tableName: 'user', createdAt: true, updatedAt: true }); User.prototype.matchPassword = async function (enteredPassword) { return await bcrypt.compare(enteredPassword, this.password); }; const DEFAULT_SALT_ROUNDS = 10; User.addHook("beforeCreate", async (user) => { const encryptedPassword = await bcrypt.hash( user.password, DEFAULT_SALT_ROUNDS ); user.password = encryptedPassword; }); User.prototype.toJSONFor = function () { return { id: this.mbr_id, username: this.mbr_pseudo, lastname: this.mbr_nom, firstname: this.mbr_prenom, email: this.mbr_email, adresse: this.mbr_adresse, ville: this.mbr_ville, region: this.mbr_region, cp: this.mbr_cp, pays: this.mbr_pays, tel: this.mbr_tel, mobile: this.mbr_mobile, statut: this.mbr_statut }; }; return User; };