Files
gameno-api/components/professors/professorModel.js
T

90 lines
2.0 KiB
JavaScript

// /components/professors/professorModel.js
'use strict';
const mongoose = require('mongoose');
const professorSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
unique: true,
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,
toJSON: { virtuals: true },
toObject: { virtuals: true }
});
// Virtual properties delegating personal details to the linked User model
professorSchema.virtual('name').get(function () {
if (this.user && typeof this.user === 'object' && this.user.name) {
return this.user.name;
}
return 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 '';
});
professorSchema.virtual('nationalIdCode').get(function () {
if (this.user && typeof this.user === 'object') {
return this.user.nationalIdCode;
}
return undefined;
});
professorSchema.virtual('phoneNumber').get(function () {
if (this.user && typeof this.user === 'object') {
return this.user.phoneNumber;
}
return undefined;
});
professorSchema.virtual('email').get(function () {
if (this.user && typeof this.user === 'object') {
return this.user.email;
}
return undefined;
});
professorSchema.virtual('cardNumber').get(function () {
if (this.user && typeof this.user === 'object') {
return this.user.cardNumber;
}
return undefined;
});
professorSchema.virtual('shabaNumber').get(function () {
if (this.user && typeof this.user === 'object') {
return this.user.shabaNumber;
}
return undefined;
});
module.exports = mongoose.model('Professor', professorSchema);