Merge branch 'master' of bitbucket.org:jubilian/fcsc_ipi_backend

This commit is contained in:
Gowtham M 2025-11-19 14:31:56 +05:30
commit f6c82b253f
2 changed files with 693 additions and 371 deletions

View File

@ -891,387 +891,709 @@ exports.forgotPasswordVerifyOTP = async (req, res) => {
} }
}; };
const GENERIC_ERROR_MSG =
"Invalid data. Please check the instructions given and upload again.";
exports.establishmentBulkUpload = async (req, res) => { exports.establishmentBulkUpload = async (req, res) => {
try { try {
// Basic file & user validations
if (!req.file) { if (!req.file) {
return res.status(400).send({ status: "failed", message: "No file uploaded" }); return res.status(400).send({
status: "failed",
message: "No file uploaded."
});
} }
const filePath = req.file.path; const filePath = req.file.path;
if (!req.file.originalname.toLowerCase().endsWith(".csv")) { if (!req.file.originalname.toLowerCase().endsWith(".csv")) {
if (fs.existsSync(filePath)) fs.unlinkSync(filePath); fs.unlinkSync(filePath);
return res.status(400).send({ status: "failed", message: "Invalid file type. Only CSV allowed." }); return res.status(400).send({
status: "failed",
message: "Invalid file type. Only CSV allowed."
});
} }
if (!req.user?.id || isNaN(req.user.id)) {
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
return res.status(400).send({ status: "failed", message: "Invalid or missing User Id." });
}
const mode = (req.body.mode || "add").toLowerCase();
if (mode !== "add") {
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
return res.status(400).send({ status: "failed", message: "Only 'Add Only' mode is supported currently." });
}
const stats = fs.statSync(filePath);
if (stats.size === 0) {
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
return res.status(400).send({ status: "failed", message: "Uploaded file is empty." });
}
// Read CSV into memory (array of normalized rows)
const rows = []; const rows = [];
// -------------------------------------------------------
// READ CSV AND SKIP EMPTY ROWS
// -------------------------------------------------------
await new Promise((resolve, reject) => {
fs.createReadStream(filePath) fs.createReadStream(filePath)
.pipe(csv()) .pipe(csv())
.on("data", (rawRow) => { .on("data", (rawRow) => {
// Normalize headers and map to canonical keys
const row = {}; const row = {};
// Normalize headers
for (const key in rawRow) { for (const key in rawRow) {
const normalizedKey = key.replace(/[\s\W]+/g, "_").trim().toLowerCase(); const normalizedKey = key.replace(/[\s\W]+/g, "_")
.trim()
.toLowerCase();
// Standard canonical keys row[normalizedKey] = rawRow[key]?.trim() || "";
if (normalizedKey === "establishment_id" || normalizedKey === "establishment_code" || normalizedKey.startsWith("establishment")) {
row["Establishment Id"] = rawRow[key]?.trim() || null;
continue;
} }
if (normalizedKey.includes("factory name") || normalizedKey.includes("factory_name") || normalizedKey.includes("Factory Name")) { const cleanedValues = Object.values(row).map(v =>(v || "").replace(/\s+/g, "").trim());
row["Factory Name"] = rawRow[key]?.trim() || null; const isEmpty = cleanedValues.every(v => v === "");
continue; if (isEmpty) return;
}
if (normalizedKey.includes("Email") || normalizedKey.includes("email") ) {
row["Email"] = rawRow[key]?.trim() || null;
continue;
}
if (normalizedKey.includes("Emirate") || normalizedKey.includes("emirate") ) {
row["Emirate"] = rawRow[key]?.trim() || null;
continue;
}
if (normalizedKey.includes("total employment") || normalizedKey.includes("total_employment") || normalizedKey.includes("total employments")) {
row["Total Employment"] = rawRow[key]?.trim() || null;
continue;
}
// HS code columns: keep any header that contains 'hs' (hs_code, hs_code_1, hs1, etc.)
if (/^hs|hs_code|hs_code_\d|hs\d/i.test(normalizedKey) || normalizedKey.includes("hs")) {
// keep the exact normalized header so we can iterate later
row[normalizedKey] = rawRow[key]?.trim() || null;
continue;
}
// Fallback - keep other columns too
row[normalizedKey] = rawRow[key]?.trim() || null;
}
rows.push(row); rows.push(row);
}) })
.on("end", async () => { .on("end", resolve)
try { .on("error", reject);
if (!rows.length) { });
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
return res.status(400).send({ status: "failed", message: "CSV file is empty or invalid." });
}
// Check required canonical columns exist in the header (based on first row keys) if (rows.length === 0) {
const firstRowKeys = Object.keys(rows[0]); fs.unlinkSync(filePath);
const required = ["Establishment Id", "Factory Name", "Email", "Emirate", "Total Employment"];
const missing = required.filter((k) => !firstRowKeys.includes(k));
if (missing.length > 0) {
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
return res.status(400).send({ return res.status(400).send({
status: "failed", status: "failed",
message: `Missing required columns: ${missing.join(", ")}`, message: "CSV file is empty.",
errors: [{ error: "Uploaded file is empty." }]
});
}
// -------------------------------------------------------
// REQUIRED HEADER CHECK
// -------------------------------------------------------
const required = [
"establishment_id",
"factory_name",
"email",
"emirate",
"total_employment"
];
const firstRowKeys = Object.keys(rows[0]);
const missing = required.filter(r => !firstRowKeys.includes(r));
if (missing.length > 0) {
fs.unlinkSync(filePath);
return res.status(400).send({
status: "failed",
message: "Invalid data. Please check the instructions given and upload again.",
errors: [
{ error: "Missing required columns: " + missing.join(", ") }
]
}); });
} }
// Prepare maps: emirates and products // -------------------------------------------------------
// LOAD REFERENCE TABLES
// -------------------------------------------------------
const emirates = await Emirate.findAll({ attributes: ["id", "name"] }); const emirates = await Emirate.findAll({ attributes: ["id", "name"] });
const emirateMap = {}; const emirateMap = {};
emirates.forEach((e) => { emirates.forEach(e => {
if (e.name) emirateMap[e.name.trim().toLowerCase()] = e.id; emirateMap[e.name.trim().toLowerCase()] = e.id;
}); });
// Build product map by normalized hs_code (digits only, no leading zeros)
const products = await Product.findAll({ attributes: ["id", "hs_code"] }); const products = await Product.findAll({ attributes: ["id", "hs_code"] });
const productMap = {}; const productMap = {};
products.forEach((p) => { products.forEach(p => {
if (!p.hs_code) return; const normalized = p.hs_code.replace(/[^0-9]/g, "").replace(/^0+/, "");
const normalized = String(p.hs_code).replace(/[^0-9]/g, "").replace(/^0+/, ""); productMap[normalized] = p.id;
if (normalized) productMap[normalized] = p.id;
}); });
// Existing establishment codes in DB
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"]
}); });
const existingEstIdSet = new Set(existingEsts.map((e) => e.establishment_code)); const existingEstIdSet = new Set(existingEsts.map(e => e.establishment_code));
const existingFactorySet = new Set(existingEsts.map((e) => e.factory_name?.trim().toLowerCase())); const existingFactorySet = new Set(existingEsts.map(e => e.factory_name.toLowerCase()));
const existingEmailSet = new Set(existingEsts.map((e) => e.establishment_contact_email?.trim().toLowerCase())); const existingEmailSet = new Set(existingEsts.map(e => e.establishment_contact_email.toLowerCase()));
// -------------------------------------------------------
// FILE-LEVEL DUPLICATE TRACKING
// -------------------------------------------------------
const errors = []; const errors = [];
const fileDuplicates = new Set(); const fileEstSet = new Set();
const seenEstIds = new Set(); const fileFactorySet = new Set();
const toInsert = []; const fileEmailSet = new Set();
// Process each row: validate & prepare const prepared = [];
// -------------------------------------------------------
// VALIDATE EACH ROW
// -------------------------------------------------------
for (let i = 0; i < rows.length; i++) { for (let i = 0; i < rows.length; i++) {
const raw = rows[i]; const r = rows[i];
const rowNumber = i + 1; const rowNum = i + 2;
const estId = raw["Establishment Id"]?.trim(); const est = r.establishment_id;
const factory = raw["Factory Name"]?.trim(); const factory = r.factory_name;
const email = raw["Email"]?.trim(); const email = r.email;
const emirateRaw = raw["Emirate"]?.trim(); const emirate = r.emirate;
const employmentRaw = raw["Total Employment"]?.trim(); const employment = r.total_employment;
// Required fields // Required validation
if (!estId || !factory || !email || !emirateRaw) { const missingFields = [];
if (!est) missingFields.push("Establishment ID");
if (!factory) missingFields.push("Factory Name");
if (!email) missingFields.push("Email");
if (!emirate) missingFields.push("Emirate");
if (!employment) missingFields.push("Total Employment");
if (missingFields.length > 0) {
errors.push({ errors.push({
row: rowNumber, row: rowNum,
error: "Missing required fields (Establishment Id, Factory Name, Email, Emirate).", error: `Missing Data: ${missingFields.join(", ")} ${missingFields.length === 1 ? "is" : "are"} required.`
}); });
continue; continue;
} }
//Checking repeating data if (isNaN(employment) || !/^\d+$/.test(employment)) {
const estIdRowMap = {};
const factoryRowMap = {};
const emailRowMap = {};
// During row loop — replace your tracking code with this:
const keyEst = estId?.trim();
const keyFactory = factory?.trim().toLowerCase();
const keyEmail = email?.trim().toLowerCase();
// Helper for inserting row numbers
const pushRow = (map, key) =>
key ? (map[key] = map[key] ? [...map[key], rowNumber] : [rowNumber]) : null;
// Track all 3
pushRow(estIdRowMap, keyEst);
pushRow(factoryRowMap, keyFactory);
pushRow(emailRowMap, keyEmail);
const duplicateDetails = [];
[
["Establishment Id", estIdRowMap],
["Factory Name", factoryRowMap],
["Email", emailRowMap],
].forEach(([field, map]) => {
Object.entries(map).forEach(([value, rows]) => {
if (rows.length > 1) {
duplicateDetails.push({
field,
value,
rows
});
}
});
});
if (duplicateDetails.length > 0) {
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
return res.status(400).send({
status: "failed",
message: "Repeated values found in uploaded file.",
duplicates: duplicateDetails,
});
}
const hsRawCodes = [];
for (const k of Object.keys(raw)) {
if (/^hs|hs_code|hs_code_\d|hs\d/i.test(k) || k.includes("hs")) {
const v = raw[k];
if (v && String(v).trim()) {
hsRawCodes.push(String(v).trim());
}
}
}
// Validation: at least one HS code must be present
if (hsRawCodes.length === 0) {
errors.push({ errors.push({
row: rowNumber, row: rowNum,
error: "Missing HS Code columns or no HS Code values provided.", error: "Total Employment must be numeric."
}); });
continue; continue;
} }
const seenEstIds = new Set(); const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const seenFactories = new Set();
const seenEmails = new Set();
if (seenEstIds.has(keyEst)) { if (!emailRegex.test(email)) {
fileDuplicates.add(`Establishment ID: ${keyEst}`); errors.push({
row: rowNum,
error: "Invalid Email: Please check the Email address."
});
continue; continue;
} }
if (seenFactories.has(keyFactory)) { // FILE-level duplicates
fileDuplicates.add(`Factory Name: ${factory}`); const estKey = est.trim();
const factoryKey = factory.trim().toLowerCase();
const emailKey = email.trim().toLowerCase();
if (fileEstSet.has(estKey)) {
errors.push({ row: rowNum, error: `Duplicate Establishment ID in file: ${est}` });
continue; continue;
} }
if (seenEmails.has(keyEmail)) { if (fileFactorySet.has(factoryKey)) {
fileDuplicates.add(`Email: ${email}`); errors.push({ row: rowNum, error: `Duplicate Factory Name in file: ${factory}` });
continue;
}
if (fileEmailSet.has(emailKey)) {
errors.push({ row: rowNum, error: `Duplicate Email in file: ${email}` });
continue; continue;
} }
seenEstIds.add(keyEst); fileEstSet.add(estKey);
seenFactories.add(keyFactory); fileFactorySet.add(factoryKey);
seenEmails.add(keyEmail); fileEmailSet.add(emailKey);
if (existingEstIdSet.has(keyEst)) { // DB-level duplicates
fileDuplicates.add(`Establishment ID already exists: ${keyEst}`); if (existingEstIdSet.has(estKey)) {
errors.push({ row: rowNum, error: `Establishment ID already exists: ${est}` });
continue; continue;
} }
if (existingFactorySet.has(keyFactory)) { if (existingFactorySet.has(factoryKey)) {
fileDuplicates.add(`Factory Name already exists: ${factory}`); errors.push({ row: rowNum, error: `Factory Name already exists: ${factory}` });
continue; continue;
} }
if (existingEmailSet.has(keyEmail)) { if (existingEmailSet.has(emailKey)) {
fileDuplicates.add(`Email already exists: ${email}`); errors.push({ row: rowNum, error: `Email already exists: ${email}` });
continue; continue;
} }
// Emirate mapping // Emirate validation
const emirateId = emirateMap[emirateRaw.toLowerCase()]; const emirateId = emirateMap[emirate.toLowerCase()];
if (!emirateId) { if (!emirateId) {
errors.push({ row: rowNum, error: `Invalid Emirate: ${emirate}` });
continue;
}
// -------------------------------------------------------
// HS CODE VALIDATION
// -------------------------------------------------------
const hsCols = Object.keys(r).filter(k => k.startsWith("hs"));
const hsRaw = hsCols.map(k => r[k]).filter(Boolean);
if (hsRaw.length === 0) {
errors.push({ row: rowNum, error: "At least one HS Code is required" });
continue;
}
// Clean & remove leading zeros
const cleaned = hsRaw.map(v =>
v.replace(/[^0-9]/g, "").replace(/^0+/, "")
);
// Check duplicates in same row
const rowDuplicates = cleaned.filter((c, idx) => cleaned.indexOf(c) !== idx);
if (rowDuplicates.length > 0) {
errors.push({ errors.push({
row: rowNumber, row: rowNum,
error: `Invalid Emirate: '${emirateRaw}'`, error: `Duplicate HS Code(s) in the same row: ${[...new Set(rowDuplicates)].join(", ")}`
}); });
continue; continue;
} }
// Clean HS codes: remove non-digits and leading zeros // DB-level HS Code validation
const cleanedHs = hsRawCodes const invalid = [];
.map((hc) => hc.replace(/[^0-9]/g, "").replace(/^0+/, ""))
.filter(Boolean); // remove empty after cleaning
const mappedProducts = cleanedHs.map((c) => productMap[c] || null); for (const raw of hsRaw) {
const cleaned = raw.replace(/\D/g, ""); // remove non-numeric fast
// Find which HS codes do NOT match product master // Check 10-digit requirement + DB existence
const notMapped = cleanedHs.filter((c, i) => mappedProducts[i] === null); if (cleaned.length !== 10 || !productMap[cleaned]) {
invalid.push(raw); // always push RAW value
}
}
if (notMapped.length > 0) { if (invalid.length > 0) {
errors.push({ errors.push({
row: rowNumber, row: rowNum,
error: `${notMapped} HS Codes do not exist.`, error: `HS Code not found in database: ${invalid.join(", ")}`
not_mapped: notMapped
}); });
continue; continue;
} }
if (mappedProducts.every((id) => id === null)) { prepared.push({
errors.push({ est,
row: rowNumber,
error: "None of the provided HS Codes match existing products.",
hs_codes: cleanedHs
});
continue;
}
// parse employment
const employment = parseInt(employmentRaw) || 0;
toInsert.push({
estCode: estId,
factory, factory,
email, email,
emirateId, emirateId,
employment, employment: parseInt(employment),
productIds: [...new Set(mappedProducts)], productIds: cleaned.map(c => productMap[c]),
rowIndex: rowNumber, rowNum
}); });
} }
// If file duplicates found (either in file or in DB), return error - consistent with product controller // -------------------------------------------------------
if (fileDuplicates.size > 0) { // STOP IF ERRORS
if (fs.existsSync(filePath)) fs.unlinkSync(filePath); // -------------------------------------------------------
if (errors.length > 0) {
logger.error("Establishment Upload Failed: " + JSON.stringify(errors));
fs.unlinkSync(filePath);
return res.status(400).send({ return res.status(400).send({
status: "failed", status: "failed",
message: "Duplicate Establishment IDs found (in file or already exist in system). Resolve and re-upload.", message: "Invalid data. Please check the instructions given and upload again.",
duplicates: [...fileDuplicates], errors
}); });
} }
// Insert prepared establishments and associated products // -------------------------------------------------------
let inserted = 0; // INSERT VALID RECORDS
for (const rec of toInsert) { // -------------------------------------------------------
try { for (const rec of prepared) {
const createdEst = await Establishment.create({ const est = await Establishment.create({
establishment_code: rec.estCode, establishment_code: rec.est,
factory_name: rec.factory, factory_name: rec.factory,
establishment_contact_email: rec.email, establishment_contact_email: rec.email,
establishment_emirate_id: rec.emirateId, establishment_emirate_id: rec.emirateId,
total_employees: rec.employment, total_employees: rec.employment,
created_by: req.user.id, created_by: req.user.id,
created_at: new Date(), created_at: new Date()
}); });
// prepare bulk create payload for establishment_products await EstablishmentProduct.bulkCreate(
const payload = rec.productIds.map((pid) => ({ rec.productIds.map(pid => ({
establishment_id: createdEst.id, establishment_id: est.id,
product_id: pid, product_id: pid,
created_by: req.user.id, created_by: req.user.id,
created_at: new Date(), created_at: new Date(),
is_active: true, is_active: true
})); }))
);
await EstablishmentProduct.bulkCreate(payload);
inserted++;
} catch (errInner) {
// Collect row-level DB insert errors for partial success reporting
errors.push({
row: rec.rowIndex,
error: `DB insert error: ${errInner.message}`,
});
}
} }
// cleanup uploaded file fs.unlinkSync(filePath);
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
// Build final response to match product import style
let finalStatus = "success";
let message = `${inserted} establishments inserted successfully.`;
if (errors.length > 0) {
finalStatus = inserted > 0 ? "partial_success" : "failed";
if (finalStatus === "partial_success") {
message = `${inserted} establishments inserted, ${errors.length} rows failed.`;
} else {
message = `No establishments imported. ${errors.length} validation errors found.`;
}
}
return res.status(200).send({ return res.status(200).send({
status: finalStatus, status: "success",
message, message: `${prepared.length} establishments inserted successfully.`,
summary: { summary: {
total_records: rows.length, total_records: rows.length,
imported: inserted, imported: prepared.length,
errors, errors: []
},
});
} catch (err) {
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
return res.status(500).send({ status: "failed", message: err.message });
} }
})
.on("error", (err) => {
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
return res.status(500).send({ status: "failed", message: err.message });
}); });
} catch (error) {
return res.status(500).send({ status: "failed", message: error.message }); } catch (err) {
logger.error("Establishment Import Fatal Error: " + err.message);
return res.status(500).send({
status: "failed",
message: "Invalid data. Please check the instructions given and upload again."
});
} }
}; };
// exports.establishmentBulkUpload = async (req, res) => {
// try {
// // Basic file & user validations
// if (!req.file) {
// return res.status(400).send({ status: "failed", message: "No file uploaded" });
// }
// const filePath = req.file.path;
// if (!req.file.originalname.toLowerCase().endsWith(".csv")) {
// if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
// return res.status(400).send({ status: "failed", message: "Invalid file type. Only CSV allowed." });
// }
// if (!req.user?.id || isNaN(req.user.id)) {
// if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
// return res.status(400).send({ status: "failed", message: "Invalid or missing User Id." });
// }
// const stats = fs.statSync(filePath);
// if (stats.size === 0) {
// if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
// return res.status(400).send({ status: "failed", message: "Uploaded file is empty." });
// }
// // Read CSV into memory (array of normalized rows)
// const rows = [];
// fs.createReadStream(filePath)
// .pipe(csv())
// .on("data", (rawRow) => {
// // Normalize headers and map to canonical keys
// const row = {};
// for (const key in rawRow) {
// const normalizedKey = key.replace(/[\s\W]+/g, "_").trim().toLowerCase();
// // Standard canonical keys
// if (normalizedKey === "establishment_id" || normalizedKey === "establishment_code" || normalizedKey.startsWith("establishment")) {
// row["Establishment Id"] = rawRow[key]?.trim() || null;
// continue;
// }
// if (normalizedKey.includes("factory name") || normalizedKey.includes("factory_name") || normalizedKey.includes("Factory Name")) {
// row["Factory Name"] = rawRow[key]?.trim() || null;
// continue;
// }
// if (normalizedKey.includes("Email") || normalizedKey.includes("email") ) {
// row["Email"] = rawRow[key]?.trim() || null;
// continue;
// }
// if (normalizedKey.includes("Emirate") || normalizedKey.includes("emirate") ) {
// row["Emirate"] = rawRow[key]?.trim() || null;
// continue;
// }
// if (normalizedKey.includes("total employment") || normalizedKey.includes("total_employment") || normalizedKey.includes("total employments")) {
// row["Total Employment"] = rawRow[key]?.trim() || null;
// continue;
// }
// // HS code columns: keep any header that contains 'hs' (hs_code, hs_code_1, hs1, etc.)
// if (/^hs|hs_code|hs_code_\d|hs\d/i.test(normalizedKey) || normalizedKey.includes("hs")) {
// // keep the exact normalized header so we can iterate later
// row[normalizedKey] = rawRow[key]?.trim() || null;
// continue;
// }
// // Fallback - keep other columns too
// row[normalizedKey] = rawRow[key]?.trim() || null;
// }
// rows.push(row);
// })
// .on("end", async () => {
// try {
// if (!rows.length) {
// if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
// return res.status(400).send({ status: "failed", message: "CSV file is empty or invalid." });
// }
// // Check required canonical columns exist in the header (based on first row keys)
// const firstRowKeys = Object.keys(rows[0]);
// const required = ["Establishment Id", "Factory Name", "Email", "Emirate", "Total Employment"];
// const missing = required.filter((k) => !firstRowKeys.includes(k));
// if (missing.length > 0) {
// if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
// return res.status(400).send({
// status: "failed",
// message: `Missing required columns: ${missing.join(", ")}`,
// });
// }
// // Prepare maps: emirates and products
// const emirates = await Emirate.findAll({ attributes: ["id", "name"] });
// const emirateMap = {};
// emirates.forEach((e) => {
// if (e.name) emirateMap[e.name.trim().toLowerCase()] = e.id;
// });
// // Build product map by normalized hs_code (digits only, no leading zeros)
// const products = await Product.findAll({ attributes: ["id", "hs_code"] });
// const productMap = {};
// products.forEach((p) => {
// if (!p.hs_code) return;
// const normalized = String(p.hs_code).replace(/[^0-9]/g, "").replace(/^0+/, "");
// if (normalized) productMap[normalized] = p.id;
// });
// // Existing establishment codes in DB
// const existingEsts = await Establishment.findAll({
// attributes: ["establishment_code", "factory_name", "establishment_contact_email"]
// });
// const existingEstIdSet = new Set(existingEsts.map((e) => e.establishment_code));
// const existingFactorySet = new Set(existingEsts.map((e) => e.factory_name?.trim().toLowerCase()));
// const existingEmailSet = new Set(existingEsts.map((e) => e.establishment_contact_email?.trim().toLowerCase()));
// const errors = [];
// const fileDuplicates = new Set();
// const seenEstIds = new Set();
// const toInsert = [];
// // Process each row: validate & prepare
// for (let i = 0; i < rows.length; i++) {
// const raw = rows[i];
// const rowNumber = i + 1;
// const estId = raw["Establishment Id"]?.trim();
// const factory = raw["Factory Name"]?.trim();
// const email = raw["Email"]?.trim();
// const emirateRaw = raw["Emirate"]?.trim();
// const employmentRaw = raw["Total Employment"]?.trim();
// // Required fields
// if (!estId || !factory || !email || !emirateRaw) {
// errors.push({
// row: rowNumber,
// error: "Missing required fields (Establishment Id, Factory Name, Email, Emirate).",
// });
// continue;
// }
// //Checking repeating data
// const estIdRowMap = {};
// const factoryRowMap = {};
// const emailRowMap = {};
// // During row loop — replace your tracking code with this:
// const keyEst = estId?.trim();
// const keyFactory = factory?.trim().toLowerCase();
// const keyEmail = email?.trim().toLowerCase();
// // Helper for inserting row numbers
// const pushRow = (map, key) =>
// key ? (map[key] = map[key] ? [...map[key], rowNumber] : [rowNumber]) : null;
// // Track all 3
// pushRow(estIdRowMap, keyEst);
// pushRow(factoryRowMap, keyFactory);
// pushRow(emailRowMap, keyEmail);
// const duplicateDetails = [];
// [
// ["Establishment Id", estIdRowMap],
// ["Factory Name", factoryRowMap],
// ["Email", emailRowMap],
// ].forEach(([field, map]) => {
// Object.entries(map).forEach(([value, rows]) => {
// if (rows.length > 1) {
// duplicateDetails.push({
// field,
// value,
// rows
// });
// }
// });
// });
// if (duplicateDetails.length > 0) {
// if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
// return res.status(400).send({
// status: "failed",
// message: "Repeated values found in uploaded file.",
// duplicates: duplicateDetails,
// });
// }
// const hsRawCodes = [];
// for (const k of Object.keys(raw)) {
// if (/^hs|hs_code|hs_code_\d|hs\d/i.test(k) || k.includes("hs")) {
// const v = raw[k];
// if (v && String(v).trim()) {
// hsRawCodes.push(String(v).trim());
// }
// }
// }
// // Validation: at least one HS code must be present
// if (hsRawCodes.length === 0) {
// errors.push({
// row: rowNumber,
// error: "Missing HS Code columns or no HS Code values provided.",
// });
// continue;
// }
// const seenEstIds = new Set();
// const seenFactories = new Set();
// const seenEmails = new Set();
// if (seenEstIds.has(keyEst)) {
// fileDuplicates.add(`Establishment ID: ${keyEst}`);
// continue;
// }
// if (seenFactories.has(keyFactory)) {
// fileDuplicates.add(`Factory Name: ${factory}`);
// continue;
// }
// if (seenEmails.has(keyEmail)) {
// fileDuplicates.add(`Email: ${email}`);
// continue;
// }
// seenEstIds.add(keyEst);
// seenFactories.add(keyFactory);
// seenEmails.add(keyEmail);
// if (existingEstIdSet.has(keyEst)) {
// fileDuplicates.add(`Establishment ID already exists: ${keyEst}`);
// continue;
// }
// if (existingFactorySet.has(keyFactory)) {
// fileDuplicates.add(`Factory Name already exists: ${factory}`);
// continue;
// }
// if (existingEmailSet.has(keyEmail)) {
// fileDuplicates.add(`Email already exists: ${email}`);
// continue;
// }
// // Emirate mapping
// const emirateId = emirateMap[emirateRaw.toLowerCase()];
// if (!emirateId) {
// errors.push({
// row: rowNumber,
// error: `Invalid Emirate: '${emirateRaw}'`,
// });
// continue;
// }
// // Clean HS codes: remove non-digits and leading zeros
// const cleanedHs = hsRawCodes
// .map((hc) => hc.replace(/[^0-9]/g, "").replace(/^0+/, ""))
// .filter(Boolean); // remove empty after cleaning
// const mappedProducts = cleanedHs.map((c) => productMap[c] || null);
// // Find which HS codes do NOT match product master
// const notMapped = cleanedHs.filter((c, i) => mappedProducts[i] === null);
// if (notMapped.length > 0) {
// errors.push({
// row: rowNumber,
// error: `${notMapped} HS Codes do not exist.`,
// not_mapped: notMapped
// });
// continue;
// }
// if (mappedProducts.every((id) => id === null)) {
// errors.push({
// row: rowNumber,
// error: "None of the provided HS Codes match existing products.",
// hs_codes: cleanedHs
// });
// continue;
// }
// // parse employment
// const employment = parseInt(employmentRaw) || 0;
// toInsert.push({
// estCode: estId,
// factory,
// email,
// emirateId,
// employment,
// productIds: [...new Set(mappedProducts)],
// rowIndex: rowNumber,
// });
// }
// // If file duplicates found (either in file or in DB), return error - consistent with product controller
// if (fileDuplicates.size > 0) {
// if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
// return res.status(400).send({
// status: "failed",
// message: "Duplicate Establishment IDs found (in file or already exist in system). Resolve and re-upload.",
// duplicates: [...fileDuplicates],
// });
// }
// // Insert prepared establishments and associated products
// let inserted = 0;
// for (const rec of toInsert) {
// try {
// const createdEst = await Establishment.create({
// establishment_code: rec.estCode,
// factory_name: rec.factory,
// establishment_contact_email: rec.email,
// establishment_emirate_id: rec.emirateId,
// total_employees: rec.employment,
// created_by: req.user.id,
// created_at: new Date(),
// });
// // prepare bulk create payload for establishment_products
// const payload = rec.productIds.map((pid) => ({
// establishment_id: createdEst.id,
// product_id: pid,
// created_by: req.user.id,
// created_at: new Date(),
// is_active: true,
// }));
// await EstablishmentProduct.bulkCreate(payload);
// inserted++;
// } catch (errInner) {
// // Collect row-level DB insert errors for partial success reporting
// errors.push({
// row: rec.rowIndex,
// error: `DB insert error: ${errInner.message}`,
// });
// }
// }
// // cleanup uploaded file
// if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
// // Build final response to match product import style
// let finalStatus = "success";
// let message = `${inserted} establishments inserted successfully.`;
// if (errors.length > 0) {
// finalStatus = inserted > 0 ? "partial_success" : "failed";
// if (finalStatus === "partial_success") {
// message = `${inserted} establishments inserted, ${errors.length} rows failed.`;
// } else {
// message = `No establishments imported. ${errors.length} validation errors found.`;
// }
// }
// return res.status(200).send({
// status: finalStatus,
// message,
// summary: {
// total_records: rows.length,
// imported: inserted,
// errors,
// },
// });
// } catch (err) {
// if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
// return res.status(500).send({ status: "failed", message: err.message });
// }
// })
// .on("error", (err) => {
// if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
// return res.status(500).send({ status: "failed", message: err.message });
// });
// } catch (error) {
// return res.status(500).send({ status: "failed", message: error.message });
// }
// };
// exports.establishmentBulkUpload = async (req, res) => { // exports.establishmentBulkUpload = async (req, res) => {
// try { // try {
// // Basic file & user validations // // Basic file & user validations

View File

@ -328,7 +328,7 @@ exports.uploadProductsFromCSV = async (req, res) => {
continue; continue;
} }
if (hsCode.length > 12) { if (hsCode.length > 10) {
errors.push({ row: index + 1, error: "HS Code must be 10 digits" }); errors.push({ row: index + 1, error: "HS Code must be 10 digits" });
continue; continue;
} }