Skip to content

Commit

Permalink
Final fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken committed Jul 20, 2021
1 parent bbd29b1 commit 9e00282
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
29 changes: 25 additions & 4 deletions controllers/productController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 🙂" });
});
Expand All @@ -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
1 change: 1 addition & 0 deletions routes/productRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mongoose
useCreateIndex: true,
useFindAndModify: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then((connection) => {
if (connection) consola.success("Database connected ✅");
Expand All @@ -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} 🤖`);
});

0 comments on commit 9e00282

Please sign in to comment.