Dup[licate HS code bug fixed

This commit is contained in:
unknown 2025-11-22 12:07:17 +05:30
parent f20b7ef064
commit 83aba0a124
2 changed files with 28 additions and 4 deletions

View File

@ -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, 110 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 });

View File

@ -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,