From 83aba0a124e6c916910d7dc3a53ccf0a21a7744e Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 22 Nov 2025 12:07:17 +0530 Subject: [PATCH] Dup[licate HS code bug fixed --- app/controllers/products.controller.js | 30 ++++++++++++++++++++++---- app/models/product.model.js | 2 ++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app/controllers/products.controller.js b/app/controllers/products.controller.js index 1587760..eb14558 100644 --- a/app/controllers/products.controller.js +++ b/app/controllers/products.controller.js @@ -10,20 +10,42 @@ const UnitMaster = db.UnitMaster; exports.createProduct = async (req, res) => { try { - const hsCode = req.body.hs_code; + const { product_name, hs_code } = req.body; - if (!/^\d+$/.test(hsCode)) { + if (!/^\d+$/.test(hs_code)) { return res.status(400).json({error: "Invalid HS Code: Must be numeric only."}); } - if (String(hsCode).length > 10 && String(hsCode).length < 1 && hsCode === 0 ) { + if (String(hs_code).length > 10 && String(hs_code).length < 1 && hs_code === 0 ) { return res.status(400).send({status: "failed", message: "Invalid HS Code: maximum length is 10 digits."}); } - if(hsCode === 0 || String(hsCode) === "0"){ + if(hs_code === 0 || String(hs_code) === "0"){ return res.status(400).send({status: "failed", message: " must be numeric, 1–10 digits, and cannot be 0."}); } + const productNameExists = await Product.findOne({ + where: { product_name, is_active: true } + }); + + if (productNameExists) { + return res.status(400).json({ + status: "failed", + message: `Product name '${product_name}' already exists.` + }); + } + + const hsCodeExists = await Product.findOne({ + where: { hs_code, is_active: true } + }); + + if (hsCodeExists) { + return res.status(400).json({ + status: "failed", + message: `HS Code '${hs_code}' already exists.` + }); + } + req.body.created_by = req.user.id; const data = await Product.create(req.body); res.status(201).send({'status':"success",'message':"created successfully",'data': data }); diff --git a/app/models/product.model.js b/app/models/product.model.js index d139b03..cebbe07 100644 --- a/app/models/product.model.js +++ b/app/models/product.model.js @@ -8,10 +8,12 @@ module.exports = (sequelize, DataTypes) => { product_name: { type: DataTypes.STRING, allowNull: false, + unique: true, }, hs_code: { type: DataTypes.INTEGER, allowNull: true, + unique: true, }, hs_description: { type: DataTypes.TEXT,