From 9e0028247e8ec7f2f60220a3ead8ff5f83bc7b08 Mon Sep 17 00:00:00 2001 From: Ken Date: Tue, 20 Jul 2021 23:30:17 +0700 Subject: [PATCH] Final fix --- controllers/productController.js | 29 +++++++++++++++++++++++++---- routes/productRoutes.js | 1 + server.js | 2 +- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/controllers/productController.js b/controllers/productController.js index d182578..818c5fd 100644 --- a/controllers/productController.js +++ b/controllers/productController.js @@ -174,7 +174,6 @@ exports.getItemBySellerId = async function getItemBySellerId(req, res) { const { id } = await req.params; // let { limit, page } = req.query; - console.log({ id, obj: ObjectId(id) }); // if (userId !== id) res.send({ error: "Id request is not match 🔒" }); const data = await Order.find({ "products.product.seller._id": ObjectId(id), @@ -639,10 +638,15 @@ exports.editStatusItem = async function editStatusItem(req, res) { const user = await User.findById(decodedUser.userId); // * get id product via params const { id } = await req.params; + console.log(id); if (!user) throw new Error("Cannot find User ��"); - Item.findByIdAndUpdate(id, { - status: newStatus, - }).then((rs) => { + ItemCart.findOneAndUpdate( + { _id: id }, + { + orderStatus: newStatus, + } + ).then((rs) => { + console.log(rs); res.status(200); res.send({ success: "New status updated 🙂" }); }); @@ -652,4 +656,21 @@ exports.editStatusItem = async function editStatusItem(req, res) { res.send({ error }); } }; +exports.modifyProduct = async function modifyProduct(req, res) { + try { + const { userId } = await req.user; + if (userId) throw new Error("Cannot find User 🙂"); + const { id } = await req.params; + const content = await req.body; + const product = await Product.findOneAndUpdate(id, content); + if (!product) throw new Error("Cannot find product 😢"); + + res.status(200); + res.send({ success: " Update success 🥳" }); + } catch (err) { + console.log(err); + res.status(500); + res.send({ error: err.message }); + } +}; // noice diff --git a/routes/productRoutes.js b/routes/productRoutes.js index 3732185..9f2c84d 100644 --- a/routes/productRoutes.js +++ b/routes/productRoutes.js @@ -71,4 +71,5 @@ router.post("/remove-from-cart/:id", jwtAuth, productController.removeFromCart); router.post("/edit-cart/:id", jwtAuth, productController.editQuantity); router.post("/rating/:id", jwtAuth, productController.editRating); router.post("/status/:id", jwtAuth, productController.editStatusItem); +router.post("/modify/:id", jwtAuth, productController.modifyProduct); module.exports = router; diff --git a/server.js b/server.js index c202825..ae06da0 100644 --- a/server.js +++ b/server.js @@ -15,6 +15,7 @@ mongoose useCreateIndex: true, useFindAndModify: true, useUnifiedTopology: true, + useFindAndModify: false, }) .then((connection) => { if (connection) consola.success("Database connected ✅"); @@ -29,7 +30,6 @@ mongoose console.error("Failed to connect to mongo on startup ", err); } }); - app.listen(port, () => { consola.success(`Server started at port ${port} 🤖`); });