HS code validation added

This commit is contained in:
unknown 2025-11-19 16:24:06 +05:30
parent 6ee57b5e9c
commit a37d59b826

View File

@ -977,13 +977,28 @@ exports.establishmentBulkUpload = async (req, res) => {
emirateMap[e.name.trim().toLowerCase()] = e.id;
});
const products = await Product.findAll({ attributes: ["id", "hs_code"] });
const productMap = {};
products.forEach(p => {
const normalized = p.hs_code.replace(/[^0-9]/g, "").replace(/^0+/, "");
productMap[normalized] = p.id;
});
function normalizeHS(val) {
if (!val) return "";
return val
.toString()
.normalize("NFKD")
.replace(/[^\d]/g, "")
.trim();
}
const products = await Product.findAll({ attributes: ["id", "hs_code"] });
const productMap = {};
products.forEach(p => {
const cleaned = normalizeHS(p.hs_code);
// Only store valid HS codes 110 digits
if (cleaned.length >= 1 && cleaned.length <= 10) {
productMap[cleaned] = p.id;
}
});
const existingEsts = await Establishment.findAll({
attributes: ["establishment_code", "factory_name", "establishment_contact_email"]
});
@ -1121,22 +1136,26 @@ exports.establishmentBulkUpload = async (req, res) => {
// DB-level HS Code validation
const invalid = [];
for (const raw of hsRaw) {
const cleaned = raw.replace(/\D/g, ""); // remove non-numeric fast
for (const raw of hsRaw) {
const cleaned = normalizeHS(raw);
// Check 10-digit requirement + DB existence
if (cleaned.length !== 10 || !productMap[cleaned]) {
invalid.push(raw); // always push RAW value
if (cleaned.length < 1 || cleaned.length > 10) {
invalid.push(raw);
continue;
}
if (!productMap[cleaned]) {
invalid.push(raw);
}
}
}
if (invalid.length > 0) {
errors.push({
row: rowNum,
error: `HS Code not found in database: ${invalid.join(", ")}`
});
continue;
}
if (invalid.length > 0) {
errors.push({
row: rowNum,
error: `HS Code not found in database: ${invalid.join(", ")}`
});
continue;
}
prepared.push({
est,