// /components/professors/professorModel.js 'use strict'; const mongoose = require('mongoose'); const professorSchema = new mongoose.Schema({ user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', index: true }, bio: { type: String, trim: true }, expertise: [{ type: String, trim: true }], courses: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Course' }], isActive: { type: Boolean, default: true } }, { timestamps: true, strict: false, toJSON: { virtuals: true }, toObject: { virtuals: true } }); // 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 this._doc?.name || undefined; }); professorSchema.virtual('surname').get(function () { if (this.user && typeof this.user === 'object' && this.user.name) { const parts = String(this.user.name).trim().split(/\s+/); if (parts.length > 1) { return parts.slice(1).join(' '); } } return this._doc?.surname || ''; }); professorSchema.virtual('nationalIdCode').get(function () { if (this.user && typeof this.user === 'object' && this.user.nationalIdCode) { return this.user.nationalIdCode; } return this._doc?.nationalIdCode || undefined; }); professorSchema.virtual('phoneNumber').get(function () { if (this.user && typeof this.user === 'object' && this.user.phoneNumber) { return this.user.phoneNumber; } return this._doc?.phoneNumber || undefined; }); professorSchema.virtual('email').get(function () { if (this.user && typeof this.user === 'object' && this.user.email) { return this.user.email; } return this._doc?.email || undefined; }); professorSchema.virtual('cardNumber').get(function () { if (this.user && typeof this.user === 'object' && this.user.cardNumber) { return this.user.cardNumber; } return this._doc?.cardNumber || undefined; }); professorSchema.virtual('shabaNumber').get(function () { if (this.user && typeof this.user === 'object' && this.user.shabaNumber) { return this.user.shabaNumber; } return this._doc?.shabaNumber || undefined; }); module.exports = mongoose.model('Professor', professorSchema);