diff --git a/app/controllers/establishment.controller.js b/app/controllers/establishment.controller.js index 7aed165..d98228b 100644 --- a/app/controllers/establishment.controller.js +++ b/app/controllers/establishment.controller.js @@ -424,7 +424,6 @@ exports.getAllEstablishments = async (req, res) => { { header: "Status", key: "status", width: 15 }, ]; - // Format data data.forEach((item) => { worksheet.addRow({ @@ -1770,6 +1769,53 @@ exports.establishmentBulkUpload = async (req, res) => { const r = p.r; const cityTownValue = r.city_town ? r.city_town.trim().toLowerCase() : ""; + const uploadedEmails = prepared.map(p => + p.email.trim().toLowerCase() + ); + + // fetch once + const [adminUsers, establishmentUsers] = await Promise.all([ + User.findAll({ + where: { email: uploadedEmails, is_active: true }, + attributes: ["email"] + }), + EstablishmentUser.findAll({ + where: { email: uploadedEmails, is_active: true }, + attributes: ["email"] + }) + ]); + + const adminEmailSet = new Set( + adminUsers.map(u => u.email.toLowerCase()) + ); + + const establishmentEmailSet = new Set( + establishmentUsers.map(u => u.email.toLowerCase()) + ); + + // collect errors + const emailErrors = prepared + .map(p => { + const email = p.email.trim().toLowerCase(); + + return establishmentEmailSet.has(email) + ? { row: p.rowNum, error: "Email address already exists in company profile" } + : adminEmailSet.has(email) + ? { row: p.rowNum, error: "Email address is already registered in the admin users" } + : null; + }) + .filter(Boolean); + + // stop upload if any error found + if (emailErrors.length > 0) { + deleteFileSecure(sanitizedPath); + return res.status(409).send({ + status: "failed", + message: "Email validation failed", + errors: emailErrors + }); + } + if (cityTownValue) { const id = cityTownMap[cityTownValue]; if (!id) { @@ -1792,23 +1838,23 @@ exports.establishmentBulkUpload = async (req, res) => { const p = prepared[k]; const r = p.r; - const existEstablishmentUser = await EstablishmentUser.findOne({ where: { email:p.email, is_active: true } }); - const existingAdminUser = await User.findOne({ where: { email:p.email, is_active: true } }); - if (existEstablishmentUser) { - logger.info(`Email already exists in company profile: ${p.email}`); - return res.status(409).send({ - status: "failed", - message: "This email address is already exists in company profile." - }); - } + // const existEstablishmentUser = await EstablishmentUser.findOne({ where: { email:p.email, is_active: true } }); + // const existingAdminUser = await User.findOne({ where: { email:p.email, is_active: true } }); + // if (existEstablishmentUser) { + // logger.info(`Email already exists in company profile: ${p.email}`); + // return res.status(409).send({ + // status: "failed", + // message: "This email address is already exists in company profile." + // }); + // } - if (existingAdminUser) { - logger.info(`Email already exists in Admin Users: ${p.email}`); - return res.status(409).send({ - status: "failed", - message: "This email address is already registered in the admin users." - }); - } + // if (existingAdminUser) { + // logger.info(`Email already exists in Admin Users: ${p.email}`); + // return res.status(409).send({ + // status: "failed", + // message: "This email address is already registered in the admin users." + // }); + // } const est = await Establishment.create({ establishment_code: p.est,