Hs code validation added in the create API
This commit is contained in:
parent
53e25a039f
commit
c31eabd96f
@ -10,11 +10,23 @@ const UnitMaster = db.UnitMaster;
|
||||
|
||||
exports.createProduct = async (req, res) => {
|
||||
try {
|
||||
const hsCode = req.body.hs_code;
|
||||
|
||||
const data = await Product.create(req.body);
|
||||
if (!/^\d+$/.test(hsCode)) {
|
||||
return res.status(400).json({error: "Invalid HS Code: Must be numeric only."});
|
||||
}
|
||||
|
||||
if (String(hsCode).length > 10 && String(hsCode).length < 1 && hsCode === 0 ) {
|
||||
return res.status(400).send({status: "failed", message: "Invalid HS Code: maximum length is 10 digits."});
|
||||
}
|
||||
|
||||
if(hsCode === 0 || String(hsCode) === "0"){
|
||||
return res.status(400).send({status: "failed", message: " must be numeric, 1–10 digits, and cannot be 0."});
|
||||
}
|
||||
|
||||
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 });
|
||||
|
||||
} catch (error) {
|
||||
res.status(500).send({'status':"failed",'message':error.message });
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ module.exports = (sequelize, DataTypes) => {
|
||||
allowNull: false,
|
||||
},
|
||||
hs_code: {
|
||||
type: DataTypes.STRING,
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: true,
|
||||
},
|
||||
hs_description: {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user