fcsc_ipi_backend/app/controllers/establishment.controller.js

2001 lines
68 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const db = require("../models");
const bcrypt = require("bcryptjs");
const Establishment = db.Establishment;
const EstablishmentUser = db.EstablishmentUser;
const EstablishmentPasswordResetRequest = db.EstablishmentPasswordResetRequest;
const EstablishmentProduct = db.EstablishmentProduct;
const CityTown = db.CityTown;
const Emirate = db.Emirate;
const user = db.user;
const Product = db.Product;
const ExcelJS = require("exceljs");
const { Op, Sequelize } = require("sequelize");
const { sendEmail } = require("../services/emailHelper");
const { sendEmailService } = require("../services/email.service");
const logger = require("../services/logger");
const fs = require("fs");
const csv = require("csv-parser");
const path = require("path");
exports.testEmail = async (req, res) => {
placeHolderData = {
contact_name : 'Gowtham',
portal_url : process.env.FE_BASE_URL,
username : '--',
password : '--',
support_email : '--',
support_phone : '--',
}
await sendEmailService('gowthamceline46@gmail.com', 'establishment_user_creation_to_user', placeHolderData);
};
exports.createEstablishment = async (req, res) => {
try {
const {
establishment_code,
factory_name,
permanent_factory_code,
industry_code,
industry_code_production,
license_number,
isic_code,
description,
// Establishment Contact Details
establishment_address,
establishment_city_town_id,
establishment_emirate_id,
establishment_postal_code,
establishment_po_box,
establishment_makani_number,
establishment_contact_person_name,
establishment_contact_person_designation,
establishment_mobile_number,
establishment_contact_email,
establishment_website,
// Corporate Details
corporate_same_as_establishment,
corporate_name,
corporate_address,
corporate_city_town_id,
corporate_emirate_id,
corporate_postal_code,
corporate_po_box,
corporate_makani_number,
corporate_contact_person_name,
corporate_contact_person_designation,
corporate_mobile_number,
corporate_email,
corporate_website,
emirati_male,
emirati_female,
non_emirati_male,
non_emirati_female,
total_emirati,
total_employees,
created_by,
establishment_user,
establishment_products
} = req.body;
// Basic Validation
if (!establishment_code || !factory_name || !establishment_user) {
return res.status(400).send({
status: "failed",
message: "Missing required fields: establishment_code, factory_name, email, establishment_user",
});
}
// Check if establishment already exists
const existingEstablishment = await Establishment.findOne({where: { establishment_code }, });
if (existingEstablishment) {
return res.status(400).send({status: "failed",message: "Establishment code already exists", });
}
// Create establishment record
const establishment = await Establishment.create({
establishment_code,
factory_name,
permanent_factory_code,
industry_code,
industry_code_production,
license_number,
isic_code,
description,
establishment_address,
establishment_city_town_id,
establishment_emirate_id,
establishment_postal_code,
establishment_po_box,
establishment_makani_number,
establishment_contact_person_name,
establishment_contact_person_designation,
establishment_mobile_number,
establishment_contact_email,
establishment_website,
corporate_same_as_establishment,
corporate_name,
corporate_address,
corporate_city_town_id,
corporate_emirate_id,
corporate_postal_code,
corporate_po_box,
corporate_makani_number,
corporate_contact_person_name,
corporate_contact_person_designation,
corporate_mobile_number,
corporate_email,
corporate_website,
emirati_male,
emirati_female,
non_emirati_male,
non_emirati_female,
total_emirati,
total_employees,
created_by: req.body.created_by || req.user.id,
created_at: new Date(),
});
// Hash password
const hashedPassword = await bcrypt.hash(establishment_user.password, 10);
// Create linked Establishment User
const user = await EstablishmentUser.create({
establishment_id: establishment.id,
name: establishment_user.name,
email: establishment_user.email,
password: hashedPassword,
created_by: req.body.created_by || req.user.id,
});
//send email to user
placeHolderData = {
contact_name : establishment_user.name,
portal_url : process.env.FE_BASE_URL,
username : establishment_user.email,
password : establishment_user.password,
support_email : process.env.SUPPORT_EMAIL,
support_phone : process.env.SUPPORT_PHONE,
}
await sendEmailService(establishment_user.email, 'establishment_user_creation_to_user', placeHolderData);
// insert establishment_products
if (Array.isArray(establishment_products) && establishment_products.length > 0) {
// remove duplicates from request itself
let uniqueProducts = [...new Set(establishment_products.map(x => x.product_id))];
// now check which already exist for this establishment
const existing = await EstablishmentProduct.findAll({
where: {
establishment_id: establishment.id,
product_id: uniqueProducts
},
attributes: ['product_id']
});
const existingIds = existing.map(x => x.product_id);
// filter only new ones (not existing)
const newProducts = uniqueProducts
.filter(pid => !existingIds.includes(pid))
.map(pid => ({
establishment_id: establishment.id,
product_id: pid,
created_at: new Date(),
}));
// only insert if have new ones
if (newProducts.length) {
await EstablishmentProduct.bulkCreate(newProducts);
}
}
// 🔹 Return success response
return res.status(201).send({
status: "success",
message: "Establishment and linked user created successfully",
data: {
establishment,
user: {
id: user.id,
name: user.name,
email: user.email,
},
},
});
} catch (err) {
if (err.name === "SequelizeUniqueConstraintError") {
// extract exact field
const field = err.errors[0].path; // <-- this gives the column name
return res.status(400).json({
status: "failed",
message: `${field} already exists`
});
}
return res.status(500).send({status: "failed",message: err.message || "Internal server error",});
}
};
// Get all establishments
// exports.getAllEstablishments = async (req, res) => {
// try {
// const data = await Establishment.findAll({
// include: [
// // {
// // model: EstablishmentUser,
// // as: "users",
// // attributes: ["id", "name", "email", "is_active"],
// // },
// {
// model: CityTown,
// as: "establishment_city",
// attributes: ["name"],
// },
// {
// model: Emirate,
// as: "establishment_emirate",
// attributes: ["name"],
// },{
// model: CityTown,
// as: "corporate_city",
// attributes: ["name"],
// },
// {
// model: Emirate,
// as: "corporate_emirate",
// attributes: ["name"],
// },
// {
// model: user,
// as: "created_user",
// attributes: ["name"],
// }
// ],
// });
// return res.status(200).send({'status':"success",'message':"Fetched successfully",'data': data });
// } catch (err) {
// return res.status(500).send({'status':"failed",'message':err.message });
// }
// };
exports.getAllEstablishments = async (req, res) => {
try {
let {
page = 1,
limit = 10,
search = "",
emirate_id,
isic_code,
status,
sort_by = "created_at",
sort_order = "DESC",
export: exportType, // detect ?export=excel
} = req.query;
page = parseInt(page);
limit = parseInt(limit);
const offset = (page - 1) * limit;
// Build dynamic where clause
const whereClause = {};
// Filters
if (status) whereClause.is_active = status === "active" ? 1 : 0;
if (isic_code) whereClause.isic_code = isic_code;
if (emirate_id) whereClause.establishment_emirate_id = emirate_id;
// Search by name, establishment_code, or contact emails
if (search) {
whereClause[Op.or] = [
{ factory_name: { [Op.like]: `%${search}%` } },
{ establishment_code: { [Op.like]: `%${search}%` } },
{ establishment_contact_email: { [Op.like]: `%${search}%` } },
{ corporate_email: { [Op.like]: `%${search}%` } },
];
}
// Fetch Data
const data = await Establishment.findAll({
where: whereClause,
attributes: {
include: [
[
Sequelize.literal(`(SELECT COUNT(*) FROM establishment_products ep WHERE ep.establishment_id = establishments.id)`),"product_count" ]
]
},
include: [
{ model: CityTown, as: "establishment_city", attributes: ["name"] },
{ model: Emirate, as: "establishment_emirate", attributes: ["name"] },
{ model: CityTown, as: "corporate_city", attributes: ["name"] },
{ model: Emirate, as: "corporate_emirate", attributes: ["name"] },
{ model: user, as: "created_user", attributes: ["name"] },
],
order: [[sort_by, sort_order]],
...(exportType ? {} : { limit, offset }), // pagination only when not exporting
});
// If export = excel → generate file
if (exportType && exportType.toLowerCase() === "excel") {
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet("Establishments");
// 🧩 Define headers
worksheet.columns = [
{ header: "ID", key: "id", width: 10 },
{ header: "Establishment Name", key: "factory_name", width: 30 },
{ header: "Contact Name", key: "establishment_contact_person_name", width: 30 },
{ header: "Emirate", key: "emirate_name", width: 20 },
{ header: "ISIC Code", key: "isic_code", width: 20 },
{ header: "Establishment ID", key: "establishment_code", width: 20 },
{ header: "Total Employees", key: "total_employees", width: 20 },
{ header: "Created By", key: "created_user", width: 20 },
{ header: "Created On", key: "created_at", width: 20 },
{ header: "Last Updated", key: "updated_at", width: 20 },
{ header: "Status", key: "status", width: 15 },
// { header: "City/Town", key: "city_name", width: 20 },
// { header: "Contact Email", key: "contact_email", width: 30 },
// { header: "Corporate Email", key: "corporate_email", width: 30 },
];
// 🧠 Format data
data.forEach((item) => {
worksheet.addRow({
id: item.id,
factory_name: item.factory_name,
establishment_contact_person_name: item.establishment_contact_person_name,
emirate_name: item.establishment_emirate?.name || "-",
isic_code: item.isic_code || "-",
establishment_code: item.establishment_code,
total_employees: item.total_employees,
created_user: item.created_user?.name || "-",
created_at: new Date(item.created_at).toLocaleDateString(),
updated_at: new Date(item.updated_at).toLocaleDateString(),
status: item.is_active ? "Active" : "Inactive",
// city_name: item.establishment_city?.name || "-",
// contact_email: item.establishment_contact_email || "-",
// corporate_email: item.corporate_email || "-",
});
});
// 🖋️ Styling header
worksheet.getRow(1).eachCell((cell) => {
cell.font = { bold: true };
cell.alignment = { horizontal: "center" };
cell.border = {
top: { style: "thin" },
left: { style: "thin" },
bottom: { style: "thin" },
right: { style: "thin" },
};
});
// 📤 Send as Excel file
res.setHeader(
"Content-Type",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
);
res.setHeader(
"Content-Disposition",
`attachment; filename=Establishments_${Date.now()}.xlsx`
);
await workbook.xlsx.write(res);
return res.end();
}
// Otherwise return JSON (paginated)
const totalCount = await Establishment.count({ where: whereClause });
return res.status(200).json({
status: "success",
message: "Fetched establishments successfully",
data,
pagination: {
total_records: totalCount,
current_page: page,
total_pages: Math.ceil(totalCount / limit),
limit,
},
});
// return res.status(200).json({
// status: "success",
// message: "Fetched establishments successfully",
// data: rows,
// pagination: {
// total_records: count,
// current_page: page,
// total_pages: Math.ceil(count / limit),
// limit,
// },
// });
} catch (err) {
console.error(err);
return res.status(500).json({
status: "failed",
message: err.message || "Internal server error",
});
}
};
// Get one establishment
exports.getEstablishmentById = async (req, res) => {
try {
const data = await Establishment.findByPk(req.params.id, {
include: [
{
model: EstablishmentUser,
as: "users",
attributes: ["id", "name", "email", "gender", "is_active"],
},
{
model: CityTown,
as: "establishment_city",
attributes: ["name"],
},
{
model: Emirate,
as: "establishment_emirate",
attributes: ["name"],
},{
model: CityTown,
as: "corporate_city",
attributes: ["name"],
},
{
model: Emirate,
as: "corporate_emirate",
attributes: ["name"],
},
{
model: user,
as: "created_user",
attributes: ["name"],
},
{
model: EstablishmentProduct,
as: "establishment_products",
attributes: ["id", "establishment_id", "product_id"],
include: [
{
model: Product,
as: "product",
attributes: ["product_name", "hs_code", "hs_description"],
},
],
},
],
});
if (!data) return res.status(404).send({'status':"failed",'message':"Record Not found",'data': "" });
return res.status(200).send({'status':"success",'message':"Fetched successfully",'data': data });
} catch (err) {
return res.status(500).send({'status':"failed",'message':err.message });
}
};
// Update establishment
exports.updateEstablishment = async (req, res) => {
try {
const { id } = req.params;
const { establishment_products, establishment_user, ...estData } = req.body;
const establishment = await Establishment.findByPk(id);
if (!establishment) {
return res.status(404).json({ status: "failed", message: "Record not found" });
}
// check duplicate establishment_code
if (estData.establishment_code) {
const exists = await Establishment.findOne({
where: { establishment_code: estData.establishment_code, id: { [Op.ne]: id } }
});
if (exists) {
return res.status(400).json({ status:"failed", message:"establishment_code already exists" });
}
}
estData.updated_by = estData.updated_by || req.user.id;
estData.updated_at = estData.updated_at || new Date();
// update establishment
await establishment.update(estData);
// update user if needed
if (establishment_user) {
const user = await EstablishmentUser.findOne({where:{establishment_id:id}});
if (user) {
let updateUser = {
name: establishment_user.name,
email: establishment_user.email
};
if (establishment_user.password) {
updateUser.password = await bcrypt.hash(establishment_user.password,10);
}
updateUser.updated_by = updateUser.updated_by || req.user.id;
updateUser.updated_at = updateUser.updated_at || new Date();
await user.update(updateUser);
}
}
// update establishment products
if (Array.isArray(establishment_products)) {
let incomingIds = establishment_products.map(e => e.product_id);
const oldProducts = await EstablishmentProduct.findAll({ where:{ establishment_id:id } });
const oldIds = oldProducts.map(e => e.product_id);
// remove products not in new list
const removeIds = oldIds.filter(v => !incomingIds.includes(v));
if (removeIds.length) {
await EstablishmentProduct.destroy({ where:{ establishment_id:id, product_id:removeIds } });
}
// add only new ones
const newIds = incomingIds.filter(v => !oldIds.includes(v));
for (let pid of newIds) {
await EstablishmentProduct.create({ establishment_id:id, product_id:pid, created_by: req.user.id});
}
}
return res.json({ status:"success", message:"Updated successfully" , data:"" });
} catch(err) {
if (err.name === "SequelizeUniqueConstraintError") {
const field = err.errors[0].path;
return res.status(400).json({ status:"failed", message:`${field} already exists` });
}
return res.status(500).json({ status:"failed", message: err.message });
}
};
// exports.updateEstablishment = async (req, res) => {
// try {
// const [updated] = await Establishment.update(req.body, {
// where: { id: req.params.id },
// });
// if (!updated){ return res.status(404).send({'status':"failed",'message':"Record Not found",'data': "" }); }
// return res.status(200).send({'status':"success",'message':"Updated successfully",'data': "" });
// } catch (err) {
// return res.status(500).send({'status':"failed",'message':err.message });
// }
// };
// Delete establishment
exports.deleteEstablishment = async (req, res) => {
try {
const deleted = await Establishment.destroy({ where: { id: req.params.id } });
if (!deleted) return res.status(404).send({'status':"failed",'message':"Record Not found",'data': "" });
return res.status(200).send({'status':"success",'message':"Deleted successfully",'data': "" });
} catch (err) {
return res.status(500).send({'status':"failed",'message':err.message });
}
};
exports.getAllEmirates = async (req, res) => {
try {
const emirates = await Emirate.findAll({
attributes: ["id", "name", "code"],
order: [["name", "ASC"]],
});
return res.status(200).send({status: "success", message: "Emirates fetched successfully", data: emirates,});
} catch (err) {
return res.status(500).send({status: "failed",message: err.message || "Internal server error", });
}
};
exports.getAllCityTowns = async (req, res) => {
try {
const { emirate_id } = req.query;
const whereClause = {};
if (emirate_id) {
whereClause.emirate_id = emirate_id;
}
const cities = await CityTown.findAll({
where: whereClause,
attributes: ["id", "name", "emirate_id"],
order: [["name", "ASC"]],
});
return res.status(200).send({status: "success", message: "City/Town fetched successfully", data: cities, });
} catch (err) {
return res.status(500).send({ status: "failed", message: err.message || "Internal server error", });
}
};
exports.getAllRequests = async (req, res) => {
try {
const requests = await EstablishmentPasswordResetRequest.findAll({
order: [["created_at", "DESC"]],
});
res.status(200).json({
status: "success",
data: requests,
});
} catch (err) {
res.status(500).json({ status: "failed", message: err.message });
}
};
exports.createRequest = async (req, res) => {
try {
const {
establishment_name,
establishment_code,
registered_email,
contact_person_name,
contact_phone,
additional_notes,
} = req.body;
// Validate input
if (!establishment_name || !establishment_code || !registered_email) {
return res.status(400).json({
status: "failed",
message: "establishment_name, establishment_code, and registered_email are required",
});
}
// Find user by email
const user = await EstablishmentUser.findOne({
where: { email: registered_email },
});
if (!user)
return res.status(404).json({ status: "failed", message: "Registered email not found" });
// Find establishment by code
const establishment = await Establishment.findOne({
where: { code: establishment_code },
});
if (!establishment)
return res.status(404).json({ status: "failed", message: "Invalid establishment code" });
// Verify both match
if (user.establishment_id !== establishment.id)
return res.status(400).json({
status: "failed",
message: "Establishment mismatch between code and email",
});
// Create reset request
const newRequest = await EstablishmentPasswordResetRequest.create({
establishment_name,
establishment_code,
registered_email,
contact_person_name,
contact_phone,
additional_notes,
establishment_user_id: user.id,
establishment_id: establishment.id,
created_by: req.user.id,
});
// Trigger Email
const subject = "Password Reset Request Received";
const body = `
Dear ${contact_person_name || establishment_name},
We have received your password reset request for establishment "${establishment_name}".
Our team will verify and get back to you shortly.
Regards,
Support Team
`;
await sendEmail(registered_email, subject, body);
res.status(201).json({
status: "success",
message: "Password reset request created and email sent successfully",
data: newRequest,
});
} catch (err) {
console.error(err);
res.status(500).json({ status: "failed", message: err.message });
}
};
exports.forgotPasswordRequestOTP = async (req, res) => {
try {
const { user_type , establishment_name, establishment_code, registered_email } = req.body;
if(user_type == 'establishment_user')
{
// Validate inputs
if (!establishment_name || !establishment_code || !registered_email)
return res.status(400).json({ status: "failed", message: "All fields are required" });
// Find establishment and user
const establishment = await Establishment.findOne({ where: { establishment_code } });
if (!establishment)
return res.status(404).json({ status: "failed", message: "Establishment not found" });
const user = await EstablishmentUser.findOne({
where: { email: registered_email, establishment_id: establishment.id },
});
if (!user)
return res.status(404).json({ status: "failed", message: "User not found for this establishment" });
// Generate 6-digit OTP
const otp = Math.floor(100000 + Math.random() * 900000).toString();
// Hash OTP before saving (security)
const hashedOtp = await bcrypt.hash(otp, 10);
// Store OTP in user record
await user.update({
reset_otp: hashedOtp,
reset_otp_expires_at: new Date(Date.now() + 10 * 60 * 1000), // valid for 10 mins
});
// Send OTP email
await sendEmail(
registered_email,
"Password Reset OTP",
`<p>Dear ${user.name},</p><p>Your OTP for password reset is <b>${otp}</b>. It is valid for 10 minutes.</p>`
);
}else{
const adminUser = await user.findOne({where: { email: registered_email },});
if (!adminUser)
return res.status(404).json({ status: "failed", message: "Admin user not found" });
// Generate 6-digit OTP
const otp = Math.floor(100000 + Math.random() * 900000).toString();
// Hash OTP before saving (security)
const hashedOtp = await bcrypt.hash(otp, 10);
// Store OTP in user record
await adminUser.update({
reset_otp: hashedOtp,
reset_otp_expires_at: new Date(Date.now() + 10 * 60 * 1000), // valid for 10 mins
});
// Send OTP email
await sendEmail(
registered_email,
"Password Reset OTP",
`<p>Dear ${adminUser.name},</p><p>Your OTP for password reset is <b>${otp}</b>. It is valid for 10 minutes.</p>`
);
}
logger.info(`OTP sent to ${registered_email}`);
return res.status(200).json({
status: "success",
message: "OTP sent successfully to registered email",
});
} catch (err) {
logger.error(`Error in forgotPasswordRequestOTP: ${err.message}`);
return res.status(500).json({ status: "failed", message: err.message });
}
};
exports.forgotPasswordVerifyOTP = async (req, res) => {
try {
const { user_type , registered_email, otp, password, confirm_password } = req.body;
if (!registered_email || !otp || !password || !confirm_password)
return res.status(400).json({ status: "failed", message: "All fields are required" });
if (password !== confirm_password)
return res.status(400).json({ status: "failed", message: "Passwords do not match" });
if(user_type == 'establishment_user')
{
const user = await EstablishmentUser.findOne({ where: { email: registered_email } });
if (!user || !user.reset_otp)
return res.status(404).json({ status: "failed", message: "verification code not found or invalid user" });
// Check OTP expiry
if (new Date() > new Date(user.reset_otp_expires_at))
return res.status(400).json({ status: "failed", message: "Verification code has expired. Please request a new one" });
// Compare OTP
const isOtpValid = await bcrypt.compare(otp, user.reset_otp);
if (!isOtpValid)
return res.status(400).json({ status: "failed", message: "Invalid verification code" });
// Update password
const hashedPassword = await bcrypt.hash(password, 10);
await user.update({
password: hashedPassword,
reset_otp: null,
reset_otp_expires_at: null,
});
}else{
const adminUser = await user.findOne({ where: { email: registered_email } });
if (!adminUser || !adminUser.reset_otp)
return res.status(404).json({ status: "failed", message: "verification code not found or invalid user" });
// Check OTP expiry
if (new Date() > new Date(adminUser.reset_otp_expires_at))
return res.status(400).json({ status: "failed", message: "Verification code has expired. Please request a new one" });
// Compare OTP
const isOtpValid = await bcrypt.compare(otp, adminUser.reset_otp);
if (!isOtpValid)
return res.status(400).json({ status: "failed", message: "Invalid verification code" });
// Update password
const hashedPassword = await bcrypt.hash(password, 10);
await adminUser.update({
password: hashedPassword,
reset_otp: null,
reset_otp_expires_at: null,
});
}
logger.info(`Password reset successful for user=${registered_email}`);
return res.status(200).json({
status: "success",
message: "Password reset successfully",
});
} catch (err) {
logger.error(`Error in forgotPasswordVerifyOTP: ${err.message}`);
return res.status(500).json({ status: "failed", message: err.message });
}
};
const GENERIC_ERROR_MSG =
"Invalid data. Please check the instructions given and upload again.";
exports.establishmentBulkUpload = async (req, res) => {
try {
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")) {
fs.unlinkSync(filePath);
return res.status(400).send({
status: "failed",
message: "Invalid file type. Only CSV allowed."
});
}
const rows = [];
// -------------------------------------------------------
// READ CSV AND SKIP EMPTY ROWS
// -------------------------------------------------------
await new Promise((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(csv())
.on("data", (rawRow) => {
const row = {};
// Normalize headers
for (const key in rawRow) {
const normalizedKey = key.replace(/[\s\W]+/g, "_")
.trim()
.toLowerCase();
row[normalizedKey] = rawRow[key]?.trim() || "";
}
const cleanedValues = Object.values(row).map(v =>(v || "").replace(/\s+/g, "").trim());
const isEmpty = cleanedValues.every(v => v === "");
if (isEmpty) return;
rows.push(row);
})
.on("end", resolve)
.on("error", reject);
});
if (rows.length === 0) {
fs.unlinkSync(filePath);
return res.status(400).send({
status: "failed",
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(", ") }
]
});
}
// -------------------------------------------------------
// LOAD REFERENCE TABLES
// -------------------------------------------------------
const emirates = await Emirate.findAll({ attributes: ["id", "name"] });
const emirateMap = {};
emirates.forEach(e => {
emirateMap[e.name.trim().toLowerCase()] = e.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"]
});
const existingEstIdSet = new Set(existingEsts.map(e => e.establishment_code));
const existingFactorySet = new Set(existingEsts.map(e => e.factory_name.toLowerCase()));
const existingEmailSet = new Set(existingEsts.map(e => e.establishment_contact_email.toLowerCase()));
// -------------------------------------------------------
// FILE-LEVEL DUPLICATE TRACKING
// -------------------------------------------------------
const errors = [];
const fileEstSet = new Set();
const fileFactorySet = new Set();
const fileEmailSet = new Set();
const prepared = [];
// -------------------------------------------------------
// VALIDATE EACH ROW
// -------------------------------------------------------
for (let i = 0; i < rows.length; i++) {
const r = rows[i];
const rowNum = i + 2;
const est = r.establishment_id;
const factory = r.factory_name;
const email = r.email;
const emirate = r.emirate;
const employment = r.total_employment;
// Required validation
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({
row: rowNum,
error: `Missing Data: ${missingFields.join(", ")} ${missingFields.length === 1 ? "is" : "are"} required.`
});
continue;
}
if (isNaN(employment) || !/^\d+$/.test(employment)) {
errors.push({
row: rowNum,
error: "Total Employment must be numeric."
});
continue;
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
errors.push({
row: rowNum,
error: "Invalid Email: Please check the Email address."
});
continue;
}
// FILE-level duplicates
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;
}
if (fileFactorySet.has(factoryKey)) {
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;
}
fileEstSet.add(estKey);
fileFactorySet.add(factoryKey);
fileEmailSet.add(emailKey);
// DB-level duplicates
if (existingEstIdSet.has(estKey)) {
errors.push({ row: rowNum, error: `Establishment ID already exists: ${est}` });
continue;
}
if (existingFactorySet.has(factoryKey)) {
errors.push({ row: rowNum, error: `Factory Name already exists: ${factory}` });
continue;
}
if (existingEmailSet.has(emailKey)) {
errors.push({ row: rowNum, error: `Email already exists: ${email}` });
continue;
}
// Emirate validation
const emirateId = emirateMap[emirate.toLowerCase()];
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({
row: rowNum,
error: `Duplicate HS Code(s) in the same row: ${[...new Set(rowDuplicates)].join(", ")}`
});
continue;
}
// DB-level HS Code validation
const invalid = [];
for (const raw of hsRaw) {
const cleaned = normalizeHS(raw);
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;
}
prepared.push({
est,
factory,
email,
emirateId,
employment: parseInt(employment),
productIds: cleaned.map(c => productMap[c]),
rowNum
});
}
// -------------------------------------------------------
// STOP IF ERRORS
// -------------------------------------------------------
if (errors.length > 0) {
logger.error("Establishment Upload Failed: " + JSON.stringify(errors));
fs.unlinkSync(filePath);
return res.status(400).send({
status: "failed",
message: "Invalid data. Please check the instructions given and upload again.",
errors
});
}
// -------------------------------------------------------
// INSERT VALID RECORDS
// -------------------------------------------------------
for (const rec of prepared) {
const est = await Establishment.create({
establishment_code: rec.est,
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()
});
await EstablishmentProduct.bulkCreate(
rec.productIds.map(pid => ({
establishment_id: est.id,
product_id: pid,
created_by: req.user.id,
created_at: new Date(),
is_active: true
}))
);
}
fs.unlinkSync(filePath);
return res.status(200).send({
status: "success",
message: `${prepared.length} establishments inserted successfully.`,
summary: {
total_records: rows.length,
imported: prepared.length,
errors: []
}
});
} 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) => {
// 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 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 = [];
// 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
// // Map to product IDs
// const mappedProducts = cleanedHs
// .map((c) => productMap[c])
// .filter(Boolean);
// // Validate mapping
// if (mappedProducts.length === 0) {
// errors.push({
// row: rowNumber,
// error: "None of the provided HS Codes match existing products.",
// hs_codes: cleanedHs
// });
// continue;
// }
// if (mappedProducts.length === 0) {
// errors.push({ row: rowNum, error: "No HS codes matched product list" });
// continue;
// }
// // Validation: at least one HS must match existing product
// if (mappedProducts.length === 0) {
// 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 });
// }
// };