diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index df088c29..17f6495c 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -340,6 +340,80 @@ exports.createTeamMember = async (req, 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 +// const departments = await Deparments.find({ +// officeName: nameRegex, +// city: cityRegex, +// }).lean(); + +// const responseData = [{ firstName: "Self" }]; + +// if (branchMatch) { +// responseData.push({ +// officeType: "branch", +// ...branchMatch +// }); +// } +// if (cityMatch) { +// responseData.push({ +// officeType: "headOffice", +// ...cityMatch +// }); +// } + +// 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; @@ -370,11 +444,17 @@ exports.getAllDepartments = async (request, reply) => { }).lean(); // 3️⃣ Departments - const departments = await Deparments.find({ + 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) { @@ -390,6 +470,7 @@ exports.getAllDepartments = async (request, reply) => { }); } + // Push modified departments responseData.push(...departments); return reply.send({ @@ -415,7 +496,6 @@ exports.getAllDepartments = async (request, reply) => { }; - exports.assignTeamMemberToQuotation = async (request, reply) => { try { const { installationId } = request.params;