diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 14df550a..bfe1aa40 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -719,7 +719,6 @@ exports.getZonebasedLocations = async (req, reply) => { } }; - const getZonesByCitys = async (city) => { try { const result = await City.aggregate([ @@ -732,30 +731,36 @@ exports.getZonebasedLocations = async (req, reply) => { { $match: { city: { $regex: `^${city.trim()}$`, $options: "i" }, // Trim & case-insensitive - }, + } }, { $group: { _id: { $toUpper: "$city" }, // Normalize city name - zones: { $addToSet: "$zone" }, // Collect unique zones - }, + zones: { $addToSet: "$zone" } // Collect unique zones + } }, { $project: { _id: 0, // Exclude _id city: "$_id", // Return city name - zones: 1, // Return collected zones - }, - }, + zones: 1 // Return collected zones (no sorting in aggregation) + } + } ]); + // Sort the zones array in ascending order + result.forEach(item => { + item.zones.sort((a, b) => a - b); // Ensure zones are sorted numerically + }); + return result; } catch (err) { - console.error(err); + console.error("Error fetching zones:", err); // Detailed error logging throw new Error("Error fetching zones."); } }; + exports.getZonesByCity = async (req, reply) => { try { const { city } = req.params; @@ -765,6 +770,11 @@ exports.getZonebasedLocations = async (req, reply) => { } const zones = await getZonesByCitys(city.trim()); // Trim input + + if (zones.length === 0) { + return reply.status(404).send({ message: "No zones found for the specified city." }); + } + reply.send({ status_code: 200, data: zones }); } catch (err) { reply.status(500).send({ message: err.message }); @@ -772,6 +782,7 @@ exports.getZonebasedLocations = async (req, reply) => { }; + const getDepartmentsByName = async (departmentName) => { try {