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