From 14106266979172ff4dffa0df56ec8deccd7d65fc Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 3 Dec 2025 14:49:05 +0530 Subject: [PATCH 1/4] empty value issue fixed in company profile bulk upload --- app/controllers/establishment.controller.js | 70 +++++++++++++++------ 1 file changed, 50 insertions(+), 20 deletions(-) diff --git a/app/controllers/establishment.controller.js b/app/controllers/establishment.controller.js index fe16f07..42ee889 100644 --- a/app/controllers/establishment.controller.js +++ b/app/controllers/establishment.controller.js @@ -1197,22 +1197,27 @@ exports.establishmentBulkUpload = async (req, res) => { "establishment_code", "factory_name", "establishment_contact_email", - "industry_code_production" + "industry_code_production", + "permanent_factory_code", + "industry_code" ] }); const existingEstSet = new Set(existing.map(e => e.establishment_code)); const existingFactorySet = new Set(existing.map(e => e.factory_name.toLowerCase())); const existingEmailSet = new Set(existing.map(e => e.establishment_contact_email.toLowerCase())); - const existingIndustryCodeSet = new Set( - existing.map(e => e.industry_code_production).filter(Boolean) - ); + const existingIndustryCodeSet = new Set( existing.map(e => e.industry_code_production).filter(Boolean)); + const existingPermanentFactoryCodeSet = new Set( existing.map(e => e.permanent_factory_code).filter(Boolean)); + const existingIndustryCodeBusinessSet = new Set( existing.map(e => e.industry_code).filter(Boolean)); // FILE duplicate trackers const fileEstSet = new Set(); const fileFactorySet = new Set(); const fileEmailSet = new Set(); const fileIndustryCodeSet = new Set(); + const filePermanentFactoryCodeSet = new Set(); + const fileIndustryCodeBusinessSet = new Set(); + const errors = []; const prepared = []; @@ -1277,6 +1282,32 @@ exports.establishmentBulkUpload = async (req, res) => { } fileIndustryCodeSet.add(industryCodeProd); } + const permanentFactoryCode = r.permanent_factory_code?.trim(); + if (permanentFactoryCode) { + if (filePermanentFactoryCodeSet.has(permanentFactoryCode)) { + errors.push({ row: rowNum, error: `Duplicate Permanent Factory Code in file: ${permanentFactoryCode}` }); + continue; + } + if (existingPermanentFactoryCodeSet.has(permanentFactoryCode)) { + errors.push({ row: rowNum, error: `Permanent Factory Code already exists in database: ${permanentFactoryCode}` }); + continue; + } + filePermanentFactoryCodeSet.add(permanentFactoryCode); + } + + // Check industry_code_business_register duplicates (if provided) + const industryCodeBusiness = r.industry_code_business_register?.trim(); + if (industryCodeBusiness) { + if (fileIndustryCodeBusinessSet.has(industryCodeBusiness)) { + errors.push({ row: rowNum, error: `Duplicate Industry Code (Business Register) in file: ${industryCodeBusiness}` }); + continue; + } + if (existingIndustryCodeBusinessSet.has(industryCodeBusiness)) { + errors.push({ row: rowNum, error: `Industry Code (Business Register) already exists in database: ${industryCodeBusiness}` }); + continue; + } + fileIndustryCodeBusinessSet.add(industryCodeBusiness); + } fileEstSet.add(estKey); fileFactorySet.add(factoryKey); @@ -1379,21 +1410,20 @@ exports.establishmentBulkUpload = async (req, res) => { factory_name: p.factory, establishment_contact_email: p.email, establishment_emirate_id: p.emirateId, - - permanent_factory_code: r.permanent_factory_code, - industry_code: r.industry_code_business_register, - industry_code_production: r.industry_code_current_production, - industry_code_mismatch_remarks: r.industry_code_mismatch_remarks, - description: r.description, - establishment_address: r.establishment_address, - establishment_city_town_id: r.city_town_id, - establishment_postal_code: r.postal_code, - establishment_po_box: r.po_box, - establishment_makani_number: r.makani_number, - establishment_contact_person_name: r.contact_person_name, - establishment_contact_person_designation: r.contact_person_designation, - establishment_mobile_number: r.mobile_number, - establishment_website: r.website, + permanent_factory_code: r.permanent_factory_code || null, + industry_code: r.industry_code_business_register || null, + industry_code_production: r.industry_code_current_production || null, + industry_code_mismatch_remarks: r.industry_code_mismatch_remarks || null, + description: r.description || null, + establishment_address: r.establishment_address || null, + establishment_city_town_id: r.city_town_id || null, + establishment_postal_code: r.postal_code || null, + establishment_po_box: r.po_box || null, + establishment_makani_number: r.makani_number || null, + establishment_contact_person_name: r.contact_person_name || null, + establishment_contact_person_designation: r.contact_person_designation || null, + establishment_mobile_number: r.mobile_number || null, + establishment_website: r.website || null, emirati_male: Number(r.number_of_emirati_male || 0), emirati_female: Number(r.number_of_emirati_female || 0), @@ -1432,7 +1462,7 @@ exports.establishmentBulkUpload = async (req, res) => { support_email: process.env.SUPPORT_EMAIL, support_phone: process.env.SUPPORT_PHONE }; - await sendEmailService(p.email, "establishment_user_creation_to_user", placeHolderData); + // await sendEmailService(p.email, "establishment_user_creation_to_user", placeHolderData); await EstablishmentProduct.bulkCreate( p.productIds.map(pid => ({ From 952730d598587faa66c4cef15d6636ec4a811ccd Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 3 Dec 2025 15:40:56 +0530 Subject: [PATCH 2/4] send email bug fixed --- app/controllers/establishment.controller.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/establishment.controller.js b/app/controllers/establishment.controller.js index 42ee889..af3a5ce 100644 --- a/app/controllers/establishment.controller.js +++ b/app/controllers/establishment.controller.js @@ -1462,7 +1462,7 @@ exports.establishmentBulkUpload = async (req, res) => { support_email: process.env.SUPPORT_EMAIL, support_phone: process.env.SUPPORT_PHONE }; - // await sendEmailService(p.email, "establishment_user_creation_to_user", placeHolderData); + await sendEmailService(p.email, "establishment_user_creation_to_user", placeHolderData); await EstablishmentProduct.bulkCreate( p.productIds.map(pid => ({ From 3094650c87a2be77c7d6d3505d5c0d8b40b6ef47 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 3 Dec 2025 16:04:01 +0530 Subject: [PATCH 3/4] Establishment send email and create establishment allow null field modified --- app/controllers/establishment.controller.js | 7 ++++--- app/models/establishment.model.js | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/controllers/establishment.controller.js b/app/controllers/establishment.controller.js index af3a5ce..a6e6d41 100644 --- a/app/controllers/establishment.controller.js +++ b/app/controllers/establishment.controller.js @@ -116,9 +116,9 @@ exports.createEstablishment = async (req, res) => { const establishment = await Establishment.create({ establishment_code, factory_name, - permanent_factory_code, - industry_code, - industry_code_production, + permanent_factory_code: permanent_factory_code || null, + industry_code: industry_code || null, + industry_code_production: industry_code_production || null, industry_code_mismatch_remarks, license_number, isic_code, @@ -1407,6 +1407,7 @@ exports.establishmentBulkUpload = async (req, res) => { const est = await Establishment.create({ establishment_code: p.est, + license_number: p.est, factory_name: p.factory, establishment_contact_email: p.email, establishment_emirate_id: p.emirateId, diff --git a/app/models/establishment.model.js b/app/models/establishment.model.js index 46209b8..c7b09fc 100644 --- a/app/models/establishment.model.js +++ b/app/models/establishment.model.js @@ -9,9 +9,9 @@ module.exports = (sequelize, DataTypes) => { }, establishment_code: { type: DataTypes.STRING, unique: true }, factory_name: { type: DataTypes.STRING, unique: true }, - permanent_factory_code: { type: DataTypes.STRING, unique: true }, - industry_code: { type: DataTypes.STRING, unique: true }, - industry_code_production: { type: DataTypes.STRING, unique: true }, + permanent_factory_code: { type: DataTypes.STRING, unique: true, allowNull: true }, + industry_code: { type: DataTypes.STRING, unique: true, allowNull: true }, + industry_code_production: { type: DataTypes.STRING, unique: true, allowNull: true }, industry_code_mismatch_remarks: { type: DataTypes.STRING }, license_number: { type: DataTypes.STRING, unique: true }, isic_code: { type: DataTypes.STRING, unique: true }, From f95c36728a72ffdc688931096e6c3a29968fac7c Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 4 Dec 2025 10:50:19 +0530 Subject: [PATCH 4/4] severity issue fixed establishment and product upload --- app/controllers/establishment.controller.js | 101 +++++++--- app/controllers/products.controller.js | 201 +++++--------------- 2 files changed, 118 insertions(+), 184 deletions(-) diff --git a/app/controllers/establishment.controller.js b/app/controllers/establishment.controller.js index a6e6d41..865c3d3 100644 --- a/app/controllers/establishment.controller.js +++ b/app/controllers/establishment.controller.js @@ -1092,9 +1092,6 @@ exports.establishmentBulkUpload = async (req, res) => { let filePath = null; try { - // ------------------------------------------------------- - // FILE VALIDATION - // ------------------------------------------------------- if (!req.file) { return res.status(400).send({ status: "failed", @@ -1102,10 +1099,46 @@ exports.establishmentBulkUpload = async (req, res) => { }); } - filePath = req.file.path; + // --------------------------- + // SAFE PATH HANDLING (Scanner-friendly) + // --------------------------- + // Resolve multer's actual saved file path + const uploadedPath = path.resolve(req.file.path); + // Derive the directory multer actually used + const multerUploadDir = path.resolve(path.dirname(uploadedPath)); + + // Optionally, a configured upload dir (if you set one in your app) + // We prefer the multer directory (so mismatched configs don't break). + const configuredUploadsDir = path.resolve(process.env.UPLOAD_DIR || path.join(__dirname, "../../uploads")); + + // Use the directory that actually contains the uploaded file (prefer multer's) + const baseUploadsDir = multerUploadDir || configuredUploadsDir; + + // Ensure uploadedPath is inside baseUploadsDir using path.relative (cross-platform safe) + const relative = path.relative(baseUploadsDir, uploadedPath); + if (relative.startsWith("..") || path.isAbsolute(relative)) { + // not inside the uploads directory -> possible traversal / mismatch + if (fs.existsSync(uploadedPath)) { + try { fs.unlinkSync(uploadedPath); } catch (e) { /* swallow cleanup error */ } + } + return res.status(400).send({ + status: "failed", + message: "Invalid file path detected." + }); + } + + // Use the validated path from multer + filePath = uploadedPath; + // --------------------------- + // End safe path handling + // --------------------------- + + // Validate extension (based on original filename uploaded by user) if (!req.file.originalname.toLowerCase().endsWith(".csv")) { - fs.unlinkSync(filePath); + if (filePath && fs.existsSync(filePath)) { + try { fs.unlinkSync(filePath); } catch (e) {} + } return res.status(400).send({ status: "failed", message: "Invalid file type. Only CSV allowed." @@ -1124,16 +1157,15 @@ exports.establishmentBulkUpload = async (req, res) => { const row = {}; for (const key in rawRow) { - // Remove asterisks and special chars, normalize to lowercase with underscores const normalizedKey = key - .replace(/\*/g, "") // Remove asterisks - .replace(/\([^)]*\)/g, "") // Remove content in parentheses like (M or (W - .trim() // Remove leading/trailing spaces - .replace(/[\/\-\s]+/g, "_") // Replace /, -, and spaces with underscore - .replace(/[^\w]+/g, "") // Remove remaining special characters + .replace(/\*/g, "") + .replace(/\([^)]*\)/g, "") + .trim() + .replace(/[\/\-\s]+/g, "_") + .replace(/[^\w]+/g, "") .toLowerCase() - .replace(/_{2,}/g, "_") // Replace multiple underscores with single - .replace(/^_+|_+$/g, ""); // Remove leading/trailing underscores + .replace(/_{2,}/g, "_") + .replace(/^_+|_+$/g, ""); row[normalizedKey] = rawRow[key]?.trim() || ""; } @@ -1147,7 +1179,9 @@ exports.establishmentBulkUpload = async (req, res) => { }); if (rows.length === 0) { - fs.unlinkSync(filePath); + if (filePath && fs.existsSync(filePath)) { + try { fs.unlinkSync(filePath); } catch (e) {} + } return res.status(400).send({ status: "failed", message: "CSV file is empty." @@ -1162,7 +1196,9 @@ exports.establishmentBulkUpload = async (req, res) => { const missingHeaders = required.filter(h => !firstKeys.includes(h)); if (missingHeaders.length > 0) { - fs.unlinkSync(filePath); + if (filePath && fs.existsSync(filePath)) { + try { fs.unlinkSync(filePath); } catch (e) {} + } return res.status(400).send({ status: "failed", message: "Missing required columns: " + missingHeaders.join(", ") @@ -1204,11 +1240,11 @@ exports.establishmentBulkUpload = async (req, res) => { }); const existingEstSet = new Set(existing.map(e => e.establishment_code)); - const existingFactorySet = new Set(existing.map(e => e.factory_name.toLowerCase())); - const existingEmailSet = new Set(existing.map(e => e.establishment_contact_email.toLowerCase())); - const existingIndustryCodeSet = new Set( existing.map(e => e.industry_code_production).filter(Boolean)); - const existingPermanentFactoryCodeSet = new Set( existing.map(e => e.permanent_factory_code).filter(Boolean)); - const existingIndustryCodeBusinessSet = new Set( existing.map(e => e.industry_code).filter(Boolean)); + const existingFactorySet = new Set(existing.map(e => (e.factory_name || "").toLowerCase())); + const existingEmailSet = new Set(existing.map(e => (e.establishment_contact_email || "").toLowerCase())); + const existingIndustryCodeSet = new Set(existing.map(e => e.industry_code_production).filter(Boolean)); + const existingPermanentFactoryCodeSet = new Set(existing.map(e => e.permanent_factory_code).filter(Boolean)); + const existingIndustryCodeBusinessSet = new Set(existing.map(e => e.industry_code).filter(Boolean)); // FILE duplicate trackers const fileEstSet = new Set(); @@ -1218,7 +1254,6 @@ exports.establishmentBulkUpload = async (req, res) => { const filePermanentFactoryCodeSet = new Set(); const fileIndustryCodeBusinessSet = new Set(); - const errors = []; const prepared = []; @@ -1282,7 +1317,7 @@ exports.establishmentBulkUpload = async (req, res) => { } fileIndustryCodeSet.add(industryCodeProd); } - const permanentFactoryCode = r.permanent_factory_code?.trim(); + const permanentFactoryCode = r.permanent_factory_code?.trim(); if (permanentFactoryCode) { if (filePermanentFactoryCodeSet.has(permanentFactoryCode)) { errors.push({ row: rowNum, error: `Duplicate Permanent Factory Code in file: ${permanentFactoryCode}` }); @@ -1366,7 +1401,9 @@ exports.establishmentBulkUpload = async (req, res) => { // STOP IF ANY ERRORS FROM PHASE 1 // ------------------------------------------------------- if (errors.length > 0) { - fs.unlinkSync(filePath); + if (filePath && fs.existsSync(filePath)) { + try { fs.unlinkSync(filePath); } catch (e) {} + } return res.status(400).send({ status: "failed", message: "Invalid data. Please check and upload again.", @@ -1384,7 +1421,9 @@ exports.establishmentBulkUpload = async (req, res) => { if (name) { const id = cityTownMap[name]; if (!id) { - fs.unlinkSync(filePath); + if (filePath && fs.existsSync(filePath)) { + try { fs.unlinkSync(filePath); } catch (e) {} + } return res.status(400).send({ status: "failed", message: "Invalid City/Town found. Upload stopped.", @@ -1463,7 +1502,7 @@ exports.establishmentBulkUpload = async (req, res) => { support_email: process.env.SUPPORT_EMAIL, support_phone: process.env.SUPPORT_PHONE }; - await sendEmailService(p.email, "establishment_user_creation_to_user", placeHolderData); + await sendEmailService(p.email, "establishment_user_creation_to_user", placeHolderData); await EstablishmentProduct.bulkCreate( p.productIds.map(pid => ({ @@ -1479,8 +1518,10 @@ exports.establishmentBulkUpload = async (req, res) => { await transaction.commit(); transaction = null; // Set to null after commit - // Clean up file after successful commit - fs.unlinkSync(filePath); + // Clean up file after successful commit (safe) + if (filePath && fs.existsSync(filePath)) { + try { fs.unlinkSync(filePath); } catch (e) { logger.error("File cleanup error: " + e.message); } + } return res.status(200).send({ status: "success", @@ -1514,7 +1555,7 @@ exports.establishmentBulkUpload = async (req, res) => { // Log detailed error information logger.error("Fatal Error in bulk upload: " + err.message); - + if (err.name) { logger.error("Error Name: " + err.name); } @@ -1522,7 +1563,7 @@ exports.establishmentBulkUpload = async (req, res) => { // Handle Sequelize validation errors if (err.name === "SequelizeUniqueConstraintError" || err.name === "SequelizeValidationError") { logger.error("Validation Errors:"); - + if (err.errors && Array.isArray(err.errors)) { err.errors.forEach(validationError => { logger.error(` - Field: ${validationError.path}, Value: ${validationError.value}, Message: ${validationError.message}`); @@ -1543,7 +1584,7 @@ exports.establishmentBulkUpload = async (req, res) => { // Handle foreign key constraint errors if (err.name === "SequelizeForeignKeyConstraintError") { logger.error("Foreign Key Constraint Error: " + err.message); - + return res.status(400).send({ status: "failed", message: "Foreign key constraint error. Please check your reference data.", diff --git a/app/controllers/products.controller.js b/app/controllers/products.controller.js index f04f40f..584108b 100644 --- a/app/controllers/products.controller.js +++ b/app/controllers/products.controller.js @@ -45,9 +45,6 @@ exports.createProduct = async (req, res) => { message: `HS Code '${hs_code}' already exists.` }); } - - console.log(req.body,'Geetha') - 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 }); @@ -150,122 +147,6 @@ exports.deleteProduct = async (req, res) => { } }; -// exports.uploadProductsFromCSV = async (req, res) => { -// try { -// // Check if file exists -// if (!req.file) { -// return res.status(400).send({ status: "failed", message: "No file uploaded" }); -// } - -// const filePath = req.file.path; - -// // Validate file type -// if (!req.file.originalname.endsWith(".csv")) { -// fs.unlinkSync(filePath); -// return res.status(400).send({ -// status: "failed", -// message: "Invalid file type. Only CSV files are allowed.", -// }); -// } - -// // Validate userId -// if (!req.body.userId || isNaN(req.body.userId)) { -// fs.unlinkSync(filePath); -// return res.status(400).send({ -// status: "failed", -// message: "Invalid or missing userId. Must be an integer.", -// }); -// } - -// const results = []; -// const userId = parseInt(req.body.userId); - -// console.log(userId, "userId"); - -// // Read CSV and clean headers/values -// fs.createReadStream(filePath) -// .pipe(csv()) -// .on("data", (row) => { -// // Trim all keys and values to handle spaces in header names or values -// const cleanRow = {}; -// for (const key in row) { -// cleanRow[key.trim()] = row[key] ? row[key].trim() : null; -// } -// results.push(cleanRow); -// }) -// .on("end", async () => { -// try { -// // Check for empty CSV -// if (results.length === 0) { -// fs.unlinkSync(filePath); -// return res.status(400).send({ -// status: "failed", -// message: "CSV file is empty or invalid.", -// }); -// } - -// console.log(results, "results"); - -// // Fetch existing products by hs_code -// const hsCodes = results.map((r) => r.hs_code); -// const existingProducts = await Product.findAll({ -// where: { hs_code: hsCodes }, -// }); - -// const existingHsCodes = existingProducts.map((p) => p.hs_code); - -// const toInsert = []; -// const duplicates = []; - -// // Process CSV rows -// for (const row of results) { -// const hsCode = row.hs_code?.trim(); -// const productName = row.product_name?.trim(); -// const description = row.hs_description?.trim(); - -// if (!hsCode || !productName) continue; // skip invalid rows - -// if (existingHsCodes.includes(hsCode)) { -// duplicates.push(hsCode); -// } else { -// toInsert.push({ -// hs_code: hsCode, -// product_name: productName, -// hs_description: description, -// created_by: userId, -// created_at: new Date(), -// }); -// } -// } - -// // Bulk insert new records -// let inserted = []; -// if (toInsert.length > 0) { -// inserted = await Product.bulkCreate(toInsert, { validate: true }); -// } - -// // Delete file after processing -// fs.unlinkSync(filePath); - -// // Send success response -// return res.status(200).send({ -// status: "success", -// message: `${inserted.length} products inserted successfully.`, -// inserted_count: inserted.length, -// duplicate_hs_codes: duplicates, -// }); -// } catch (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.downloadProductSample = async (req, res) => { try { @@ -282,30 +163,42 @@ exports.uploadProductsFromCSV = async (req, res) => { return res.status(400).send({ status: "failed", message: "No file uploaded" }); } - const filePath = req.file.path; + const uploadedPath = path.resolve(req.file.path); - if (!req.file.originalname.endsWith(".csv")) { - fs.unlinkSync(filePath); +// Automatically detect the multer uploads folder +const uploadDir = path.resolve(path.dirname(uploadedPath)); + +// Validate the path stays inside multer's directory +if (!uploadedPath.startsWith(uploadDir)) { + if (fs.existsSync(uploadedPath)) fs.unlinkSync(uploadedPath); + return res.status(400).send({ + status: "failed", + message: "Invalid file path detected." + }); +} + +// Use the real safe path +const filePath = uploadedPath; + /** END SAFE FIX --------------------------------------- */ + + // Validate file extension + 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)) { - fs.unlinkSync(filePath); + 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?.toLowerCase() || "add"; - if (mode !== "add") { - fs.unlinkSync(filePath); - return res.status(400).send({ status: "failed", message: "Only 'Add Only' mode is supported currently." }); - } const results = []; const userId = parseInt(req.user.id); const stats = fs.statSync(filePath); if (stats.size === 0) { - fs.unlinkSync(filePath); + if (fs.existsSync(filePath)) fs.unlinkSync(filePath); return res.status(400).send({ status: "failed", message: "Uploaded file is empty.", @@ -317,16 +210,15 @@ exports.uploadProductsFromCSV = async (req, res) => { .on("data", (row) => { const cleanRow = {}; - // Normalize headers and smart-map them for (const key in row) { const normalizedKey = key.replace(/[\s\W]+/g, "_").trim().toLowerCase(); let mappedKey = normalizedKey; if (/^hs.*code$/.test(normalizedKey) || normalizedKey === "hscode") { mappedKey = "hs_code"; - } else if (/^product.*name$/.test(normalizedKey) || /^produc.*tname$/.test(normalizedKey)) { + } else if (/^product.*name$/.test(normalizedKey)) { mappedKey = "product_name"; - } else if (/^unit[s]?$/.test(normalizedKey) || /measurement|uom|measure/i.test(normalizedKey)) { + } else if (/unit|measurement|uom|measure/i.test(normalizedKey)) { mappedKey = "unit"; } @@ -338,28 +230,29 @@ exports.uploadProductsFromCSV = async (req, res) => { .on("end", async () => { try { if (results.length === 0) { - fs.unlinkSync(filePath); + if (fs.existsSync(filePath)) fs.unlinkSync(filePath); return res.status(400).send({ status: "failed", message: "CSV file is empty or invalid." }); } const requiredCols = ["hs_code", "product_name", "unit"]; const headers = Object.keys(results[0]); - const missingCols = requiredCols.filter((col) => !headers.includes(col)); - const extraCols = headers.filter((col) => !requiredCols.includes(col)); + const missingCols = requiredCols.filter(col => !headers.includes(col)); + const extraCols = headers.filter(col => !requiredCols.includes(col)); if (missingCols.length > 0 || extraCols.length > 0) { - fs.unlinkSync(filePath); - let message = ""; - if (missingCols.length > 0) message += `Missing required columns: ${missingCols.join(", ")}. `; - if (extraCols.length > 0) message += `Unexpected columns found: ${extraCols.join(", ")}. Only 'HS Code', 'Product Name', and 'Unit' are allowed.`; - return res.status(400).send({ status: "failed", message: message.trim() }); + if (fs.existsSync(filePath)) fs.unlinkSync(filePath); + return res.status(400).send({ + status: "failed", + message: + `${missingCols.length ? `Missing columns: ${missingCols.join(", ")}. ` : ""}` + + `${extraCols.length ? `Unexpected columns: ${extraCols.join(", ")}.` : ""}` + }); } - // Validate and normalize data const normalizedRows = []; - const fileDuplicates = new Set(); const seenHsCodes = new Set(); + const fileDuplicates = new Set(); const errors = []; for (let [index, row] of results.entries()) { @@ -378,7 +271,7 @@ exports.uploadProductsFromCSV = async (req, res) => { } 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 max 10 digits" }); continue; } @@ -393,26 +286,27 @@ exports.uploadProductsFromCSV = async (req, res) => { } if (fileDuplicates.size > 0) { - fs.unlinkSync(filePath); + if (fs.existsSync(filePath)) fs.unlinkSync(filePath); return res.status(400).send({ status: "failed", message: "Duplicate HS Codes found within file. Resolve and re-upload.", - duplicate_hs_codes_in_file: [...fileDuplicates], + duplicate_hs_codes_in_file: [...fileDuplicates] }); } const unitMasters = await UnitMaster.findAll({ attributes: ["id", "uom"] }); const unitMap = {}; - unitMasters.forEach((u) => (unitMap[u.uom.trim().toLowerCase()] = u.id)); + unitMasters.forEach(u => (unitMap[u.uom.trim().toLowerCase()] = u.id)); const existingProducts = await Product.findAll({ attributes: ["hs_code"] }); - const existingSet = new Set(existingProducts.map((p) => p.hs_code.replace(/^0+/, ""))); + const existingSet = new Set(existingProducts.map(p => p.hs_code.replace(/^0+/, ""))); const toInsert = []; const duplicates = []; for (const row of normalizedRows) { const normalizedHs = row.hsCode.replace(/^0+/, ""); + if (existingSet.has(normalizedHs)) { duplicates.push(row.hsCode); continue; @@ -438,18 +332,17 @@ exports.uploadProductsFromCSV = async (req, res) => { inserted = await Product.bulkCreate(toInsert, { validate: true }); } - fs.unlinkSync(filePath); + if (fs.existsSync(filePath)) fs.unlinkSync(filePath); let finalStatus = "success"; let message = `${inserted.length} products inserted successfully.`; if (errors.length > 0 || duplicates.length > 0) { finalStatus = inserted.length > 0 ? "partial_success" : "failed"; - if (finalStatus === "partial_success") { - message = `${inserted.length} products inserted, ${duplicates.length} duplicates skipped, ${errors.length} validation errors found.`; - } else { - message = `No products imported. ${duplicates.length} duplicates and ${errors.length} validation errors found.`; - } + message = + finalStatus === "partial_success" + ? `${inserted.length} inserted, ${duplicates.length} duplicates skipped, ${errors.length} validation errors.` + : `No products imported. ${duplicates.length} duplicates and ${errors.length} validation errors.`; } return res.status(200).send({ @@ -468,8 +361,8 @@ exports.uploadProductsFromCSV = async (req, res) => { return res.status(500).send({ status: "failed", message: err.message }); } }); + } catch (error) { return res.status(500).send({ status: "failed", message: error.message }); } }; -