diff --git a/server/controller/colorCtrl.js b/server/controller/colorCtrl.js index a450a87..0f5834a 100644 --- a/server/controller/colorCtrl.js +++ b/server/controller/colorCtrl.js @@ -9,12 +9,12 @@ const createColor = asyncHandler(async (req, res) => { // Get title from request body const { title } = req.body; // Create a new color with the title - const brand = await Color.create({ title }); + const color = await Color.create({ title }); // Return response with success message and created color res.status(201).json({ success: true, message: "Color created successfully", - brand, + color, }); } catch (error) { throw new Error(error); @@ -29,14 +29,14 @@ const updateColor = asyncHandler(async (req, res) => { // Validate the id validateMongoDbId(id); // Find and update the color with the given id - const brand = await Color.findByIdAndUpdate(id, req.body, { + const color = await Color.findByIdAndUpdate(id, req.body, { new: true, }); // Return response with success message and updated color res.status(200).json({ success: true, message: "Color updated successfully", - brand, + color, }); } catch (error) { throw new Error(error); @@ -51,12 +51,12 @@ const deleteColor = asyncHandler(async (req, res) => { // Validate the id validateMongoDbId(id); // Find and delete the color with the given id - const brand = await Color.findByIdAndDelete(id); + const color = await Color.findByIdAndDelete(id); // Return response with success message and deleted color res.status(200).json({ success: true, message: "Color deleted successfully", - brand, + color, }); } catch (error) { throw new Error(error); @@ -71,28 +71,28 @@ const getColor = asyncHandler(async (req, res) => { // Validate the id validateMongoDbId(id); // Find the color with the given id - const brand = await Color.findById(id); + const color = await Color.findById(id); // Return response with success message and fetched color res.status(200).json({ success: true, message: "Color fetched successfully", - brand, + color, }); } catch (error) { throw new Error(error); } }); -// Get all brands +// Get all colors const getAllColors = asyncHandler(async (req, res) => { try { // Find all colors - const brands = await Color.find({}); + const colors = await Color.find({}); // Return response with success message and fetched colors res.status(200).json({ success: true, message: "Colors fetched successfully", - brands, + colors, }); } catch (error) { throw new Error(error); diff --git a/server/controller/userCtrl.js b/server/controller/userCtrl.js index 1ff4e92..a95e1b7 100644 --- a/server/controller/userCtrl.js +++ b/server/controller/userCtrl.js @@ -438,7 +438,7 @@ const resetPassword = asyncHandler(async (req, res) => { // Get wishlist const getWishlist = asyncHandler(async (req, res) => { // Log the user from the request - console.log(req.user); + // console.log(req.user); // Get the user id from the request user const { _id } = req.user; // Validate the MongoDB Id @@ -460,10 +460,6 @@ const getWishlist = asyncHandler(async (req, res) => { // ADD to User Cart const userCart = asyncHandler(async (req, res) => { - // Log the request body - console.log(req.body); - // Log the user id from the request user - console.log(req.user._id); // Get the cart from the request body const { cart } = req.body; // Get the user id from the request user @@ -505,16 +501,12 @@ const userCart = asyncHandler(async (req, res) => { for (let i = 0; i < products.length; i++) { cartTotal = cartTotal + products[i].price * products[i].count; } - // Log the cart total - console.log("cartTotal", cartTotal); // Create a new cart with the products, total and user id let newCart = await new Cart({ products, cartTotal, orderedBy: user._id, }).save(); - // Log the new cart - console.log("new cart ----> ", newCart); // Return a success response res.status(200).json({ status: "success", @@ -714,6 +706,7 @@ const getOrders = asyncHandler(async (req, res) => { // Find the orders associated with the user and populate the product details const UserOrders = await Order.findOne({ orderedBy: _id }) .populate("products.product") + .populate("orderedBy") .exec(); // Return the response with the orders details @@ -727,6 +720,26 @@ const getOrders = asyncHandler(async (req, res) => { } }); +// Get all orders +const getAllOrders = asyncHandler(async (req, res) => { + try { + // Find all the orders and populate the product details + const orders = await Order.find() + .populate("products.product") + .populate("orderedBy") + .exec(); + + // Return the response with the orders details + res.status(200).json({ + status: "success", + message: "All orders fetched successfully", + orders, + }); + } catch (error) { + throw new Error(error); + } +}); + // update order status const updateOrderStatus = asyncHandler(async (req, res) => { // Get the status from the request body @@ -781,4 +794,5 @@ module.exports = { createOrder, getOrders, updateOrderStatus, + getAllOrders, }; diff --git a/server/index.js b/server/index.js index 0803061..5444cb6 100644 --- a/server/index.js +++ b/server/index.js @@ -15,6 +15,7 @@ const bodyParser = require("body-parser"); const { notFound, errorHandler } = require("./middlewares/errorHandler"); const cookieParser = require("cookie-parser"); const morgan = require("morgan"); +const cors = require("cors"); // Initialize the Express app const app = express(); @@ -25,6 +26,7 @@ dbConnect(); // Use middlewares app.use(morgan("dev")); // Log every request to the console using the dev format for Morgan +app.use(cors()); // Allow cross origin resource sharing app.use(bodyParser.json()); // Parse incoming JSON requests app.use(bodyParser.urlencoded({ extended: false })); // Support parsing of x-www-form-urlencoded data app.use(cookieParser()); // Parse cookies attached to incoming client requests diff --git a/server/package-lock.json b/server/package-lock.json index fab4e6c..76181f6 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -5,7 +5,6 @@ "requires": true, "packages": { "": { - "name": "server", "version": "1.0.0", "license": "ISC", "dependencies": { @@ -13,8 +12,10 @@ "body-parser": "^1.20.2", "cloudinary": "^1.36.1", "cookie-parser": "^1.4.6", + "cors": "^2.8.5", "dotenv": "^16.0.3", "express": "^4.18.2", + "express-async-handler": "^1.2.0", "jsonwebtoken": "^9.0.0", "mongoose": "^7.0.3", "morgan": "^1.10.0", @@ -552,6 +553,18 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" }, + "node_modules/cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "dependencies": { + "object-assign": "^4", + "vary": "^1" + }, + "engines": { + "node": ">= 0.10" + } + }, "node_modules/data-uri-to-buffer": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-3.0.1.tgz", @@ -799,6 +812,11 @@ "node": ">= 0.10.0" } }, + "node_modules/express-async-handler": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/express-async-handler/-/express-async-handler-1.2.0.tgz", + "integrity": "sha512-rCSVtPXRmQSW8rmik/AIb2P0op6l7r1fMW538yyvTMltCO4xQEWMmobfrIxN2V1/mVrgxB8Az3reYF6yUZw37w==" + }, "node_modules/express/node_modules/body-parser": { "version": "1.20.1", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", @@ -3128,6 +3146,15 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" }, + "cors": { + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", + "requires": { + "object-assign": "^4", + "vary": "^1" + } + }, "data-uri-to-buffer": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-3.0.1.tgz", @@ -3345,6 +3372,11 @@ } } }, + "express-async-handler": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/express-async-handler/-/express-async-handler-1.2.0.tgz", + "integrity": "sha512-rCSVtPXRmQSW8rmik/AIb2P0op6l7r1fMW538yyvTMltCO4xQEWMmobfrIxN2V1/mVrgxB8Az3reYF6yUZw37w==" + }, "fast-levenshtein": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz", diff --git a/server/package.json b/server/package.json index a37eb4c..33f5102 100644 --- a/server/package.json +++ b/server/package.json @@ -15,8 +15,10 @@ "body-parser": "^1.20.2", "cloudinary": "^1.36.1", "cookie-parser": "^1.4.6", + "cors": "^2.8.5", "dotenv": "^16.0.3", "express": "^4.18.2", + "express-async-handler": "^1.2.0", "jsonwebtoken": "^9.0.0", "mongoose": "^7.0.3", "morgan": "^1.10.0", diff --git a/server/routes/authRoute.js b/server/routes/authRoute.js index 121fdd8..5f61b39 100644 --- a/server/routes/authRoute.js +++ b/server/routes/authRoute.js @@ -24,6 +24,7 @@ const { createOrder, getOrders, updateOrderStatus, + getAllOrders, } = require("../controller/userCtrl"); // Import the authMiddleware and isAdmin middleware functions @@ -57,6 +58,7 @@ router.post("/cart/cash-order", authMiddleware, createOrder); // Define routes for retrieving user data router.get("/all-users", getallUsers); router.get("/get-orders", authMiddleware, getOrders); +router.get("/get-all-orders", authMiddleware, isAdmin, getAllOrders); router.get("/refresh", handleRefreshToken); router.get("/logout", logout); router.get("/wishlist", authMiddleware, getWishlist);