144 lines
2.9 KiB
JavaScript
144 lines
2.9 KiB
JavaScript
module.exports = (sequelize, DataTypes) => {
|
|
const EstablishmentUser = sequelize.define(
|
|
"establishment_users",
|
|
{
|
|
id: {
|
|
type: DataTypes.INTEGER,
|
|
autoIncrement: true,
|
|
primaryKey: true,
|
|
},
|
|
|
|
establishment_id: {
|
|
type: DataTypes.INTEGER,
|
|
allowNull: false,
|
|
},
|
|
|
|
name: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
},
|
|
|
|
email: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
unique: true,
|
|
},
|
|
|
|
password: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
},
|
|
|
|
gender: {
|
|
type: DataTypes.STRING,
|
|
allowNull: true,
|
|
},
|
|
|
|
is_active: {
|
|
type: DataTypes.BOOLEAN,
|
|
defaultValue: true,
|
|
},
|
|
|
|
created_at: {
|
|
type: DataTypes.DATE,
|
|
defaultValue: DataTypes.NOW,
|
|
},
|
|
|
|
created_by: {
|
|
type: DataTypes.INTEGER,
|
|
allowNull: false,
|
|
},
|
|
|
|
updated_at: {
|
|
type: DataTypes.DATE,
|
|
allowNull: true,
|
|
},
|
|
|
|
updated_by: {
|
|
type: DataTypes.INTEGER,
|
|
allowNull: true,
|
|
},
|
|
|
|
last_login: {
|
|
type: DataTypes.DATE,
|
|
allowNull: true,
|
|
},
|
|
|
|
reset_otp: {
|
|
type: DataTypes.STRING,
|
|
allowNull: true,
|
|
},
|
|
|
|
reset_otp_expires_at: {
|
|
type: DataTypes.DATE,
|
|
allowNull: true,
|
|
},
|
|
},
|
|
{
|
|
login_otp: {
|
|
type: DataTypes.STRING,
|
|
allowNull: true,
|
|
},
|
|
|
|
login_otp_expires_at: {
|
|
type: DataTypes.DATE,
|
|
allowNull: true,
|
|
},
|
|
},
|
|
{
|
|
timestamps: false,
|
|
tableName: "establishment_users",
|
|
|
|
// Hide sensitive fields by default
|
|
defaultScope: {
|
|
attributes: {
|
|
exclude: [
|
|
"password",
|
|
"reset_otp",
|
|
"reset_otp_expires_at",
|
|
"login_otp",
|
|
"login_otp_expires_at",
|
|
],
|
|
},
|
|
},
|
|
|
|
// Scope for login & sensitive operations
|
|
scopes: {
|
|
withSensitive: {
|
|
attributes: {
|
|
include: [
|
|
"password",
|
|
"reset_otp",
|
|
"reset_otp_expires_at",
|
|
"login_otp",
|
|
"login_otp_expires_at",
|
|
],
|
|
},
|
|
},
|
|
},
|
|
}
|
|
);
|
|
|
|
EstablishmentUser.prototype.toJSON = function () {
|
|
const values = { ...this.get() };
|
|
delete values.password;
|
|
delete values.reset_otp;
|
|
delete values.reset_otp_expires_at;
|
|
delete values.login_otp;
|
|
delete values.login_otp_expires_at;
|
|
return values;
|
|
};
|
|
|
|
// ====================================================
|
|
// Associations
|
|
// ====================================================
|
|
EstablishmentUser.associate = (models) => {
|
|
EstablishmentUser.belongsTo(models.Establishment, {
|
|
foreignKey: "establishment_id",
|
|
as: "establishments",
|
|
});
|
|
};
|
|
|
|
return EstablishmentUser;
|
|
};
|