File size: 4,153 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
import mongoose, { Schema } from 'mongoose';
import { ILearningPath } from '../types';

// Checklist item schema for granular progress tracking
const checklistItemSchema = new Schema({
    id: { type: String, required: true },
    title: { type: String, required: true },
    description: { type: String },
    completed: { type: Boolean, default: false },
    completedAt: { type: Date },
    estimatedHours: { type: Number, default: 1 },
    resourceType: {
        type: String,
        enum: ['video', 'article', 'exercise', 'project', 'quiz'],
        default: 'article',
    },
    isRequired: { type: Boolean, default: true },
    url: { type: String },
});

// Learning step schema with milestone support
const learningStepSchema = new Schema({
    title: { type: String, required: true },
    description: { type: String, required: true },
    order: { type: Number, required: true },
    completed: { type: Boolean, default: false },
    completedAt: { type: Date },
    estimatedTime: { type: Number, default: 60 }, // in minutes
    milestoneId: { type: String },
    moduleId: { type: String },
    checklist: [checklistItemSchema],
    quizTopic: { type: String },
    passingScore: { type: Number, default: 70 },
    needsReview: { type: Boolean, default: false },
    skippedDueToMastery: { type: Boolean, default: false },
    // Legacy support
    id: { type: Number },
    topic: { type: String },
    icon: { type: String, default: 'book' },
    status: {
        type: String,
        enum: ['completed', 'active', 'locked'],
        default: 'locked',
    },
    progress: { type: Number, default: 0 },
});

const learningPathSchema = new Schema<ILearningPath>(
    {
        userId: {
            type: Schema.Types.ObjectId,
            ref: 'User',
            required: true,
        },
        title: {
            type: String,
            required: true,
        },
        description: {
            type: String,
            required: true,
        },
        category: {
            type: String,
            default: 'Web Development',
        },
        difficulty: {
            type: String,
            enum: ['beginner', 'intermediate', 'advanced'],
            default: 'intermediate',
        },
        estimatedTime: {
            type: String,
            default: '24h 30m',
        },
        totalWeeks: {
            type: Number,
            default: 12,
        },
        totalSteps: {
            type: Number,
            default: 12,
        },
        steps: [learningStepSchema],
        currentStepIndex: {
            type: Number,
            default: 0,
        },
        progress: {
            type: Number,
            default: 0,
            min: 0,
            max: 100,
        },
        startedAt: {
            type: Date,
            default: Date.now,
        },
        completedAt: {
            type: Date,
        },
        isActive: {
            type: Boolean,
            default: true,
        },
        tags: [String],
    },
    {
        timestamps: true,
    }
);

// Indexes for efficient queries
learningPathSchema.index({ userId: 1, isActive: 1 });
learningPathSchema.index({ userId: 1, createdAt: -1 });

// Virtual for completion percentage
learningPathSchema.virtual('completionPercentage').get(function (this: ILearningPath) {
    if (!this.steps || this.steps.length === 0) return 0;
    const completed = this.steps.filter(s => s.completed).length;
    return Math.round((completed / this.steps.length) * 100);
});

// Pre-save hook to update progress
learningPathSchema.pre('save', function (this: ILearningPath, next) {
    if (this.steps && this.steps.length > 0) {
        const completed = this.steps.filter(s => s.completed).length;
        this.progress = Math.round((completed / this.steps.length) * 100);
        this.totalSteps = this.steps.length;

        // Mark as completed if all steps done
        if (this.progress === 100 && !this.completedAt) {
            this.completedAt = new Date();
        }
    }
    next();
});

const LearningPath = mongoose.model<ILearningPath>('LearningPath', learningPathSchema);

export default LearningPath;