File size: 4,461 Bytes
81b66b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import mongoose, { Schema } from 'mongoose';
import bcrypt from 'bcryptjs';
import { IUser } from '../types';

const userSchema = new Schema<IUser>(
    {
        email: {
            type: String,
            required: [true, 'Email is required'],
            unique: true,
            lowercase: true,
            trim: true,
        },
        password: {
            type: String,
            required: [true, 'Password is required'],
            minlength: 6,
            select: false,
        },
        name: {
            type: String,
            required: [true, 'Name is required'],
            trim: true,
        },
        avatar: {
            type: String,
            default: '',
        },
        role: {
            type: String,
            enum: ['student', 'teacher', 'admin'],
            default: 'student',
        },
        learnerProfile: {
            learningStyle: {
                type: String,
                enum: ['visual', 'auditory', 'kinesthetic', 'reading'],
                default: 'visual',
            },
            secondaryStyle: {
                type: String,
                enum: ['visual', 'auditory', 'kinesthetic', 'reading'],
            },
            academicHistory: [{
                institution: String,
                degree: String,
                fieldOfStudy: String,
                startYear: Number,
                endYear: Number,
                grades: [{
                    subject: String,
                    grade: String,
                    score: Number,
                }],
            }],
            strengths: {
                type: [String],
                default: [],
            },
            weaknesses: {
                type: [String],
                default: [],
            },
            preferredLearningTime: {
                startHour: { type: Number, default: 9 },
                endHour: { type: Number, default: 17 },
                timezone: { type: String, default: 'UTC' },
            },
            averageSessionDuration: {
                type: Number,
                default: 45, // minutes
            },
            learningPace: {
                type: String,
                enum: ['slow', 'moderate', 'fast'],
                default: 'moderate',
            },
            dailyGoalMinutes: {
                type: Number,
                default: 60,
            },
            weeklyGoalHours: {
                type: Number,
                default: 10,
            },
            interests: {
                type: [String],
                default: [],
            },
            careerGoals: {
                type: [String],
                default: [],
            },
        },
        preferences: {
            theme: {
                type: String,
                enum: ['light', 'dark', 'system'],
                default: 'system',
            },
            notifications: {
                type: Boolean,
                default: true,
            },
            emailUpdates: {
                type: Boolean,
                default: true,
            },
            language: {
                type: String,
                default: 'en',
            },
            accessibilityMode: {
                type: Boolean,
                default: false,
            },
            twoFactorEnabled: {
                type: Boolean,
                default: false,
            },
        },
        currentLearningPathId: {
            type: Schema.Types.ObjectId,
            ref: 'LearningPath',
        },
        isVerified: {
            type: Boolean,
            default: false,
        },
        verificationToken: String,
        verificationTokenExpires: Date,
        resetPasswordToken: String,
        resetPasswordExpires: Date,
        lastActive: {
            type: Date,
            default: Date.now,
        },
    },
    {
        timestamps: true,
    }
);

// Hash password before saving
userSchema.pre('save', async function (next) {
    if (!this.isModified('password')) {
        return next();
    }
    const salt = await bcrypt.genSalt(10);
    this.password = await bcrypt.hash(this.password, salt);
    next();
});

// Compare password method
userSchema.methods.comparePassword = async function (candidatePassword: string): Promise<boolean> {
    return bcrypt.compare(candidatePassword, this.password);
};

const User = mongoose.model<IUser>('User', userSchema);

export default User;