Advertisement
Guest User

test

a guest
Mar 29th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. module.exports = function(sequelize, DataTypes) {
  2. var users = sequelize.define('users', {
  3. id: { type: DataTypes.INTEGER, allowNull: false, autoIncrement: true, primaryKey: true },
  4. email: { type: DataTypes.TEXT, allowNull: false, validate: { isEmail: true } },
  5. username: { type: DataTypes.TEXT, allowNull: false },
  6. firstName: { type: DataTypes.TEXT, allowNull: false },
  7. lastName: { type: DataTypes.TEXT, allowNull: false },
  8. vat: { type: DataTypes.STRING, allowNull: true },
  9. userType: { type: DataTypes.INTEGER, allowNull: false },
  10. status: { type: DataTypes.STRING, allowNull: true },
  11. token: { type: DataTypes.STRING, allowNull: true },
  12. password: {
  13. type: DataTypes.STRING,
  14. allowNull: false,
  15. set: function(v) {
  16. var salt = bcrypt.genSaltSync(10);
  17. var hash = bcrypt.hashSync(v, salt);
  18. this.setDataValue('password', hash);
  19. }
  20. }
  21. }, {
  22. classMethods: {
  23. associate: function(models) {
  24. users.hasMany(models.leads);
  25. users.hasMany(models.appointments, { onDelete: 'cascade', hooks:true, foreignKey: 'userId' });
  26. users.belongsTo(models.category);
  27. users.belongsTo(models.sub_category);
  28. users.belongsTo(models.market_subcat);
  29. users.belongsTo(models.market);
  30. },
  31. validPassword: function(password, passwd, done, user){
  32. bcrypt.compare(password, passwd, function(err, isMatch){
  33. if (err) console.log(err)
  34. if (isMatch) {
  35. return done(null, user)
  36. } else {
  37. return done(null, false)
  38. }
  39. })
  40. }
  41. },
  42. instanceMethods: {
  43. comparePassword : function(candidatePassword, cb) {
  44. bcrypt.compare(candidatePassword, this.getDataValue('password'), function(err, isMatch) {
  45. if(err) return cb(err);
  46. cb(null, isMatch);
  47. });
  48. },
  49. toJSON: function () {
  50. var values = this.get();
  51. delete values.password;
  52. return values;
  53. }
  54. }
  55. });
  56.  
  57. return users;
  58.  
  59. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement