2022-02-18 19:32:30 +00:00
"use strict" ;
2019-04-24 21:01:54 +00:00
2023-06-14 09:34:19 +00:00
const { isValidPassword , generateHashPassword } = require ( "../../helpers/security.helper" ) ;
2019-04-24 21:01:54 +00:00
module . exports = function ( sequelize , DataTypes ) {
2022-02-18 19:32:30 +00:00
const User = sequelize . define (
"User" ,
{
id : {
type : DataTypes . UUID ,
defaultValue : DataTypes . UUIDV4 ,
primaryKey : true ,
} ,
phone : {
type : DataTypes . STRING ,
unique : true ,
allowNull : true , //Tiene que poderse dar de alta usuarios solo con el correo electronico para que siga funcionando las invitaciones por web como hasta ahora
} ,
email : {
type : DataTypes . STRING ,
unique : true ,
// allowNull: false,
// validate: { isEmail: true }
} ,
password : {
type : DataTypes . STRING ,
// allowNull: false,
} ,
fbuid : {
type : DataTypes . STRING ,
} ,
name : {
type : DataTypes . STRING ,
} ,
surname : {
type : DataTypes . STRING ,
} ,
profile _picture : {
type : DataTypes . STRING ,
defaultValue : "media/defaultProfile.png" ,
} ,
accessibility : {
type : DataTypes . BOOLEAN ,
defaultValue : true ,
} ,
profile : {
type : DataTypes . STRING ,
} ,
refresh _token : {
type : DataTypes . STRING ( 512 ) ,
} ,
state : {
type : DataTypes . STRING ,
defaultValue : "active" ,
} ,
app _version : {
type : DataTypes . STRING ,
} ,
level : {
type : DataTypes . INTEGER ,
defaultValue : 1 ,
} ,
lastlogin : {
type : DataTypes . DATE ,
defaultValue : null ,
} ,
2023-06-14 09:34:19 +00:00
country : {
type : DataTypes . STRING ,
defaultValue : "ES" ,
} ,
2022-02-18 19:32:30 +00:00
} ,
{
tableName : "users" ,
freezeTableName : true ,
timestamps : true ,
}
) ;
2022-02-17 12:12:13 +00:00
2022-02-18 19:32:30 +00:00
User . associate = function ( models ) {
User . Roles = User . belongsToMany ( models . Rol , {
through : models . UserRoles ,
foreignKey : "userId" ,
as : "roles" ,
} ) ;
User . Entity = User . belongsTo ( models . Entity , { foreignKey : "entityId" } ) ;
User . EventsCreates = User . hasMany ( models . Event , { foreignKey : "userId" } ) ;
User . Devices = User . hasMany ( models . UserDevice , { foreignKey : "userId" } ) ;
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 . Questions = User . hasMany ( models . EventQuestion , {
foreignKey : "userId" ,
as : "questions" ,
required : false ,
2019-04-24 21:01:54 +00:00
} ) ;
2022-02-18 19:32:30 +00:00
// User.InscriptionsValidate = User.hasMany(models.EventIncription, { foreignkey: 'validateUserId'})
//User.Reactions = User.hasMany(models.UserReaction, { foreignKey: 'UserId' });
} ;
2019-04-24 21:01:54 +00:00
2022-02-18 19:32:30 +00:00
User . beforeCreate ( async function ( model , options ) {
if ( model . password ) {
const encrypted = await generateHashPassword ( model . password ) ;
model . password = encrypted ;
}
return model ;
} ) ;
2019-04-24 21:01:54 +00:00
2022-02-18 19:32:30 +00:00
// Instance Methods
// InventoryLevel.prototype.someMethod = function () {...}
2019-04-24 21:01:54 +00:00
2022-02-18 19:32:30 +00:00
User . prototype . comparePassword = async function ( candidatePassword ) {
const user = this ;
if ( user . password ) {
return await isValidPassword ( user . password , candidatePassword ) ;
} else {
return false ;
2019-04-24 21:01:54 +00:00
}
2022-02-18 19:32:30 +00:00
} ;
2019-04-24 21:01:54 +00:00
2022-02-18 19:32:30 +00:00
return User ;
} ;