fix(professors, settings): add auto-migration for legacy professors, deep populate user in class/session/course services, and fix SMS bypass number persistence

This commit is contained in:
2026-08-26 05:17:41 +03:30
parent 0a022c0917
commit 43404f2dfc
12 changed files with 337 additions and 57 deletions
+25 -15
View File
@@ -7,8 +7,6 @@ const professorSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
unique: true,
index: true
},
bio: {
@@ -29,16 +27,28 @@ const professorSchema = new mongoose.Schema({
}
}, {
timestamps: true,
strict: false,
toJSON: { virtuals: true },
toObject: { virtuals: true }
});
// Virtual properties delegating personal details to the linked User model
// Auto-populate linked user document on find queries unless explicitly skipped
professorSchema.pre(/^find/, function (next) {
if (this.options?._skipUserPopulate !== true) {
this.populate({
path: 'user',
select: 'name nationalIdCode phoneNumber email cardNumber shabaNumber role isActive'
});
}
next();
});
// Virtual properties delegating personal details to the linked User model with legacy fallback
professorSchema.virtual('name').get(function () {
if (this.user && typeof this.user === 'object' && this.user.name) {
return this.user.name;
}
return undefined;
return this._doc?.name || undefined;
});
professorSchema.virtual('surname').get(function () {
@@ -48,42 +58,42 @@ professorSchema.virtual('surname').get(function () {
return parts.slice(1).join(' ');
}
}
return '';
return this._doc?.surname || '';
});
professorSchema.virtual('nationalIdCode').get(function () {
if (this.user && typeof this.user === 'object') {
if (this.user && typeof this.user === 'object' && this.user.nationalIdCode) {
return this.user.nationalIdCode;
}
return undefined;
return this._doc?.nationalIdCode || undefined;
});
professorSchema.virtual('phoneNumber').get(function () {
if (this.user && typeof this.user === 'object') {
if (this.user && typeof this.user === 'object' && this.user.phoneNumber) {
return this.user.phoneNumber;
}
return undefined;
return this._doc?.phoneNumber || undefined;
});
professorSchema.virtual('email').get(function () {
if (this.user && typeof this.user === 'object') {
if (this.user && typeof this.user === 'object' && this.user.email) {
return this.user.email;
}
return undefined;
return this._doc?.email || undefined;
});
professorSchema.virtual('cardNumber').get(function () {
if (this.user && typeof this.user === 'object') {
if (this.user && typeof this.user === 'object' && this.user.cardNumber) {
return this.user.cardNumber;
}
return undefined;
return this._doc?.cardNumber || undefined;
});
professorSchema.virtual('shabaNumber').get(function () {
if (this.user && typeof this.user === 'object') {
if (this.user && typeof this.user === 'object' && this.user.shabaNumber) {
return this.user.shabaNumber;
}
return undefined;
return this._doc?.shabaNumber || undefined;
});
module.exports = mongoose.model('Professor', professorSchema);