File size: 12,617 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | import { Response, NextFunction } from 'express';
import SkillGap from '../models/SkillGap';
import { Quiz, QuizAttempt } from '../models/Quiz';
import LearningPath from '../models/LearningPath';
import Progress from '../models/Progress';
import { AuthRequest } from '../types';
import { allLearningPaths } from '../data/learningPathTemplates';
// Core topics the system tracks
const TRACKED_TOPICS = ['JavaScript', 'React', 'TypeScript', 'Node.js', 'CSS', 'APIs', 'MongoDB'];
// Compute real skill levels from quiz attempts + learning path progress
const computeSkillLevels = async (userId: string) => {
// 1. Get quiz performance by topic
const attempts = await QuizAttempt.find({ userId });
const quizIds = [...new Set(attempts.map(a => a.quizId.toString()))];
const quizzes = await Quiz.find({ _id: { $in: quizIds } });
const quizMap = new Map(quizzes.map(q => [q._id.toString(), q]));
const topicStats: Record<string, { correct: number; total: number; attempts: number }> = {};
for (const attempt of attempts) {
const quiz = quizMap.get(attempt.quizId.toString());
if (!quiz) continue;
for (const answer of attempt.answers) {
const question = quiz.questions.find(q => q.questionId === answer.questionId);
const topic = question?.topic || quiz.topic;
if (!topicStats[topic]) {
topicStats[topic] = { correct: 0, total: 0, attempts: 0 };
}
topicStats[topic].total++;
topicStats[topic].attempts++;
if (answer.isCorrect) {
topicStats[topic].correct++;
}
}
}
// 2. Get learning path completion by module topic
const learningPaths = await LearningPath.find({ userId, isActive: true });
const pathCompletion: Record<string, { completed: number; total: number }> = {};
for (const lp of learningPaths) {
for (const step of lp.steps) {
// Map module titles to topics
const topicHint = detectTopicFromTitle(step.title);
if (!topicHint) continue;
if (!pathCompletion[topicHint]) {
pathCompletion[topicHint] = { completed: 0, total: 0 };
}
pathCompletion[topicHint].total++;
// Count completed checklist items
const completedItems = step.checklist.filter(c => c.completed).length;
const totalItems = step.checklist.length;
if (totalItems > 0) {
pathCompletion[topicHint].completed += completedItems;
}
}
}
// 3. Get Progress model data for additional context
const progress = await Progress.findOne({ userId });
const progressTopics: Record<string, number> = {};
if (progress?.topicProgress) {
for (const tp of progress.topicProgress) {
progressTopics[tp.topicName] = tp.masteryLevel;
}
}
// 4. Merge all data sources into final skill levels
const skills = TRACKED_TOPICS.map(topic => {
let currentLevel = 0;
let hasData = false;
let weightedComponents: { value: number; weight: number }[] = [];
// Quiz score (highest weight - 50%)
if (topicStats[topic] && topicStats[topic].total > 0) {
const quizScore = Math.round((topicStats[topic].correct / topicStats[topic].total) * 100);
weightedComponents.push({ value: quizScore, weight: 50 });
hasData = true;
}
// Learning path completion (30% weight)
if (pathCompletion[topic] && pathCompletion[topic].total > 0) {
const completionRate = Math.round((pathCompletion[topic].completed / pathCompletion[topic].total) * 100);
weightedComponents.push({ value: completionRate, weight: 30 });
hasData = true;
}
// Progress mastery (20% weight)
if (progressTopics[topic] !== undefined) {
weightedComponents.push({ value: progressTopics[topic], weight: 20 });
hasData = true;
}
if (hasData) {
const totalWeight = weightedComponents.reduce((sum, c) => sum + c.weight, 0);
currentLevel = Math.round(
weightedComponents.reduce((sum, c) => sum + (c.value * c.weight), 0) / totalWeight
);
}
const targetLevel = 100;
const gap = targetLevel - currentLevel;
let priority: 'high' | 'medium' | 'low' = 'low';
if (gap > 50) priority = 'high';
else if (gap > 25) priority = 'medium';
return { topic, currentLevel, targetLevel, gap, priority, hasData };
});
return skills;
};
// Map step titles to topics
const detectTopicFromTitle = (title: string): string | null => {
const lower = title.toLowerCase();
if (lower.includes('javascript') || lower.includes('es6') || lower.includes('async')) return 'JavaScript';
if (lower.includes('react') || lower.includes('jsx') || lower.includes('hooks') || lower.includes('component')) return 'React';
if (lower.includes('typescript') || lower.includes('types') || lower.includes('generics')) return 'TypeScript';
if (lower.includes('node') || lower.includes('express') || lower.includes('backend')) return 'Node.js';
if (lower.includes('css') || lower.includes('flexbox') || lower.includes('grid') || lower.includes('responsive')) return 'CSS';
if (lower.includes('api') || lower.includes('rest') || lower.includes('http')) return 'APIs';
if (lower.includes('mongo') || lower.includes('database') || lower.includes('mongoose')) return 'MongoDB';
if (lower.includes('auth') || lower.includes('jwt') || lower.includes('security')) return 'APIs';
return null;
};
// @desc Get user's skill gaps (computed from real data)
// @route GET /api/skill-gaps
// @access Private
export const getSkillGaps = async (
req: AuthRequest,
res: Response,
next: NextFunction
): Promise<void> => {
try {
const userId = req.user?._id;
if (!userId) {
res.status(401).json({ success: false, error: 'Not authenticated' });
return;
}
// Compute real skill levels from quiz + learning path + progress
const computedSkills = await computeSkillLevels(userId.toString());
// Check if user has any real data
const hasAnyData = computedSkills.some(s => s.hasData);
if (!hasAnyData) {
// No quiz/learning path data yet — return empty state with zeros
const defaultSkills = TRACKED_TOPICS.map(topic => ({
topic,
currentLevel: 0,
targetLevel: 100,
gap: 100,
priority: 'high' as const,
}));
// Upsert into DB
await SkillGap.findOneAndUpdate(
{ userId },
{ skills: defaultSkills, lastAssessmentDate: new Date() },
{ new: true, upsert: true }
);
res.status(200).json({
success: true,
data: {
skills: defaultSkills,
lastAssessmentDate: new Date(),
hasRealData: false,
message: 'Complete quizzes or learning path steps to see your real skill levels.',
},
});
return;
}
// Strip internal flags and persist
const skills = computedSkills.map(({ hasData, ...rest }) => rest);
const skillGap = await SkillGap.findOneAndUpdate(
{ userId },
{ skills, lastAssessmentDate: new Date() },
{ new: true, upsert: true }
);
res.status(200).json({
success: true,
data: {
...skillGap?.toObject(),
hasRealData: true,
},
});
} catch (error) {
next(error);
}
};
// @desc Submit skill assessment
// @route POST /api/skill-gaps/assessment
// @access Private
export const submitAssessment = async (
req: AuthRequest,
res: Response,
next: NextFunction
): Promise<void> => {
try {
const { skills } = req.body;
// Calculate priorities based on gaps
const processedSkills = skills.map((skill: { topic: string; currentLevel: number; targetLevel?: number }) => {
const gap = (skill.targetLevel || 100) - skill.currentLevel;
let priority: 'high' | 'medium' | 'low' = 'low';
if (gap > 50) priority = 'high';
else if (gap > 25) priority = 'medium';
return {
...skill,
targetLevel: skill.targetLevel || 100,
gap,
priority,
};
});
const skillGap = await SkillGap.findOneAndUpdate(
{ userId: req.user?._id },
{
skills: processedSkills,
lastAssessmentDate: new Date(),
},
{ new: true, upsert: true }
);
res.status(200).json({
success: true,
data: skillGap,
});
} catch (error) {
next(error);
}
};
// @desc Get skill improvement recommendations
// @route GET /api/skill-gaps/recommendations
// @access Private
export const getRecommendations = async (
req: AuthRequest,
res: Response,
next: NextFunction
): Promise<void> => {
try {
const userId = req.user?._id;
if (!userId) {
res.status(401).json({ success: false, error: 'Not authenticated' });
return;
}
// Re-compute from real data
const computedSkills = await computeSkillLevels(userId.toString());
const prioritySkills = computedSkills
.filter(s => s.priority === 'high' || s.priority === 'medium')
.sort((a, b) => b.gap - a.gap);
// Build recommendations with learning path links
const recommendations = prioritySkills.map(skill => {
// Find matching learning paths
const matchingPaths = allLearningPaths.filter(path =>
path.modules.some(m =>
m.milestones.some(ms =>
ms.checklistItems.some(ci =>
ci.title.toLowerCase().includes(skill.topic.toLowerCase()) ||
(ms.quizTopic || '').toLowerCase() === skill.topic.toLowerCase()
)
)
)
);
// Pull specific resource URLs for this topic
const resources: { title: string; url: string; type: string }[] = [];
for (const path of matchingPaths) {
for (const mod of path.modules) {
for (const ms of mod.milestones) {
for (const ci of ms.checklistItems) {
if (ci.url && (
ci.title.toLowerCase().includes(skill.topic.toLowerCase()) ||
(ms.quizTopic || '').toLowerCase() === skill.topic.toLowerCase()
)) {
resources.push({
title: ci.title,
url: ci.url,
type: ci.resourceType,
});
}
}
}
}
}
const estimatedWeeks = skill.gap > 50 ? '4-6 weeks' : skill.gap > 25 ? '2-4 weeks' : '1-2 weeks';
return {
topic: skill.topic,
currentLevel: skill.currentLevel,
gap: skill.gap,
priority: skill.priority,
suggestion: skill.currentLevel === 0
? `Start learning ${skill.topic} from scratch with beginner-friendly resources`
: `Improve your ${skill.topic} skills from ${skill.currentLevel}% to ${skill.targetLevel}%`,
estimatedTime: estimatedWeeks,
suggestedPaths: matchingPaths.map(p => ({ id: p.id, title: p.title })),
resources: resources.slice(0, 5),
};
});
res.status(200).json({
success: true,
data: {
prioritySkills: prioritySkills.map(({ hasData, ...rest }) => rest),
recommendations,
lastAssessment: new Date(),
},
});
} catch (error) {
next(error);
}
};
|