Repository navigation
Expand file tree
/
Copy pathProfile.js
More file actions
240 lines (222 loc) · 6.42 KB
/
Copy pathProfile.js
File metadata and controls
240 lines (222 loc) · 6.42 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
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
// controllers/Profile.js
const Profile = require("../models/Profile")
const CourseProgress = require("../models/CourseProgress")
const Course = require("../models/Course")
const User = require("../models/User")
const { uploadImageToCloudinary } = require("../utils/imageUploader")
const mongoose = require("mongoose")
const { convertSecondsToDuration } = require("../utils/secToDuration")
// Method for updating a profile
exports.updateProfile = async (req, res) => {
try {
const {
firstName = "",
lastName = "",
dateOfBirth = "",
about = "",
contactNumber = "",
gender = "",
} = req.body
const id = req.user.id
// Find the profile by id
const userDetails = await User.findById(id)
const profile = await Profile.findById(userDetails.additionalDetails)
const user = await User.findByIdAndUpdate(id, {
firstName,
lastName,
})
await user.save()
// Update the profile fields
profile.dateOfBirth = dateOfBirth
profile.about = about
profile.contactNumber = contactNumber
profile.gender = gender
// Save the updated profile
await profile.save()
// Find the updated user details
const updatedUserDetails = await User.findById(id)
.populate("additionalDetails")
.exec()
return res.json({
success: true,
message: "Profile updated successfully",
updatedUserDetails,
})
} catch (error) {
console.log(error)
return res.status(500).json({
success: false,
error: error.message,
})
}
}
exports.deleteAccount = async (req, res) => {
try {
const id = req.user.id
console.log(id)
const user = await User.findById({ _id: id })
if (!user) {
return res.status(404).json({
success: false,
message: "User not found",
})
}
// Delete Assosiated Profile with the User
await Profile.findByIdAndDelete({
_id: new mongoose.Types.ObjectId(user.additionalDetails),
})
for (const courseId of user.courses) {
await Course.findByIdAndUpdate(
courseId,
{ $pull: { studentsEnroled: id } },
{ new: true }
)
}
// Now Delete User
await User.findByIdAndDelete({ _id: id })
res.status(200).json({
success: true,
message: "User deleted successfully",
})
await CourseProgress.deleteMany({ userId: id })
} catch (error) {
console.log(error)
res
.status(500)
.json({ success: false, message: "User Cannot be deleted successfully" })
}
}
exports.getAllUserDetails = async (req, res) => {
try {
const id = req.user.id
const userDetails = await User.findById(id)
.populate("additionalDetails")
.exec()
console.log(userDetails)
res.status(200).json({
success: true,
message: "User Data fetched successfully",
data: userDetails,
})
} catch (error) {
return res.status(500).json({
success: false,
message: error.message,
})
}
}
exports.updateDisplayPicture = async (req, res) => {
try {
const displayPicture = req.files.displayPicture
const userId = req.user.id
const image = await uploadImageToCloudinary(
displayPicture,
process.env.FOLDER_NAME,
1000,
1000
)
console.log(image)
const updatedProfile = await User.findByIdAndUpdate(
{ _id: userId },
{ image: image.secure_url },
{ new: true }
)
res.send({
success: true,
message: `Image Updated successfully`,
data: updatedProfile,
})
} catch (error) {
return res.status(500).json({
success: false,
message: error.message,
})
}
}
exports.getEnrolledCourses = async (req, res) => {
try {
const userId = req.user.id
let userDetails = await User.findOne({
_id: userId,
})
.populate({
path: "courses",
populate: {
path: "courseContent",
populate: {
path: "subSection",
},
},
})
.exec()
userDetails = userDetails.toObject()
var SubsectionLength = 0
for (var i = 0; i < userDetails.courses.length; i++) {
let totalDurationInSeconds = 0
SubsectionLength = 0
for (var j = 0; j < userDetails.courses[i].courseContent.length; j++) {
totalDurationInSeconds += userDetails.courses[i].courseContent[
j
].subSection.reduce((acc, curr) => acc + parseInt(curr.timeDuration), 0)
userDetails.courses[i].totalDuration = convertSecondsToDuration(
totalDurationInSeconds
)
SubsectionLength +=
userDetails.courses[i].courseContent[j].subSection.length
}
let courseProgressCount = await CourseProgress.findOne({
courseID: userDetails.courses[i]._id,
userId: userId,
})
courseProgressCount = courseProgressCount?.completedVideos.length
if (SubsectionLength === 0) {
userDetails.courses[i].progressPercentage = 100
} else {
// To make it up to 2 decimal point
const multiplier = Math.pow(10, 2)
userDetails.courses[i].progressPercentage =
Math.round(
(courseProgressCount / SubsectionLength) * 100 * multiplier
) / multiplier
}
}
if (!userDetails) {
return res.status(400).json({
success: false,
message: `Could not find user with id: ${userDetails}`,
})
}
return res.status(200).json({
success: true,
data: userDetails.courses,
})
} catch (error) {
return res.status(500).json({
success: false,
message: error.message,
})
}
}
exports.instructorDashboard = async (req, res) => {
try {
const courseDetails = await Course.find({ instructor: req.user.id });
const courseData = courseDetails.map((course) => {
const totalStudentsEnrolled = course.studentsEnroled ? course.studentsEnroled.length : 0;
const totalAmountGenerated = totalStudentsEnrolled * course.price;
// Create a new object with the additional fields
const courseDataWithStats = {
_id: course._id,
courseName: course.courseName,
courseDescription: course.courseDescription,
// Include other course properties as needed
totalStudentsEnrolled,
totalAmountGenerated,
};
return courseDataWithStats;
});
res.status(200).json({ courses: courseData });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Server Error" });
}
};