Skip to content

Commit

Permalink
Merge pull request #483 from nishant0708/Duplicate
Browse files Browse the repository at this point in the history
Change in user dashboard #482
  • Loading branch information
usha-madithati authored Jul 31, 2024
2 parents 42b07c0 + 1567350 commit e3b879d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
29 changes: 18 additions & 11 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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!",
Expand Down
2 changes: 1 addition & 1 deletion src/components/PForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: ${
Expand Down

0 comments on commit e3b879d

Please sign in to comment.