Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
722 changes: 381 additions & 341 deletions BackEnd/controller/course/courseController.js

Large diffs are not rendered by default.

343 changes: 184 additions & 159 deletions BackEnd/controller/trainee/traineeController.js
Original file line number Diff line number Diff line change
@@ -1,175 +1,200 @@
const Instructor = require('../../models/InstructorSchema')
const Course = require('../../models/course/courseSchema')
const Trainee = require('../../models/traineeSchema')
const CourseSectionProgress = require('../../models/course/courseProgress/courseSectionProgress')
const Instructor = require("../../models/InstructorSchema");
const Course = require("../../models/course/courseSchema");
const Trainee = require("../../models/traineeSchema");
const CourseSectionProgress = require("../../models/course/courseProgress/courseSectionProgress");
const Payment = require("../../models/lib/payment/paymentSchema");

const getTraineebyId = async (req, res) => {
const _id = req.query
const _id = req.query;

const trainee = await Trainee.findById(_id)
const trainee = await Trainee.findById(_id);

if (!trainee) {
return res.status(404).json({
error: 'user not found'
})
}
if (!trainee) {
return res.status(404).json({
error: "user not found",
});
}

return res.status(200).json(trainee)
}
return res.status(200).json(trainee);
};

const joinCourse = async (req, res) => {
const {
_id,
course_id,
} = req.body

try {
const trainee = await Trainee.joinCourse(_id,course_id)
res.status(200).json({
trainee,
})
} catch (error) {
res.status(400).json({
error: error.message
})
}
}
const { _id, course_id } = req.body;

try {
const trainee = await Trainee.joinCourse(_id, course_id);
res.status(200).json({
trainee,
});
} catch (error) {
res.status(400).json({
error: error.message,
});
}
};

const rateCourse = async (req, res) => {
const {
user_id,
course_id,
rating
} = req.body

try {
if(!user_id)
throw Error('userID not Entered')
const course = await Course.rateCourse(user_id,course_id, rating)

res.status(200).json({
course,
})
} catch (error) {
res.status(400).json({
error: error.message
})
}
}
const { user_id, course_id, rating } = req.body;

try {
if (!user_id) throw Error("userID not Entered");
const course = await Course.rateCourse(user_id, course_id, rating);

res.status(200).json({
course,
});
} catch (error) {
res.status(400).json({
error: error.message,
});
}
};

const reviewInstructor = async (req, res) => {
const {
_id,
instructor_id,
type,
reviewString
} = req.body

try {
const review = await Trainee.reviewInstructor(_id,instructor_id, type, reviewString)

res.status(200).json({
review,
})
} catch (error) {
res.status(400).json({
error: error.message
})
}
}
const getPreview = async (req,res) => {
const {courseId} = req.body

try{
const previewURl = await Course.find({_id:courseId}).select({coursePreviewUrl:1 , _id:0})
res.status(200).json({
previewURl
})
}catch(error){
console.log(error)
res.status(400).json({
error: error.message
})
}
}
const requestRefund = async (req,res) => {
const {
_id,
course_id
} = req.body

try{
if(!_id)
throw Error('userId not Entered')
if(!course_id)
throw Error('courseId not Entered')

const course = await Course.findOne({
_id: course_id
})
var trainee = await Trainee.findOne({
_id
})
const sectionProgress = await CourseSectionProgress.findOne({
user_id:_id,
course_id:course_id
})
if (!course)
throw Error('Course Does not Exist')
if (!trainee)
throw Error('Trainee Does not Exist')
if (!sectionProgress)
throw Error('Section Progress Does not Exist')
const percentage = sectionProgress.finishedPercentage;

var ownedCourses = trainee.ownedCourses

for (let i = 0; i < ownedCourses.length; i++) {
console.log(ownedCourses[i].course_id)
if(ownedCourses[i].course_id == course_id)
{
ownedCourses.splice(i, 1);

}
}
trainee = await Trainee.findByIdAndUpdate({
_id
},{
ownedCourses
})

await CourseSectionProgress.deleteOne({
user_id:_id,
course_id:course_id
})

if(percentage < 50)
const { _id, instructor_id, type, reviewString } = req.body;

try {
const review = await Trainee.reviewInstructor(
_id,
instructor_id,
type,
reviewString
);

res.status(200).json({
review,
});
} catch (error) {
res.status(400).json({
error: error.message,
});
}
};
const getPreview = async (req, res) => {
const { courseId } = req.body;

try {
const previewURl = await Course.find({ _id: courseId }).select({
coursePreviewUrl: 1,
_id: 0,
});
res.status(200).json({
previewURl,
});
} catch (error) {
console.log(error);
res.status(400).json({
error: error.message,
});
}
};
const requestRefund = async (req, res) => {
const { payment_id } = req.body;

try {
if (!payment_id) throw Error("payment_id not Entered");
const payment = await Payment.findOne({
_id: payment_id,
});

const course = await Course.findOne({
_id: payment.course_id,
});
var trainee = await Trainee.findOne({
_id: payment.user_id,
});
const instructor = await Instructor.findById({
_id: course.instructor_id,
});
let sectionProgress = await CourseSectionProgress.findOne({
user_id: payment.user_id,
course_id: payment.course_id,
});
if (!sectionProgress)
sectionProgress = await CourseSectionProgress.create({
user_id: payment.user_id,
course_id: payment.course_id,
sectionTitle: "Introduction",
});
if (!course) throw Error("Course Does not Exist");
if (!trainee) throw Error("Trainee Does not Exist");

const percentage = sectionProgress.finishedPercentage;
if (percentage > 50) {
res.status(200).json({
message:
"Sorry you can't refund this course you Exceeded 50% of the materials",
});
} else {
let ownedCourses = trainee.ownedCourses;
ownedCourses = ownedCourses.filter(
(course) => course.course_id.toString() != payment.course_id
);
await Instructor.findByIdAndUpdate(
{
res.status(200).json({
ownedCourses
})
_id: course.instructor_id,
},
{
credit: instructor.credit - course.price,
}
else{
res.status(200).json({
message: "Sorry you can't refund this course you Exceeded 50% of the materials"
})
);
trainee = await Trainee.findByIdAndUpdate(
{
_id: payment.user_id,
},
{
credit: trainee.credit + course.price,
ownedCourses: ownedCourses,
}



}catch(error){
console.log(error)
res.status(400).json({
error: error.message
})
);
paymentUpdate = await Payment.findByIdAndUpdate(
{
_id: payment_id,
},
{
status: "Refunded",
}
);

await CourseSectionProgress.deleteOne({
user_id: payment.user_id,
course_id: payment.course_id,
});
res.status(200).json({
trainee,
paymentUpdate,
instructor,
});
}
}
} catch (error) {
console.log(error);
res.status(400).json({
error: error.message,
});
}
};

const getPaymentById = async (req, res) => {
const { _id } = req.query;
try {
const payments = await Payment.find({ user_id: _id });
res.status(200).json({
payments,
});
} catch (error) {
console.log(error);
res.status(400).json({
error: error.message,
});
}
};

module.exports = {
joinCourse,
rateCourse,
reviewInstructor,
getTraineebyId,
getPreview,
requestRefund,

}
joinCourse,
rateCourse,
reviewInstructor,
getTraineebyId,
getPreview,
requestRefund,
getPaymentById,
};
Loading