From b65c5cfa1d2032d070e50a65f37231093ec409c0 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 7 Aug 2025 16:27:27 +0530 Subject: [PATCH] Chnages and ALL filed added cities --- src/controllers/departmentController.js | 129 ++++++++++++++++++------ src/routes/departmentRoute.js | 4 +- 2 files changed, 102 insertions(+), 31 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index a1ca1f30..e1351fd0 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -262,7 +262,7 @@ exports.getallCompanyNames = async (req, reply) => { .select("officeName -_id") // Select only officeName and exclude _id .exec() .then((docs) => { - const officeNames = docs.map((doc) => doc.officeName); // Extract only officeName values + const officeNames = ["ALL", ...docs.map((doc) => doc.officeName)]; // Prepend "ALL" reply.send({ status_code: 200, data: officeNames, count: officeNames.length }); }) .catch((err) => { @@ -1237,34 +1237,105 @@ const getDepartmentsByName = async (officeName, city, departmentName) => { }; - exports.getCitiesByOfficeName = async (req, reply) => { - try { - let { officeName } = req.params; - - // Trim and normalize spaces - officeName = officeName.trim().replace(/\s+/g, ' '); // Replace multiple spaces with one - const regexOfficeName = new RegExp(officeName.replace(/\s+/g, '\\s*'), "i"); - - // Debugging: Check all available office names in DB - console.log("All Cities with Office Name:", await City.find().select("officeName city").lean()); - console.log("All Branches with Office Name:", await Branch.find().select("officeName city").lean()); - - // Query both collections with case-insensitive regex - const cityResults = await City.find({ officeName: { $regex: officeName, $options: "i" } }).select("city -_id").lean(); - const branchResults = await Branch.find({ officeName: { $regex: officeName, $options: "i" } }).select("city -_id").lean(); - - // Extract and merge unique city names - const cityNames = [...new Set([...cityResults.map(c => c.city), ...branchResults.map(b => b.city)])]; - - console.log("Final City Results:", cityNames); - - reply.send({ status_code: 200, data: cityNames }); - } catch (err) { - console.error("Error fetching cities:", err); - reply.send({ error: err.message }); +// exports.getCitiesByOfficeName = async (req, reply) => { +// try { +// let { officeName } = req.params; + +// if (!officeName) { +// return reply.code(400).send({ error: "officeName is required" }); +// } + +// // Split by comma and normalize names +// let officeNames = officeName.split(',').map(name => +// name.trim().replace(/\s+/g, ' ') +// ); + +// // Handle "All" — fetch all cities from both collections +// if (officeNames.includes('All')) { +// const allCityDocs = await City.find().select("city -_id").lean(); +// const allBranchDocs = await Branch.find().select("city -_id").lean(); + +// const allCities = [...new Set([ +// ...allCityDocs.map(doc => doc.city), +// ...allBranchDocs.map(doc => doc.city), +// ])]; + +// return reply.send({ status_code: 200, data: allCities }); +// } + +// // Build regex conditions for each office name +// const regexConditions = officeNames.map(name => ({ +// officeName: { $regex: new RegExp(name.replace(/\s+/g, '\\s*'), 'i') } +// })); + +// // Query both collections +// const cityResults = await City.find({ $or: regexConditions }).select("city -_id").lean(); +// const branchResults = await Branch.find({ $or: regexConditions }).select("city -_id").lean(); + +// // Extract and merge unique city names +// const cityNames = [...new Set([ +// ...cityResults.map(c => c.city), +// ...branchResults.map(b => b.city) +// ])]; + +// reply.send({ status_code: 200, data: cityNames }); +// } catch (err) { +// console.error("Error fetching cities:", err); +// reply.send({ error: err.message }); +// } +// }; + +exports.getCitiesByOfficeName = async (req, reply) => { + try { + let { officeName } = req.params; + + if (!officeName) { + return reply.code(400).send({ error: "officeName is required" }); } - }; - + + // Split by comma and normalize names + let officeNames = officeName.split(',').map(name => + name.trim().replace(/\s+/g, ' ') + ); + + // Handle "All" — fetch all cities from both collections + if (officeNames.includes('All')) { + const allCityDocs = await City.find().select("city -_id").lean(); + const allBranchDocs = await Branch.find().select("city -_id").lean(); + + const allCities = [...new Set([ + ...allCityDocs.map(doc => doc.city), + ...allBranchDocs.map(doc => doc.city), + ])]; + + allCities.unshift("ALL"); // ✅ Add "ALL" at the beginning + + return reply.send({ status_code: 200, data: allCities }); + } + + // Build regex conditions for each office name + const regexConditions = officeNames.map(name => ({ + officeName: { $regex: new RegExp(name.replace(/\s+/g, '\\s*'), 'i') } + })); + + // Query both collections + const cityResults = await City.find({ $or: regexConditions }).select("city -_id").lean(); + const branchResults = await Branch.find({ $or: regexConditions }).select("city -_id").lean(); + + // Extract and merge unique city names + const cityNames = [...new Set([ + ...cityResults.map(c => c.city), + ...branchResults.map(b => b.city) + ])]; + + cityNames.unshift("ALL"); // ✅ Add "ALL" at the beginning + + reply.send({ status_code: 200, data: cityNames }); + } catch (err) { + console.error("Error fetching cities:", err); + reply.send({ error: err.message }); + } +}; // Helper function to fetch department names by city const getDepartmentNamesByCity = async (city) => { @@ -1274,7 +1345,7 @@ const getDepartmentsByName = async (officeName, city, departmentName) => { const query = { $or: [ { city: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } }, - { officeName: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } } + // { officeName: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } } ] }; diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index e4e59d81..3ad4efda 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -676,13 +676,13 @@ module.exports = function (fastify, opts, next) { type: "object", properties: { city: { type: "string" }, - officeName: { type: "string" }, + // officeName: { type: "string" }, }, required: ["city"], }, }, - handler: departmentController.getOffices, + handler: departmentController.getDepartmentsByCity, }); fastify.route({