2019-04-24 21:01:54 +00:00
|
|
|
'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,
|
|
|
|
|
},
|
2019-06-13 07:53:57 +00:00
|
|
|
phone: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
allowNull: false,
|
|
|
|
|
unique: true
|
|
|
|
|
},
|
2019-04-24 21:01:54 +00:00
|
|
|
email: {
|
|
|
|
|
type: DataTypes.STRING,
|
2019-06-21 08:40:28 +00:00
|
|
|
// allowNull: false,
|
|
|
|
|
// unique: true,
|
|
|
|
|
// validate: { isEmail: true }
|
2019-04-24 21:01:54 +00:00
|
|
|
},
|
|
|
|
|
password: {
|
|
|
|
|
type: DataTypes.STRING,
|
2019-06-21 08:40:28 +00:00
|
|
|
// allowNull: false,
|
2019-06-13 07:53:57 +00:00
|
|
|
},
|
2019-06-21 08:40:28 +00:00
|
|
|
fbuid: {
|
2019-06-13 07:53:57 +00:00
|
|
|
type: DataTypes.STRING,
|
2019-04-24 21:01:54 +00:00
|
|
|
},
|
2019-06-13 07:53:57 +00:00
|
|
|
name: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
},
|
|
|
|
|
surname: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
},
|
|
|
|
|
profile_picture: {
|
|
|
|
|
type: DataTypes.STRING,
|
|
|
|
|
defaultValue: 'media/defaultProfile.png',
|
|
|
|
|
},
|
|
|
|
|
accessibility: {
|
|
|
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
|
defaultValue: true,
|
|
|
|
|
},
|
2019-07-09 13:18:25 +00:00
|
|
|
refresh_token: {
|
2019-07-09 10:14:18 +00:00
|
|
|
type: DataTypes.STRING(512),
|
2019-07-08 11:11:40 +00:00
|
|
|
},
|
|
|
|
|
state: {
|
|
|
|
|
type: DataTypes.STRING,
|
2019-07-09 10:14:18 +00:00
|
|
|
defaultValue: 'active',
|
2019-07-08 11:11:40 +00:00
|
|
|
},
|
2019-06-13 07:53:57 +00:00
|
|
|
lastlogin: {
|
|
|
|
|
type: DataTypes.DATE,
|
|
|
|
|
defaultValue: null,
|
2019-05-09 16:23:54 +00:00
|
|
|
},
|
2019-04-24 21:01:54 +00:00
|
|
|
}, {
|
2019-07-05 07:06:29 +00:00
|
|
|
tableName: 'users',
|
2019-04-24 21:01:54 +00:00
|
|
|
freezeTableName: true,
|
|
|
|
|
timestamps: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
User.associate = function (models) {
|
2019-06-13 07:53:57 +00:00
|
|
|
User.Roles = User.belongsToMany(models.Rol, {
|
|
|
|
|
through: models.UserRoles,
|
|
|
|
|
foreignKey: 'userId'
|
|
|
|
|
});
|
|
|
|
|
User.Entity = User.belongsTo(models.Entity, { foreignKey: 'entityId' });
|
|
|
|
|
User.Devices = User.hasMany(models.UserDevice, { foreignKey: 'userId' });
|
2019-07-05 07:06:29 +00:00
|
|
|
User.Comments = User.hasMany(models.Comment, { foreignKey: 'userId' });
|
|
|
|
|
User.EventsReservations = User.hasMany(models.EventReservation, { foreignKey: 'userId' });
|
|
|
|
|
User.EventsInscriptions = User.hasMany(models.EventInscription, { foreignKey: 'userId' });
|
|
|
|
|
// User.InscriptionsValidate = User.hasMany(models.EventIncription, { foreignkey: 'validateUserId'})
|
2019-06-13 07:53:57 +00:00
|
|
|
//User.Reactions = User.hasMany(models.UserReaction, { foreignKey: 'UserId' });
|
2019-04-24 21:01:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
User.beforeCreate(async function (model, options) {
|
2019-06-21 08:40:28 +00:00
|
|
|
if (model.password) {
|
|
|
|
|
const encrypted = await generateHashPassword(model.password)
|
|
|
|
|
model.password = encrypted;
|
|
|
|
|
}
|
2019-04-24 21:01:54 +00:00
|
|
|
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;
|
|
|
|
|
};
|