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

@ -74,41 +74,64 @@ module.exports = (sequelize, DataTypes) => {
allowNull: true,
},
},
{
login_otp: {
type: DataTypes.STRING,
allowNull: true,
},
login_otp_expires_at: {
type: DataTypes.DATE,
allowNull: true,
},
},
{
// ============================================================
// Model Options
// ============================================================
timestamps: false,
tableName: "establishment_users",
// 1. Hide sensitive fields by default
// Hide sensitive fields by default
defaultScope: {
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: {
withSensitive: {
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 () {
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;
};
// Relationships
// ====================================================
// Associations
// ====================================================
EstablishmentUser.associate = (models) => {
EstablishmentUser.belongsTo(models.Establishment, {
foreignKey: "establishment_id",

View File

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