From 71d59c0aa24dcd96f70dfc8a0bdf431cf0543270 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 4 Feb 2025 11:46:36 +0530 Subject: [PATCH] trim the ofice name --- src/controllers/departmentController.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index f0ea51ff..886cdec5 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -1000,22 +1000,25 @@ exports.getZonebasedLocations = async (req, reply) => { exports.getCitiesByOfficeName = async (req, reply) => { try { - const { officeName } = req.params; + let { officeName } = req.params; - // Case-insensitive search - const regexOfficeName = new RegExp(`^${officeName}$`, "i"); + // 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"); - // Query both collections - const cityResults = await City.find({ officeName: regexOfficeName }).select("city -_id").lean(); - const branchResults = await Branch.find({ officeName: regexOfficeName }).select("city -_id").lean(); + // 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)])]; - // Debugging - console.log("City Results:", cityResults); - console.log("Branch Results:", branchResults); - + console.log("Final City Results:", cityNames); + reply.send({ status_code: 200, data: cityNames }); } catch (err) { console.error("Error fetching cities:", err);