UNPKG

education-module-ai

Version:
325 lines (324 loc) 12.6 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserProfileController = void 0; const agent_framework_backend_1 = require("@defikitdotnet/agent-framework-backend"); const uuid_1 = require("uuid"); const UserProfile_1 = require("../models/UserProfile"); const plugin_manager_1 = require("../plugins/plugin-manager"); // In-memory storage for demo purposes const profileStore = {}; // Create a resource implementation for UserProfileResource const userProfileResource = { name: "user-profiles", findAll: async () => Object.values(profileStore), findById: async (id) => profileStore[id] || null, create: async (data) => { const id = data.id || (0, uuid_1.v4)(); const newProfile = new UserProfile_1.UserProfileResource(); Object.assign(newProfile, data, { id, createdAt: new Date(), }); profileStore[id] = newProfile; return newProfile; }, update: async (id, data) => { if (!profileStore[id]) return null; const updated = { ...profileStore[id], ...data }; profileStore[id] = updated; return updated; }, delete: async (id) => { if (!profileStore[id]) return false; delete profileStore[id]; return true; }, }; /** * User profile API controller */ let UserProfileController = class UserProfileController extends agent_framework_backend_1.ResourceController { constructor() { super(userProfileResource); this.educationModuleAdapter = null; } setEducationModuleAdapter(adapter) { this.educationModuleAdapter = adapter; console.log("EducationModuleAdapter set in controller"); } /** * Get all user profiles using the database adapter */ async getAllUserProfiles(req, res) { if (this.educationModuleAdapter) { const profiles = await this.educationModuleAdapter.getAllUserProfiles(); res.status(200).json({ data: profiles }); } else { res.status(500).json({ error: "EducationModuleAdapter not set" }); } } /** * Get user profile by ID using the database adapter */ async getUserProfile(req, res) { const { id } = req.params; if (this.educationModuleAdapter) { const profile = await this.educationModuleAdapter.getUserProfile(id); res.status(200).json({ data: profile }); } else { res.status(500).json({ error: "EducationModuleAdapter not set" }); } } /** * Create or update a user profile using the database adapter */ async createUserProfile(req, res) { const { id } = req.params; if (this.educationModuleAdapter) { const profile = await this.educationModuleAdapter.saveUserProfile(req.body); res.status(200).json({ data: profile }); } else { res.status(500).json({ error: "EducationModuleAdapter not set" }); } } /** * Initialize a new user profile */ async initializeProfile(req, res) { const { name, email, learningStyles = [] } = req.body; if (!name) { res.status(400).json({ error: "Name is required" }); return; } try { // Create new profile const newProfile = await this.resource.create({ name, email, learningStyles: learningStyles, }); // Notify plugins await plugin_manager_1.pluginManager.notifyProfileCreated(newProfile); res.status(201).json({ data: newProfile }); } catch (error) { console.error("Error initializing user profile:", error); res.status(500).json({ error: "Failed to initialize user profile" }); } } /** * Update a user's learning styles */ async updateLearningStyles(req, res) { const { id } = req.params; const { learningStyles } = req.body; if (!Array.isArray(learningStyles)) { res.status(400).json({ error: "Learning styles must be an array" }); return; } try { const profile = await this.resource.findById(id); if (!profile) { res.status(404).json({ error: "Profile not found" }); return; } // Update learning styles const updatedProfile = await this.resource.update(id, { learningStyles: learningStyles, }); // Notify plugins await plugin_manager_1.pluginManager.notifyProfileUpdated(updatedProfile); res.status(200).json({ data: updatedProfile }); } catch (error) { console.error("Error updating learning styles:", error); res.status(500).json({ error: "Failed to update learning styles" }); } } /** * Add or update a subject for a user */ async addSubject(req, res) { const { id } = req.params; const { subject, level = 1 } = req.body; if (!subject) { res.status(400).json({ error: "Subject is required" }); return; } try { const profile = await this.resource.findById(id); if (!profile) { res.status(404).json({ error: "Profile not found" }); return; } // Create or update subject progress const subjectProgress = { ...profile.subjectProgress }; if (!subjectProgress[subject]) { subjectProgress[subject] = { level: parseInt(level || "1", 10), topics: {}, completedSessions: 0, completedContent: [], }; } else { subjectProgress[subject].level = level; } // Update profile const updatedProfile = await this.resource.update(id, { subjectProgress, }); // Notify plugins await plugin_manager_1.pluginManager.notifyProfileUpdated(updatedProfile); res.status(200).json({ data: updatedProfile }); } catch (error) { console.error("Error adding subject:", error); res.status(500).json({ error: "Failed to add subject" }); } } /** * Get content recommendations for a user */ async getRecommendations(req, res) { const { id } = req.params; try { const profile = await this.resource.findById(id); if (!profile) { res.status(404).json({ error: "Profile not found" }); return; } res.status(200).json({ data: profile.recommendations }); } catch (error) { console.error("Error getting recommendations:", error); res.status(500).json({ error: "Failed to get recommendations" }); } } /** * Get learning progress for a user */ async getProgress(req, res) { const { id } = req.params; const { subject } = req.query; try { const profile = await this.resource.findById(id); if (!profile) { res.status(404).json({ error: "Profile not found" }); return; } // Get progress for a specific subject or all subjects const progress = subject ? { [subject]: profile.subjectProgress[subject] } : profile.subjectProgress; res.status(200).json({ data: progress }); } catch (error) { console.error("Error getting progress:", error); res.status(500).json({ error: "Failed to get progress" }); } } /** * Update user's last active time and streak */ async updateActivity(req, res) { const { id } = req.params; try { const profile = await this.resource.findById(id); if (!profile) { res.status(404).json({ error: "Profile not found" }); return; } const now = new Date(); let streak = profile.streak; // Update streak logic - simplified for demo // A proper implementation would check if the last activity was yesterday if (!profile.lastActive || (now.getDate() !== profile.lastActive.getDate())) { streak += 1; } // Update profile const updatedProfile = await this.resource.update(id, { lastActive: now, streak, }); res.status(200).json({ data: { lastActive: now, streak } }); } catch (error) { console.error("Error updating activity:", error); res.status(500).json({ error: "Failed to update activity" }); } } }; exports.UserProfileController = UserProfileController; __decorate([ (0, agent_framework_backend_1.Get)("/"), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], UserProfileController.prototype, "getAllUserProfiles", null); __decorate([ (0, agent_framework_backend_1.Get)("/:id"), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], UserProfileController.prototype, "getUserProfile", null); __decorate([ (0, agent_framework_backend_1.Post)("/"), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], UserProfileController.prototype, "createUserProfile", null); __decorate([ (0, agent_framework_backend_1.Post)("/initialize"), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], UserProfileController.prototype, "initializeProfile", null); __decorate([ (0, agent_framework_backend_1.Post)("/:id/learning-styles"), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], UserProfileController.prototype, "updateLearningStyles", null); __decorate([ (0, agent_framework_backend_1.Post)("/:id/subjects"), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], UserProfileController.prototype, "addSubject", null); __decorate([ (0, agent_framework_backend_1.Get)("/:id/recommendations"), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], UserProfileController.prototype, "getRecommendations", null); __decorate([ (0, agent_framework_backend_1.Get)("/:id/progress"), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], UserProfileController.prototype, "getProgress", null); __decorate([ (0, agent_framework_backend_1.Post)("/:id/activity"), __metadata("design:type", Function), __metadata("design:paramtypes", [Object, Object]), __metadata("design:returntype", Promise) ], UserProfileController.prototype, "updateActivity", null); exports.UserProfileController = UserProfileController = __decorate([ (0, agent_framework_backend_1.Controller)("/api/user-profiles"), __metadata("design:paramtypes", []) ], UserProfileController);