Repository navigation
Expand file tree
/
Copy pathcourseProgress.js
More file actions
99 lines (85 loc) · 3.01 KB
/
Copy pathcourseProgress.js
File metadata and controls
99 lines (85 loc) · 3.01 KB
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
const mongoose = require("mongoose")
const Section = require("../models/Section")
const SubSection = require("../models/SubSection")
const CourseProgress = require("../models/CourseProgress")
const Course = require("../models/Course")
exports.updateCourseProgress = async (req, res) => {
const { courseId, subsectionId } = req.body
const userId = req.user.id
try {
// Check if the subsection is valid
const subsection = await SubSection.findById(subsectionId)
if (!subsection) {
return res.status(404).json({ error: "Invalid subsection" })
}
// Find the course progress document for the user and course
let courseProgress = await CourseProgress.findOne({
courseID: courseId,
userId: userId,
})
if (!courseProgress) {
// If course progress doesn't exist, create a new one
return res.status(404).json({
success: false,
message: "Course progress Does Not Exist",
})
} else {
// If course progress exists, check if the subsection is already completed
if (courseProgress.completedVideos.includes(subsectionId)) {
return res.status(400).json({ error: "Subsection already completed" })
}
// Push the subsection into the completedVideos array
courseProgress.completedVideos.push(subsectionId)
}
// Save the updated course progress
await courseProgress.save()
return res.status(200).json({ message: "Course progress updated" })
} catch (error) {
console.error(error)
return res.status(500).json({ error: "Internal server error" })
}
}
// exports.getProgressPercentage = async (req, res) => {
// const { courseId } = req.body
// const userId = req.user.id
// if (!courseId) {
// return res.status(400).json({ error: "Course ID not provided." })
// }
// try {
// // Find the course progress document for the user and course
// let courseProgress = await CourseProgress.findOne({
// courseID: courseId,
// userId: userId,
// })
// .populate({
// path: "courseID",
// populate: {
// path: "courseContent",
// },
// })
// .exec()
// if (!courseProgress) {
// return res
// .status(400)
// .json({ error: "Can not find Course Progress with these IDs." })
// }
// console.log(courseProgress, userId)
// let lectures = 0
// courseProgress.courseID.courseContent?.forEach((sec) => {
// lectures += sec.subSection.length || 0
// })
// let progressPercentage =
// (courseProgress.completedVideos.length / lectures) * 100
// // To make it up to 2 decimal point
// const multiplier = Math.pow(10, 2)
// progressPercentage =
// Math.round(progressPercentage * multiplier) / multiplier
// return res.status(200).json({
// data: progressPercentage,
// message: "Succesfully fetched Course progress",
// })
// } catch (error) {
// console.error(error)
// return res.status(500).json({ error: "Internal server error" })
// }
// }