From a95e8e3d7ef44fff6306236a6a0a010f18c2a431 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 31 Jan 2025 12:45:49 +0530 Subject: [PATCH] trim the city values --- src/controllers/departmentController.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index c10e393f..9a4489fe 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -699,19 +699,20 @@ exports.getZonebasedLocations = async (req, reply) => { try { const result = await City.aggregate([ { - $unwind: "$city" // Convert location array into separate documents + $project: { + city: { $trim: { input: "$city" } }, // Trim city field in DB + zone: 1 // Keep zone field + } }, { $match: { - city: { $regex: `^${city}$`, $options: "i" }, // Match city case-insensitively + city: { $regex: `^${city.trim()}$`, $options: "i" }, // Trim & case-insensitive }, }, { $group: { - _id: { - $toUpper: { $trim: { input: "$city" } } // Normalize city name - }, - zones: { $addToSet: "$zone" } // Collect unique zones + _id: { $toUpper: "$city" }, // Normalize city name + zones: { $addToSet: "$zone" }, // Collect unique zones }, }, { @@ -734,17 +735,18 @@ exports.getZonebasedLocations = async (req, reply) => { try { const { city } = req.params; - if (!city) { + if (!city || city.trim() === "") { return reply.status(400).send({ message: "City is required." }); } - const zones = await getZonesByCitys(city); + const zones = await getZonesByCitys(city.trim()); // Trim input reply.send({ status_code: 200, data: zones }); } catch (err) { reply.status(500).send({ message: err.message }); } }; + const getDepartmentsByName = async (departmentName) => { try {