module changes updated

This commit is contained in:
unknown 2025-12-30 16:48:14 +05:30
parent b904b18355
commit 88d7617da5
2 changed files with 62 additions and 17 deletions

View File

@ -75,40 +75,63 @@ module.exports = (sequelize, DataTypes) => {
}, },
}, },
{ {
// ============================================================ login_otp: {
// Model Options type: DataTypes.STRING,
// ============================================================ allowNull: true,
},
login_otp_expires_at: {
type: DataTypes.DATE,
allowNull: true,
},
},
{
timestamps: false, timestamps: false,
tableName: "establishment_users", tableName: "establishment_users",
// 1. Hide sensitive fields by default // Hide sensitive fields by default
defaultScope: { defaultScope: {
attributes: { attributes: {
exclude: ["password", "reset_otp", "reset_otp_expires_at"], exclude: [
"password",
"reset_otp",
"reset_otp_expires_at",
"login_otp",
"login_otp_expires_at",
],
}, },
}, },
// 2. Scope for login / sensitive queries // Scope for login & sensitive operations
scopes: { scopes: {
withSensitive: { withSensitive: {
attributes: { attributes: {
include: ["password", "reset_otp", "reset_otp_expires_at"], include: [
"password",
"reset_otp",
"reset_otp_expires_at",
"login_otp",
"login_otp_expires_at",
],
}, },
}, },
}, },
} }
); );
// 3. Remove sensitive fields from all API responses
EstablishmentUser.prototype.toJSON = function () { EstablishmentUser.prototype.toJSON = function () {
const values = { ...this.get() }; const values = { ...this.get() };
delete values.password; delete values.password;
delete values.reset_otp; delete values.reset_otp;
delete values.reset_otp_expires_at; delete values.reset_otp_expires_at;
delete values.login_otp;
delete values.login_otp_expires_at;
return values; return values;
}; };
// Relationships // ====================================================
// Associations
// ====================================================
EstablishmentUser.associate = (models) => { EstablishmentUser.associate = (models) => {
EstablishmentUser.belongsTo(models.Establishment, { EstablishmentUser.belongsTo(models.Establishment, {
foreignKey: "establishment_id", foreignKey: "establishment_id",

View File

@ -32,15 +32,32 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.DATE, type: DataTypes.DATE,
allowNull: true, allowNull: true,
}, },
login_otp: {
type: DataTypes.STRING,
allowNull: true,
},
login_otp_expires_at: {
type: DataTypes.DATE,
allowNull: true,
},
is_active: { is_active: {
type: DataTypes.BOOLEAN, type: DataTypes.BOOLEAN,
defaultValue: true, defaultValue: true,
}, },
}, },
{ {
// 1. EXCLUDE sensitive fields from all queries defaultScope: {
defaultScope: { attributes: {
attributes: { exclude: ["password", "reset_otp", "reset_otp_expires_at"] }, exclude: [
"password",
"reset_otp",
"reset_otp_expires_at",
"login_otp",
"login_otp_expires_at",
],
},
}, },
// ------------------------------------------------------------- // -------------------------------------------------------------
@ -50,13 +67,18 @@ module.exports = (sequelize, DataTypes) => {
scopes: { scopes: {
withSensitive: { withSensitive: {
attributes: { attributes: {
include: ["password", "reset_otp", "reset_otp_expires_at"], include: [
"password",
"reset_otp",
"reset_otp_expires_at",
"login_otp",
"login_otp_expires_at",
],
}, },
}, },
}, },
} }
); );
return User; return User;
}; };