From 96d5445dd5bffeca94223edd1fab7d43851e8b1d Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Tue, 30 Jul 2024 06:55:56 +0530 Subject: [PATCH 1/2] Fixed --- backend/index.js | 29 ++++++++++++++++++----------- src/components/Announcement.js | 2 +- src/components/PForm.js | 2 +- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/backend/index.js b/backend/index.js index b1daba1..73f1ede 100644 --- a/backend/index.js +++ b/backend/index.js @@ -229,7 +229,7 @@ app.get("/products/:barcode", async (req, res) => { }); app.post("/add-product", authenticateUser, async (req, res) => { - const { product_name, barcode, mfd, expiry_date, product_info ,notificationPeriod } = req.body; + const { product_name, barcode, mfd, expiry_date, product_info, notificationPeriod } = req.body; const userId = req.user.userId; if (!product_name || !barcode || !mfd || !expiry_date || !product_info || !notificationPeriod) { @@ -250,17 +250,24 @@ app.post("/add-product", authenticateUser, async (req, res) => { .send({ message: "Expiry date must be after the manufacturing date." }); } - const newProduct = new Product({ - product_name, - barcode, - mfd: new Date(mfd), - expiry_date: new Date(expiry_date), - product_info, - addedBy: userId, - notificationPeriod, - }); - try { + // Check if a product with the same barcode exists + const existingProduct = await Product.findOne({ barcode }); + + if (existingProduct) { + return res.status(409).send({ message: "A product with this barcode already exists." }); + } + + const newProduct = new Product({ + product_name, + barcode, + mfd: new Date(mfd), + expiry_date: new Date(expiry_date), + product_info, + addedBy: userId, + notificationPeriod, + }); + await newProduct.save(); res.status(201).send({ message: "Product added successfully!", diff --git a/src/components/Announcement.js b/src/components/Announcement.js index 3082565..6fd42ff 100644 --- a/src/components/Announcement.js +++ b/src/components/Announcement.js @@ -24,7 +24,7 @@ const Announcement = () => { try { const response = await axios.post( - "https://smartserver-scbe.onrender.com/announcements", + "http://localhost:6352/announcements", announcement ); diff --git a/src/components/PForm.js b/src/components/PForm.js index f0c2d64..1703099 100644 --- a/src/components/PForm.js +++ b/src/components/PForm.js @@ -134,7 +134,7 @@ const PForm = () => { } catch (error) { if (error.response) { if (error.response.status === 409) { - toast.error("Duplicate product. Please check and try again."); + toast.error("Product with same barcode Exist. Please check and try again."); } else { toast.error( `Error occurred: ${ From 1567350e88f1f7489f87a467b35dac6e3791144f Mon Sep 17 00:00:00 2001 From: Nishant Kaushal <101548649+nishant0708@users.noreply.github.com> Date: Tue, 30 Jul 2024 06:59:08 +0530 Subject: [PATCH 2/2] Fixed --- src/components/Announcement.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Announcement.js b/src/components/Announcement.js index 6fd42ff..3082565 100644 --- a/src/components/Announcement.js +++ b/src/components/Announcement.js @@ -24,7 +24,7 @@ const Announcement = () => { try { const response = await axios.post( - "http://localhost:6352/announcements", + "https://smartserver-scbe.onrender.com/announcements", announcement );