Skip to content

Commit

Permalink
reversed wallet system
Browse files Browse the repository at this point in the history
  • Loading branch information
AVtheking committed Nov 25, 2023
1 parent 8a24e38 commit 12daca7
Showing 1 changed file with 20 additions and 71 deletions.
91 changes: 20 additions & 71 deletions controllers/payment_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,6 @@ const { User, Course } = require("../models");
const { courseIdSchema } = require("../utils/validator");

const paymentCtrl = {
buycourse: async (req, res, next) => {
try {
const user = req.user;
const courseId = req.params.courseId;
const course = await Course.findById(courseId);
if (!course) {
return next(new ErrorHandler(400, "No course found"));
}
const courseIdIndex = user.ownedCourse.findIndex((course) =>
course.courseId.equals(courseId)
);
if (course.createdBy.equals(user._id)) {
return next(new ErrorHandler(400, "You cannot buy your own course"));
}
if (courseIdIndex != -1) {
return next(
new ErrorHandler(402, "You have already enrolled in this course")
);
}
const amount = course.price;
if (user.wallet < amount) {
return next(new ErrorHandler(402, "Insufficient balance"));
}
user.wallet -= amount;
user.ownedCourse.push({ courseId: courseId });
await user.save();
await Course.findByIdAndUpdate(courseId, {
$inc: { totalStudents: 1 },
});
return res.json({
success: true,
message: "Payment successful",
});
} catch (e) {
next(e);
}
},
buyCourseCart: async (req, res, next) => {
try {
const user = req.user;
const amount = req.params.amount;
if (user.cart.length == 0) {
return next(new ErrorHandler(404, "No course in cart"));
}
if (user.wallet < amount) {
return next(new ErrorHandler(402, "Insufficient balance"));
}
for (let i = 0; i < user.cart.length; i++) {
const courseId = user.cart[i];
// const course = await Course.findById(courseId);

user.ownedCourse.push({ courseId: courseId });
await Course.findByIdAndUpdate(courseId, {
$inc: { totalStudents: 1 },
});
}
user.wallet -= amount;
user.cart = [];
await user.save();
res.json({
success: true,
message: "Payment successful",
});
} catch (e) {
next(e);
}
},
createOrderCart: async (req, res, next) => {
try {
const user = req.user;
Expand Down Expand Up @@ -142,8 +75,23 @@ const paymentCtrl = {
},
createOrder: async (req, res, next) => {
try {
// const user = req.user;
const amount = req.params.amount;
const courseid = req.params.courseId;
const result = await courseIdSchema.validateAsync({ params: courseid });
const courseId = result.params;
const course = await Course.findById(courseId);
if (!course) {
return next(new ErrorHandler(400, "No course found"));
}
const user = req.user;
const courseIdIndex = user.ownedCourse.findIndex((course) =>
course.courseId.equals(courseId)
);
if (courseIdIndex != -1) {
return next(
new ErrorHandler(402, "You have already enrolled in this course")
);
}
const amount = course.price;

const razorpayInstance = new Razorpay({
key_id: process.env.KEY_ID,
Expand All @@ -158,7 +106,7 @@ const paymentCtrl = {
};

const order = await razorpayInstance.orders.create(options);

// console.log(order);
return res.json({
success: true,
message: "Order Created",
Expand All @@ -169,6 +117,7 @@ const paymentCtrl = {
order: order,
});
} catch (error) {
// console.log(error);
next(error);
}
},
Expand Down Expand Up @@ -213,4 +162,4 @@ const paymentCtrl = {
}
},
};
module.exports = paymentCtrl;
module.exports = paymentCtrl;

0 comments on commit 12daca7

Please sign in to comment.