Initial Version

This commit is contained in:
Ruslan845
2026-03-10 03:45:00 +09:00
commit 2c4fc7f933
128 changed files with 7617 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
const jwt = require('jsonwebtoken');
const User = require('../../models/userModel');
const ErrorHandler = require('../../utils/errorHandler');
const asyncErrorHandler = require('../helpers/asyncErrorHandler');
exports.isAuthenticatedUser = asyncErrorHandler(async (req, res, next) => {
const { token } = req.cookies;
if (!token) {
return next(new ErrorHandler("Please Login to Access", 401))
}
const decodedData = jwt.verify(token, process.env.JWT_SECRET);
req.user = await User.findById(decodedData.id);
next();
});
exports.authorizeRoles = (...roles) => {
return (req, res, next) => {
if (!roles.includes(req.user.role)) {
return next(new ErrorHandler(`Role: ${req.user.role} is not allowed`, 403));
}
next();
}
}

View File

@@ -0,0 +1,36 @@
const Review = require("../../models/Review")
module.exports = async (product,newStar) => {
// const product = req.product
// if (!product.isVerified && product.isDeleted) {
// return res.status(404).json({ error: 'Product not found' })
// }
let stars = await Review.find({ product: product._id }).select('star');
let fiveStars = 0, fourStars = 0, threeStars = 0, twoStars = 0, oneStars = 0;
stars.forEach(s => {
if (s.star === 5) fiveStars += 1
if (s.star === 4) fourStars += 1
if (s.star === 3) threeStars += 1
if (s.star === 2) twoStars += 1
if (s.star === 1) oneStars += 1
})
//this condition is executed during postReview and editReview
if (newStar === 5) fiveStars += 1
if (newStar === 4) fourStars += 1
if (newStar === 3) threeStars += 1
if (newStar === 2) twoStars += 1
if (newStar === 1) oneStars += 1
let totalRatingUsers = (fiveStars + fourStars + threeStars + twoStars + oneStars)
let averageStar = (5 * fiveStars + 4 * fourStars + 3 * threeStars + 2 * twoStars + oneStars) / totalRatingUsers
return stars = {
fiveStars,
fourStars,
threeStars,
twoStars,
oneStars,
averageStar,
totalRatingUsers
}
}

View File

@@ -0,0 +1,38 @@
const Cart = require("../../models/Cart")
const Review = require("../../models/Review")
const Order = require("../../models/Order")
const Whislist = require("../../models/WishList")
module.exports = async(product,user,type) =>{
let hasOnCart = null
let hasBought = null
let hasOnWishlist = null
let hasReviewed = null
if (user) {
//cart bahek aru ko lagi check gareko
if (type !=='carts') {
//has on cart?
hasOnCart = await Cart.findOne({ user: user._id, product: product._id, isDeleted: null })
if (!hasOnCart) hasOnCart = false
}
//wishlist bahek aru ko lagi check gareko
if (type !=='wishlists') {
// has on wishlist?
hasOnWishlist = await Whislist.findOne({ user: user._id, product: product._id, isDeleted: null })
if (!hasOnWishlist) hasOnWishlist = false
}
if (type==='product') {
//has bought?
hasBought = await Order.findOne({ user: user, $or: [{ 'status.currentStatus': 'complete' }, { 'status.currentStatus': 'tobereturned', 'status.currentStatus': 'return' }] })
hasBought ? hasBought = true : hasBought = false
//has reviewed?
hasReviewed = await Review.findOne({ user: user, product: product._id }).select('comment star user')
if (!hasReviewed) hasReviewed = false
}
}
return {hasBought,hasOnCart,hasOnWishlist,hasReviewed}
}