UNPKG

@defikitdotnet/education-module-ai

Version:
943 lines 51.1 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CourseService = void 0; /** * Service class for handling course-related business logic */ var CourseService = /** @class */ (function () { function CourseService(courseRepository, sectionRepository, lectureRepository, quizRepository, quizQuestionRepository, categoryRepository, enrollmentRepository, dbAdapter) { this.courseRepository = courseRepository; this.sectionRepository = sectionRepository; this.lectureRepository = lectureRepository; this.quizRepository = quizRepository; this.quizQuestionRepository = quizQuestionRepository; this.categoryRepository = categoryRepository; this.enrollmentRepository = enrollmentRepository; this.dbAdapter = dbAdapter; } /** * Create a new course */ CourseService.prototype.createCourse = function (courseData) { return __awaiter(this, void 0, void 0, function () { var visibility, _i, _a, categoryId, category, dataToCreate, course; return __generator(this, function (_b) { switch (_b.label) { case 0: // Validate the level if (!["beginner", "intermediate", "advanced"].includes(courseData.level)) { throw new Error("Invalid level. Must be beginner, intermediate, or advanced."); } visibility = courseData.visibility || "private"; if (!["private", "public"].includes(visibility)) { throw new Error("Invalid visibility. Must be private or public."); } if (!(courseData.categories && courseData.categories.length > 0)) return [3 /*break*/, 4]; _i = 0, _a = courseData.categories; _b.label = 1; case 1: if (!(_i < _a.length)) return [3 /*break*/, 4]; categoryId = _a[_i]; return [4 /*yield*/, this.categoryRepository.findById(categoryId)]; case 2: category = _b.sent(); if (!category) { throw new Error("Category with ID ".concat(categoryId, " not found")); } // Ensure the category belongs to the same teacher if (category.teacherId !== courseData.teacherId) { throw new Error("Category with ID ".concat(categoryId, " does not belong to this teacher")); } _b.label = 3; case 3: _i++; return [3 /*break*/, 1]; case 4: dataToCreate = { teacherId: courseData.teacherId, title: courseData.title, description: courseData.description || "", thumbnail: courseData.thumbnail || "", level: courseData.level, visibility: visibility, // Pass visibility // Repository needs to handle converting array to JSON string categories: JSON.stringify(courseData.categories || []), // Pass categories as JSON string createdAt: new Date(), updatedAt: new Date(), }; return [4 /*yield*/, this.courseRepository.create(dataToCreate)]; case 5: course = _b.sent(); return [2 /*return*/, course]; } }); }); }; CourseService.prototype.parseCategories = function (categoriesData) { if (Array.isArray(categoriesData)) { return categoriesData; // Already an array } if (typeof categoriesData === "string") { try { var parsed = JSON.parse(categoriesData); return Array.isArray(parsed) ? parsed : []; } catch (e) { console.error("Failed to parse categories JSON:", e); return []; // Return empty array on parse error } } return []; // Default to empty array }; /** * Create content for a course */ CourseService.prototype.createContent = function (courseId, contentData) { return __awaiter(this, void 0, void 0, function () { var course, sections, sectionId, newSection, lectures, orderIndex, content, lectureContent, newLecture; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.courseRepository.findById(courseId)]; case 1: course = _a.sent(); if (!course) { throw new Error("Course with ID ".concat(courseId, " not found")); } // Validate content type if (!["text", "video", "h5p"].includes(contentData.type)) { throw new Error("Invalid content type. Must be text, video, or h5p."); } return [4 /*yield*/, this.sectionRepository.findByCourseId(courseId)]; case 2: sections = _a.sent(); if (!(sections.length === 0)) return [3 /*break*/, 4]; return [4 /*yield*/, this.sectionRepository.create({ courseId: courseId, title: "Section 1", description: "First section", orderIndex: 0, createdAt: new Date(), updatedAt: new Date(), })]; case 3: newSection = _a.sent(); sectionId = newSection.id; return [3 /*break*/, 5]; case 4: // Use the first section sectionId = sections[0].id; _a.label = 5; case 5: return [4 /*yield*/, this.lectureRepository.findBySectionId(sectionId)]; case 6: lectures = _a.sent(); orderIndex = lectures.length; content = contentData.content; lectureContent = ""; if (contentData.type === "text") { lectureContent = content.text || ""; } else if (contentData.type === "video") { lectureContent = JSON.stringify({ url: content.url || "" }); } else if (contentData.type === "h5p") { lectureContent = JSON.stringify({ h5pId: content.h5p || "" }); } return [4 /*yield*/, this.lectureRepository.create({ sectionId: sectionId, title: contentData.title, content: lectureContent, type: contentData.type, orderIndex: orderIndex, createdAt: new Date(), updatedAt: new Date(), })]; case 7: newLecture = _a.sent(); return [2 /*return*/, { id: newLecture.id, courseId: courseId, title: newLecture.title, description: contentData.description || "", type: contentData.type, content: contentData.content, order: orderIndex, createdAt: newLecture.createdAt, updatedAt: newLecture.updatedAt, }]; } }); }); }; /** * Get all courses for a teacher */ CourseService.prototype.getCoursesByTeacherId = function (teacherId) { return __awaiter(this, void 0, void 0, function () { var courses, categories, categoryMap, enhancedCourses, _i, courses_1, course, sections, lectureCount, _a, sections_1, section, lectures, parsedCategories, categoryNames; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.courseRepository.findByTeacherId(teacherId)]; case 1: courses = _b.sent(); return [4 /*yield*/, this.categoryRepository.findByTeacherId(teacherId)]; case 2: categories = _b.sent(); categoryMap = new Map(categories.map(function (cat) { return [cat.id, cat]; })); enhancedCourses = []; _i = 0, courses_1 = courses; _b.label = 3; case 3: if (!(_i < courses_1.length)) return [3 /*break*/, 10]; course = courses_1[_i]; return [4 /*yield*/, this.sectionRepository.findByCourseId(course.id)]; case 4: sections = _b.sent(); lectureCount = 0; _a = 0, sections_1 = sections; _b.label = 5; case 5: if (!(_a < sections_1.length)) return [3 /*break*/, 8]; section = sections_1[_a]; return [4 /*yield*/, this.lectureRepository.findBySectionId(section.id)]; case 6: lectures = _b.sent(); lectureCount += lectures.length; _b.label = 7; case 7: _a++; return [3 /*break*/, 5]; case 8: parsedCategories = this.parseCategories(course.categories); categoryNames = parsedCategories .map(function (catId) { var _a; return (_a = categoryMap.get(catId)) === null || _a === void 0 ? void 0 : _a.name; }) .filter(Boolean); enhancedCourses.push({ id: course.id, teacherId: course.teacherId, title: course.title, description: course.description, thumbnail: course.thumbnail, level: course.level, visibility: course.visibility, createdAt: course.createdAt || new Date(), updatedAt: course.updatedAt || new Date(), categories: parsedCategories, sectionCount: sections.length, lectureCount: lectureCount, categoryNames: categoryNames, }); _b.label = 9; case 9: _i++; return [3 /*break*/, 3]; case 10: return [2 /*return*/, enhancedCourses]; } }); }); }; /** * Get a course by ID with all sections, lectures, and quizzes */ CourseService.prototype.getCourseDetail = function (courseId) { return __awaiter(this, void 0, void 0, function () { var course, parsedCategories, categoryNames, _i, parsedCategories_1, categoryId, category, sections, sectionsWithDetails, _a, sections_2, section, lectures, quiz, quizWithQuestions, questions, secCourseId, secCreatedAt, secUpdatedAt, sectionData; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.courseRepository.findById(courseId)]; case 1: course = _b.sent(); if (!course) { return [2 /*return*/, null]; } parsedCategories = this.parseCategories(course.categories); categoryNames = []; _i = 0, parsedCategories_1 = parsedCategories; _b.label = 2; case 2: if (!(_i < parsedCategories_1.length)) return [3 /*break*/, 5]; categoryId = parsedCategories_1[_i]; return [4 /*yield*/, this.categoryRepository.findById(categoryId)]; case 3: category = _b.sent(); if (category) categoryNames.push(category.name); _b.label = 4; case 4: _i++; return [3 /*break*/, 2]; case 5: return [4 /*yield*/, this.sectionRepository.findByCourseId(courseId)]; case 6: sections = _b.sent(); sectionsWithDetails = []; _a = 0, sections_2 = sections; _b.label = 7; case 7: if (!(_a < sections_2.length)) return [3 /*break*/, 13]; section = sections_2[_a]; return [4 /*yield*/, this.lectureRepository.findBySectionId(section.id)]; case 8: lectures = _b.sent(); return [4 /*yield*/, this.quizRepository.findBySectionId(section.id)]; case 9: quiz = _b.sent(); quizWithQuestions = void 0; if (!quiz) return [3 /*break*/, 11]; return [4 /*yield*/, this.quizQuestionRepository.findByQuizId(quiz.id)]; case 10: questions = _b.sent(); quizWithQuestions = __assign(__assign({}, quiz), { questions: questions }); _b.label = 11; case 11: secCourseId = section.courseId, secCreatedAt = section.createdAt, secUpdatedAt = section.updatedAt, sectionData = __rest(section, ["courseId", "createdAt", "updatedAt"]); sectionsWithDetails.push(__assign(__assign({}, sectionData), { lectures: lectures, quiz: quizWithQuestions })); _b.label = 12; case 12: _a++; return [3 /*break*/, 7]; case 13: return [2 /*return*/, { id: course.id, teacherId: course.teacherId, title: course.title, description: course.description, thumbnail: course.thumbnail, level: course.level, visibility: course.visibility, createdAt: course.createdAt || new Date(), updatedAt: course.updatedAt || new Date(), categories: parsedCategories, sections: sectionsWithDetails, categoryNames: categoryNames, }]; } }); }); }; /** * Update a course */ CourseService.prototype.updateCourse = function (courseId, data) { return __awaiter(this, void 0, void 0, function () { var existingCourse, _i, _a, categoryId, category, courseUpdateData, existingSections, existingSectionIds, incomingSectionIds, _b, _c, sectionPayload, currentSectionId, newSection, sectionsToDelete, _d, sectionsToDelete_1, sectionToDelete; return __generator(this, function (_e) { switch (_e.label) { case 0: return [4 /*yield*/, this.courseRepository.findById(courseId)]; case 1: existingCourse = _e.sent(); if (!existingCourse) return [2 /*return*/, null]; if (!(data.categories && data.categories.length > 0)) return [3 /*break*/, 5]; _i = 0, _a = data.categories; _e.label = 2; case 2: if (!(_i < _a.length)) return [3 /*break*/, 5]; categoryId = _a[_i]; return [4 /*yield*/, this.categoryRepository.findById(categoryId)]; case 3: category = _e.sent(); if (!category || category.teacherId !== existingCourse.teacherId) { throw new Error("Category with ID ".concat(categoryId, " not found or does not belong to this teacher")); } _e.label = 4; case 4: _i++; return [3 /*break*/, 2]; case 5: courseUpdateData = { title: data.title, description: data.description, thumbnail: data.thumbnail, level: data.level, visibility: data.visibility, categories: JSON.stringify(data.categories || []), updatedAt: new Date(), }; return [4 /*yield*/, this.courseRepository.update(courseId, courseUpdateData)]; case 6: _e.sent(); return [4 /*yield*/, this.sectionRepository.findByCourseId(courseId)]; case 7: existingSections = _e.sent(); existingSectionIds = new Set(existingSections.map(function (sec) { return sec.id; })); incomingSectionIds = new Set(data.sections.map(function (sec) { return sec.id; }).filter(Boolean)); _b = 0, _c = data.sections; _e.label = 8; case 8: if (!(_b < _c.length)) return [3 /*break*/, 16]; sectionPayload = _c[_b]; sectionPayload.orderIndex; // Assuming orderIndex is provided correctly from frontend based on array order currentSectionId = void 0; if (!(sectionPayload.id && existingSectionIds.has(sectionPayload.id))) return [3 /*break*/, 10]; return [4 /*yield*/, this.sectionRepository.update(sectionPayload.id, { title: sectionPayload.title, description: sectionPayload.description, orderIndex: sectionPayload.orderIndex, updatedAt: new Date(), })]; case 9: _e.sent(); currentSectionId = sectionPayload.id; return [3 /*break*/, 12]; case 10: return [4 /*yield*/, this.sectionRepository.create({ courseId: courseId, title: sectionPayload.title, description: sectionPayload.description || "", orderIndex: sectionPayload.orderIndex, createdAt: new Date(), updatedAt: new Date(), })]; case 11: newSection = _e.sent(); currentSectionId = newSection.id; _e.label = 12; case 12: return [4 /*yield*/, this.updateLecturesForSection(currentSectionId, sectionPayload.lectures)]; case 13: _e.sent(); return [4 /*yield*/, this.updateQuizForSection(currentSectionId, sectionPayload.quiz || null)]; case 14: _e.sent(); _e.label = 15; case 15: _b++; return [3 /*break*/, 8]; case 16: sectionsToDelete = existingSections.filter(function (sec) { return !incomingSectionIds.has(sec.id); }); _d = 0, sectionsToDelete_1 = sectionsToDelete; _e.label = 17; case 17: if (!(_d < sectionsToDelete_1.length)) return [3 /*break*/, 20]; sectionToDelete = sectionsToDelete_1[_d]; return [4 /*yield*/, this.sectionRepository.delete(sectionToDelete.id)]; case 18: _e.sent(); _e.label = 19; case 19: _d++; return [3 /*break*/, 17]; case 20: return [2 /*return*/, this.getCourseDetail(courseId)]; } }); }); }; CourseService.prototype.updateLecturesForSection = function (sectionId, lectures) { return __awaiter(this, void 0, void 0, function () { var existingLectures, existingLectureIds, incomingLectureIds, _i, lectures_1, lecturePayload, lecturesToDelete, _a, lecturesToDelete_1, lectureToDelete; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.lectureRepository.findBySectionId(sectionId)]; case 1: existingLectures = _b.sent(); existingLectureIds = new Set(existingLectures.map(function (lec) { return lec.id; })); incomingLectureIds = new Set(lectures.map(function (lec) { return lec.id; }).filter(Boolean)); _i = 0, lectures_1 = lectures; _b.label = 2; case 2: if (!(_i < lectures_1.length)) return [3 /*break*/, 7]; lecturePayload = lectures_1[_i]; lecturePayload.orderIndex; // Assuming orderIndex is provided correctly from frontend based on array order if (!(lecturePayload.id && existingLectureIds.has(lecturePayload.id))) return [3 /*break*/, 4]; return [4 /*yield*/, this.lectureRepository.update(lecturePayload.id, { title: lecturePayload.title, content: lecturePayload.content, type: lecturePayload.type, orderIndex: lecturePayload.orderIndex, updatedAt: new Date(), })]; case 3: _b.sent(); return [3 /*break*/, 6]; case 4: return [4 /*yield*/, this.lectureRepository.create({ sectionId: sectionId, title: lecturePayload.title, content: lecturePayload.content, type: lecturePayload.type, orderIndex: lecturePayload.orderIndex, createdAt: new Date(), updatedAt: new Date(), })]; case 5: _b.sent(); _b.label = 6; case 6: _i++; return [3 /*break*/, 2]; case 7: lecturesToDelete = existingLectures.filter(function (l) { return !incomingLectureIds.has(l.id); }); _a = 0, lecturesToDelete_1 = lecturesToDelete; _b.label = 8; case 8: if (!(_a < lecturesToDelete_1.length)) return [3 /*break*/, 11]; lectureToDelete = lecturesToDelete_1[_a]; return [4 /*yield*/, this.lectureRepository.delete(lectureToDelete.id)]; case 9: _b.sent(); _b.label = 10; case 10: _a++; return [3 /*break*/, 8]; case 11: return [2 /*return*/]; } }); }); }; CourseService.prototype.updateQuizForSection = function (sectionId, quizPayload) { return __awaiter(this, void 0, void 0, function () { var existingQuiz, currentQuizId, newQuiz; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.quizRepository.findBySectionId(sectionId)]; case 1: existingQuiz = _a.sent(); if (!quizPayload) return [3 /*break*/, 7]; currentQuizId = void 0; if (!(existingQuiz && quizPayload.id === existingQuiz.id)) return [3 /*break*/, 3]; // update return [4 /*yield*/, this.quizRepository.update(existingQuiz.id, { title: quizPayload.title, description: quizPayload.description, updatedAt: new Date(), })]; case 2: // update _a.sent(); currentQuizId = existingQuiz.id; return [3 /*break*/, 5]; case 3: return [4 /*yield*/, this.quizRepository.create({ sectionId: sectionId, title: quizPayload.title, description: quizPayload.description, createdAt: new Date(), updatedAt: new Date(), })]; case 4: newQuiz = _a.sent(); currentQuizId = newQuiz.id; _a.label = 5; case 5: return [4 /*yield*/, this.updateQuestionsForQuiz(currentQuizId, quizPayload.questions)]; case 6: _a.sent(); return [3 /*break*/, 9]; case 7: if (!existingQuiz) return [3 /*break*/, 9]; return [4 /*yield*/, this.quizRepository.delete(existingQuiz.id)]; case 8: _a.sent(); _a.label = 9; case 9: return [2 /*return*/]; } }); }); }; CourseService.prototype.updateQuestionsForQuiz = function (quizId, questionPayloads) { return __awaiter(this, void 0, void 0, function () { var existingQuestions, existingQuestionIds, incomingQuestionIds, _i, questionPayloads_1, questionPayload, questionsToDelete, _a, questionsToDelete_1, questionToDelete; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.quizQuestionRepository.findByQuizId(quizId)]; case 1: existingQuestions = _b.sent(); existingQuestionIds = new Set(existingQuestions.map(function (q) { return q.id; })); incomingQuestionIds = new Set(questionPayloads.map(function (q) { return q.id; }).filter(Boolean)); _i = 0, questionPayloads_1 = questionPayloads; _b.label = 2; case 2: if (!(_i < questionPayloads_1.length)) return [3 /*break*/, 7]; questionPayload = questionPayloads_1[_i]; if (!(questionPayload.id && existingQuestionIds.has(questionPayload.id))) return [3 /*break*/, 4]; // Update Existing Question return [4 /*yield*/, this.quizQuestionRepository.update(questionPayload.id, { question: questionPayload.question, // Handle options storage (e.g., stringify JSON array) options: questionPayload.options || [], answer: questionPayload.correctAnswer, explanation: questionPayload.explanation || "", updatedAt: new Date(), })]; case 3: // Update Existing Question _b.sent(); return [3 /*break*/, 6]; case 4: // Create New Question return [4 /*yield*/, this.quizQuestionRepository.create({ quizId: quizId, question: questionPayload.question, options: questionPayload.options || [], answer: questionPayload.correctAnswer, explanation: questionPayload.explanation || "", createdAt: new Date(), updatedAt: new Date(), })]; case 5: // Create New Question _b.sent(); _b.label = 6; case 6: _i++; return [3 /*break*/, 2]; case 7: questionsToDelete = existingQuestions.filter(function (q) { return !incomingQuestionIds.has(q.id); }); _a = 0, questionsToDelete_1 = questionsToDelete; _b.label = 8; case 8: if (!(_a < questionsToDelete_1.length)) return [3 /*break*/, 11]; questionToDelete = questionsToDelete_1[_a]; return [4 /*yield*/, this.quizQuestionRepository.delete(questionToDelete.id)]; case 9: _b.sent(); _b.label = 10; case 10: _a++; return [3 /*break*/, 8]; case 11: return [2 /*return*/]; } }); }); }; /** * Delete a course */ CourseService.prototype.deleteCourse = function (courseId) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.courseRepository.delete(courseId)]; }); }); }; /** * Get public courses */ CourseService.prototype.getPublicCourses = function () { return __awaiter(this, void 0, void 0, function () { var courses, allCategories, categoryMap, enhancedCourses, _i, courses_2, course, sections, lectureCount, _a, sections_3, section, lectures, parsedCategories, categoryNames; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.courseRepository.findPublicCourses()]; case 1: courses = _b.sent(); return [4 /*yield*/, this.categoryRepository.findAll()]; case 2: allCategories = _b.sent(); categoryMap = new Map(allCategories.map(function (cat) { return [cat.id, cat]; })); enhancedCourses = []; _i = 0, courses_2 = courses; _b.label = 3; case 3: if (!(_i < courses_2.length)) return [3 /*break*/, 10]; course = courses_2[_i]; return [4 /*yield*/, this.sectionRepository.findByCourseId(course.id)]; case 4: sections = _b.sent(); lectureCount = 0; _a = 0, sections_3 = sections; _b.label = 5; case 5: if (!(_a < sections_3.length)) return [3 /*break*/, 8]; section = sections_3[_a]; return [4 /*yield*/, this.lectureRepository.findBySectionId(section.id)]; case 6: lectures = _b.sent(); lectureCount += lectures.length; _b.label = 7; case 7: _a++; return [3 /*break*/, 5]; case 8: parsedCategories = this.parseCategories(course.categories); categoryNames = parsedCategories .map(function (catId) { var _a; return (_a = categoryMap.get(catId)) === null || _a === void 0 ? void 0 : _a.name; }) .filter(Boolean); enhancedCourses.push(__assign(__assign({}, course), { categories: parsedCategories, sectionCount: sections.length, lectureCount: lectureCount, categoryNames: categoryNames })); _b.label = 9; case 9: _i++; return [3 /*break*/, 3]; case 10: return [2 /*return*/, enhancedCourses]; } }); }); }; /** * Get courses by teacher ID */ CourseService.prototype.getCoursesByTeacher = function (teacherId) { return __awaiter(this, void 0, void 0, function () { var courses, allCategories, categoryMap, enhancedCourses, _i, courses_3, course, sections, lectureCount, _a, sections_4, section, lectures, parsedCategories, categoryNames; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.courseRepository.findByTeacherId(teacherId)]; case 1: courses = _b.sent(); return [4 /*yield*/, this.categoryRepository.findAll()]; case 2: allCategories = _b.sent(); categoryMap = new Map(allCategories.map(function (cat) { return [cat.id, cat]; })); enhancedCourses = []; _i = 0, courses_3 = courses; _b.label = 3; case 3: if (!(_i < courses_3.length)) return [3 /*break*/, 10]; course = courses_3[_i]; return [4 /*yield*/, this.sectionRepository.findByCourseId(course.id)]; case 4: sections = _b.sent(); lectureCount = 0; _a = 0, sections_4 = sections; _b.label = 5; case 5: if (!(_a < sections_4.length)) return [3 /*break*/, 8]; section = sections_4[_a]; return [4 /*yield*/, this.lectureRepository.findBySectionId(section.id)]; case 6: lectures = _b.sent(); lectureCount += lectures.length; _b.label = 7; case 7: _a++; return [3 /*break*/, 5]; case 8: parsedCategories = this.parseCategories(course.categories); categoryNames = parsedCategories .map(function (catId) { var _a; return (_a = categoryMap.get(catId)) === null || _a === void 0 ? void 0 : _a.name; }) .filter(Boolean); enhancedCourses.push(__assign(__assign({}, course), { categories: parsedCategories, sectionCount: sections.length, lectureCount: lectureCount, categoryNames: categoryNames })); _b.label = 9; case 9: _i++; return [3 /*break*/, 3]; case 10: return [2 /*return*/, enhancedCourses]; } }); }); }; /** * Get courses by category */ CourseService.prototype.getCoursesByCategory = function (categoryId) { return __awaiter(this, void 0, void 0, function () { var courses, category, enhancedCourses, _i, courses_4, course, sections, lectureCount, _a, sections_5, section, lectures, parsedCategories; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.courseRepository.findByCategory(categoryId)]; case 1: courses = _b.sent(); return [4 /*yield*/, this.categoryRepository.findById(categoryId)]; case 2: category = _b.sent(); if (!category) { throw new Error("Category with ID ".concat(categoryId, " not found")); } enhancedCourses = []; _i = 0, courses_4 = courses; _b.label = 3; case 3: if (!(_i < courses_4.length)) return [3 /*break*/, 10]; course = courses_4[_i]; return [4 /*yield*/, this.sectionRepository.findByCourseId(course.id)]; case 4: sections = _b.sent(); lectureCount = 0; _a = 0, sections_5 = sections; _b.label = 5; case 5: if (!(_a < sections_5.length)) return [3 /*break*/, 8]; section = sections_5[_a]; return [4 /*yield*/, this.lectureRepository.findBySectionId(section.id)]; case 6: lectures = _b.sent(); lectureCount += lectures.length; _b.label = 7; case 7: _a++; return [3 /*break*/, 5]; case 8: parsedCategories = this.parseCategories(course.categories); enhancedCourses.push(__assign(__assign({}, course), { categories: parsedCategories, sectionCount: sections.length, lectureCount: lectureCount, categoryNames: [category.name] })); _b.label = 9; case 9: _i++; return [3 /*break*/, 3]; case 10: return [2 /*return*/, enhancedCourses]; } }); }); }; /** * Fetch a course with all its sections and lectures */ CourseService.prototype.getCourseDetails = function (courseId) { return __awaiter(this, void 0, void 0, function () { var course, sections, lectures, _i, sections_6, section, sectionLectures, error_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 7, , 8]); return [4 /*yield*/, this.courseRepository.findById(courseId)]; case 1: course = _a.sent(); if (!course) return [2 /*return*/, null]; return [4 /*yield*/, this.sectionRepository.findByCourseId(courseId)]; case 2: sections = _a.sent(); lectures = []; _i = 0, sections_6 = sections; _a.label = 3; case 3: if (!(_i < sections_6.length)) return [3 /*break*/, 6]; section = sections_6[_i]; return [4 /*yield*/, this.lectureRepository.findBySectionId(section.id)]; case 4: sectionLectures = _a.sent(); lectures = __spreadArray(__spreadArray([], lectures, true), sectionLectures, true); _a.label = 5; case 5: _i++; return [3 /*break*/, 3]; case 6: return [2 /*return*/, { course: course, sections: sections, lectures: lectures, totalLectures: lectures.length }]; case 7: error_1 = _a.sent(); console.error("Error fetching course details:", error_1); throw error_1; case 8: return [2 /*return*/]; } }); }); }; /** * Fetch all enrolled courses for a student with progress information */ CourseService.prototype.getEnrolledCoursesWithProgress = function (studentId) { return __awaiter(this, void 0, void 0, function () { var enrollments, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, this.enrollmentRepository.findAllByStudentId(studentId)]; case 1: enrollments = _a.sent(); return [2 /*return*/, enrollments]; case 2: error_2 = _a.sent(); console.error("Error fetching enrolled courses:", error_2); throw error_2; case 3: return [2 /*return*/]; } }); }); }; /** * Mark a lecture as completed and update course progress */ CourseService.prototype.markLectureCompleted = function (studentId, courseId, lectureId) { return __awaiter(this, void 0, void 0, function () { var enrollment, completedLectures, totalLectures, progressPercentage, isCompleted, updatedEnrollment, error_3; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 6, , 7]); return [4 /*yield*/, this.enrollmentRepository.findByStudentAndCourse(studentId, courseId)]; case 1: enrollment = _a.sent(); if (!enrollment) { throw new Error("Student is not enrolled in this course"); } // Mark the lecture as completed return [4 /*yield*/, this.enrollmentRepository.markLectureCompleted(studentId, courseId, lectureId)]; case 2: // Mark the lecture as completed _a.sent(); return [4 /*yield*/, this.enrollmentRepository.getCompletedLectures(studentId, courseId)]; case 3: completedLectures = _a.sent(); return [4 /*yield*/, this.getTotalLectureCount(courseId)]; case 4: totalLectures = (_a.sent()).totalLectures; progressPercentage = totalLectures > 0 ? Math.round((completedLectures.length / totalLectures) * 100) : 0; isCompleted = progressPercentage >= 100; return [4 /*yield*/, this.enrollmentRepository.updateProgress(studentId, courseId, progressPercentage)]; case 5: updatedEnrollment = _a.sent(); return [2 /*return*/, { success: true, progress: progressPercentage, isCompleted: isCompleted }]; case 6: error_3 = _a.sent(); console.error("Error marking lecture as completed:", error_3); t