From c31eabd96f1222c8d8df27e87f5ceaa88b5f8306 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 20 Nov 2025 11:55:40 +0530 Subject: [PATCH] Hs code validation added in the create API --- app/controllers/products.controller.js | 16 ++++++++++++++-- app/models/product.model.js | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/controllers/products.controller.js b/app/controllers/products.controller.js index 9b5805c..8dbcae2 100644 --- a/app/controllers/products.controller.js +++ b/app/controllers/products.controller.js @@ -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 }); } diff --git a/app/models/product.model.js b/app/models/product.model.js index 4275b30..544b3fc 100644 --- a/app/models/product.model.js +++ b/app/models/product.model.js @@ -10,7 +10,7 @@ module.exports = (sequelize, DataTypes) => { allowNull: false, }, hs_code: { - type: DataTypes.STRING, + type: DataTypes.INTEGER, allowNull: true, }, hs_description: {