Dup[licate HS code bug fixed
This commit is contained in:
parent
f20b7ef064
commit
83aba0a124
@ -10,20 +10,42 @@ const UnitMaster = db.UnitMaster;
|
|||||||
|
|
||||||
exports.createProduct = async (req, res) => {
|
exports.createProduct = async (req, res) => {
|
||||||
try {
|
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."});
|
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."});
|
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."});
|
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;
|
req.body.created_by = req.user.id;
|
||||||
const data = await Product.create(req.body);
|
const data = await Product.create(req.body);
|
||||||
res.status(201).send({'status':"success",'message':"created successfully",'data': data });
|
res.status(201).send({'status':"success",'message':"created successfully",'data': data });
|
||||||
|
|||||||
@ -8,10 +8,12 @@ module.exports = (sequelize, DataTypes) => {
|
|||||||
product_name: {
|
product_name: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
|
unique: true,
|
||||||
},
|
},
|
||||||
hs_code: {
|
hs_code: {
|
||||||
type: DataTypes.INTEGER,
|
type: DataTypes.INTEGER,
|
||||||
allowNull: true,
|
allowNull: true,
|
||||||
|
unique: true,
|
||||||
},
|
},
|
||||||
hs_description: {
|
hs_description: {
|
||||||
type: DataTypes.TEXT,
|
type: DataTypes.TEXT,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user