import { formatDate } from '../helpers/helper';

export default (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    email: {
      allowNull: false,
      type: DataTypes.STRING,
      unique: true
    },
    firstname: {
      allowNull: false,
      type: DataTypes.STRING
    },
    lastname: {
      allowNull: false,
      type: DataTypes.STRING
    },
    username: {
      allowNull: false,
      type: DataTypes.STRING,
      unique: true
    },
    password: {
      allowNull: false,
      type: DataTypes.STRING,
    },
    roleId: {
      type: DataTypes.INTEGER
    },
    createdAt: {
      type: DataTypes.DATE,
      get() {
        return formatDate(this.getDataValue('createdAt'));
      },
    },
    updatedAt: {
      type: DataTypes.DATE,
      get() {
        return formatDate(this.getDataValue('updatedAt'));
      },
    }
  }, {
    getterMethods: {
      fullName() {
        return `${this.lastname} ${this.firstname}`;
      }
    }
  });

  User.associate = (models) => {
    User.hasMany(models.Document, {
      foreignKey: 'userId',
      as: 'documents',
    });
    User.belongsTo(models.Role, {
      foreignKey: 'roleId',
      onDelete: 'CASCADE'
    });
  };
  return User;
};

@@ -12,16 +12,30 @@
 rmatDate
+, hashPassword
  %7D from 
@@ -1228,16 +1228,133 @@
 );%0A  %7D;%0A
+%0A  User.beforeCreate(user =%3E hashPassword(user.password)%0A    .then((hash) =%3E %7B%0A      user.password = hash;%0A    %7D));%0A%0A
   return

add password hash hook