From 314ed6cb41af9be31ea8f4711d1e7e36b8976067 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 29 Aug 2025 16:29:39 +0530 Subject: [PATCH] chnages on Get all department details fetch the managers list --- src/controllers/installationController.js | 142 ++++++++++++++++++---- 1 file changed, 116 insertions(+), 26 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index e6ad8d01..e2a6a13b 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -460,6 +460,87 @@ exports.getTeamMembers = async (request, reply) => { +// exports.getAllDepartments = async (request, reply) => { +// try { +// const { officeName, city } = request.params; + +// if (!officeName || !city) { +// return reply.status(400).send({ +// simplydata: { +// error: true, +// message: "officeName and city are required in path params", +// }, +// }); +// } + +// // Case-insensitive regex without start/end anchors to avoid trailing space issues +// const nameRegex = new RegExp(officeName.trim().replace(/\s+/g, "\\s*"), "i"); +// const cityRegex = new RegExp(city.trim().replace(/\s+/g, "\\s*"), "i"); + +// // 1️⃣ Branch match +// const branchMatch = await Branch.findOne({ +// officeName: nameRegex, +// city: cityRegex, +// }).lean(); + +// // 2️⃣ City match +// const cityMatch = await City.findOne({ +// officeName: nameRegex, +// city: cityRegex, +// }).lean(); + +// // 3️⃣ Departments +// let departments = await Deparments.find({ +// officeName: nameRegex, +// city: cityRegex, +// }).lean(); + +// // 🔹 Add nameoftheContactPerson for departments +// departments = departments.map(dep => ({ +// ...dep, +// nameoftheContactPerson: `${(dep.firstName || "").trim()} ${(dep.lastName || "").trim()}`.trim() +// })); + +// const responseData = [{ firstName: "Self" }]; + +// if (branchMatch) { +// responseData.push({ +// officeType: "branch", +// ...branchMatch +// }); +// } +// if (cityMatch) { +// responseData.push({ +// officeType: "headOffice", +// ...cityMatch +// }); +// } + +// // Push modified departments +// responseData.push(...departments); + +// return reply.send({ +// simplydata: { +// error: false, +// message: +// departments.length || branchMatch || cityMatch +// ? "Data retrieved successfully" +// : "No data found for the given officeName and city", +// data: responseData, +// }, +// }); + +// } catch (err) { +// console.error("Error fetching departments:", err); +// return reply.status(500).send({ +// simplydata: { +// error: true, +// message: "Internal server error", +// }, +// }); +// } +// }; + exports.getAllDepartments = async (request, reply) => { try { const { officeName, city } = request.params; @@ -473,63 +554,73 @@ exports.getAllDepartments = async (request, reply) => { }); } - // Case-insensitive regex without start/end anchors to avoid trailing space issues - const nameRegex = new RegExp(officeName.trim().replace(/\s+/g, "\\s*"), "i"); - const cityRegex = new RegExp(city.trim().replace(/\s+/g, "\\s*"), "i"); + // Regex for officeName (case-insensitive, flexible spaces) + const nameRegex = new RegExp( + officeName.trim().replace(/\s+/g, "\\s*"), + "i" + ); + + // If city === "ALL" → no filter, else regex + const cityFilter = + city.toUpperCase() === "ALL" + ? {} + : { city: new RegExp(city.trim().replace(/\s+/g, "\\s*"), "i") }; - // 1️⃣ Branch match - const branchMatch = await Branch.findOne({ + // 1️⃣ Branch match (all branches for that officeName) + const branchMatches = await Branch.find({ officeName: nameRegex, - city: cityRegex, + ...cityFilter, }).lean(); - // 2️⃣ City match - const cityMatch = await City.findOne({ + // 2️⃣ City (headOffice) match + const cityMatches = await City.find({ officeName: nameRegex, - city: cityRegex, + ...cityFilter, }).lean(); - // 3️⃣ Departments + // 3️⃣ Departments (all matching officeName + city filter) let departments = await Deparments.find({ officeName: nameRegex, - city: cityRegex, + ...cityFilter, }).lean(); - // 🔹 Add nameoftheContactPerson for departments - departments = departments.map(dep => ({ + // Add contactPerson to departments + departments = departments.map((dep) => ({ ...dep, - nameoftheContactPerson: `${(dep.firstName || "").trim()} ${(dep.lastName || "").trim()}`.trim() + nameoftheContactPerson: `${(dep.firstName || "").trim()} ${ + (dep.lastName || "").trim() + }`.trim(), })); + // 🔹 Build response const responseData = [{ firstName: "Self" }]; - if (branchMatch) { + branchMatches.forEach((br) => responseData.push({ officeType: "branch", - ...branchMatch - }); - } - if (cityMatch) { + ...br, + }) + ); + + cityMatches.forEach((ho) => responseData.push({ officeType: "headOffice", - ...cityMatch - }); - } + ...ho, + }) + ); - // Push modified departments responseData.push(...departments); return reply.send({ simplydata: { error: false, message: - departments.length || branchMatch || cityMatch + departments.length || branchMatches.length || cityMatches.length ? "Data retrieved successfully" : "No data found for the given officeName and city", data: responseData, }, }); - } catch (err) { console.error("Error fetching departments:", err); return reply.status(500).send({ @@ -541,7 +632,6 @@ exports.getAllDepartments = async (request, reply) => { } }; - exports.assignTeamMemberToQuotation = async (request, reply) => { try { const { installationId } = request.params;