zones sorting

master^2
Bhaskar 8 months ago
parent 2cf3a53602
commit 47f4522793

@ -719,7 +719,6 @@ exports.getZonebasedLocations = async (req, reply) => {
} }
}; };
const getZonesByCitys = async (city) => { const getZonesByCitys = async (city) => {
try { try {
const result = await City.aggregate([ const result = await City.aggregate([
@ -732,30 +731,36 @@ exports.getZonebasedLocations = async (req, reply) => {
{ {
$match: { $match: {
city: { $regex: `^${city.trim()}$`, $options: "i" }, // Trim & case-insensitive city: { $regex: `^${city.trim()}$`, $options: "i" }, // Trim & case-insensitive
}, }
}, },
{ {
$group: { $group: {
_id: { $toUpper: "$city" }, // Normalize city name _id: { $toUpper: "$city" }, // Normalize city name
zones: { $addToSet: "$zone" }, // Collect unique zones zones: { $addToSet: "$zone" } // Collect unique zones
}, }
}, },
{ {
$project: { $project: {
_id: 0, // Exclude _id _id: 0, // Exclude _id
city: "$_id", // Return city name 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; return result;
} catch (err) { } catch (err) {
console.error(err); console.error("Error fetching zones:", err); // Detailed error logging
throw new Error("Error fetching zones."); throw new Error("Error fetching zones.");
} }
}; };
exports.getZonesByCity = async (req, reply) => { exports.getZonesByCity = async (req, reply) => {
try { try {
const { city } = req.params; const { city } = req.params;
@ -765,6 +770,11 @@ exports.getZonebasedLocations = async (req, reply) => {
} }
const zones = await getZonesByCitys(city.trim()); // Trim input 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 }); reply.send({ status_code: 200, data: zones });
} catch (err) { } catch (err) {
reply.status(500).send({ message: err.message }); reply.status(500).send({ message: err.message });
@ -772,6 +782,7 @@ exports.getZonebasedLocations = async (req, reply) => {
}; };
const getDepartmentsByName = async (departmentName) => { const getDepartmentsByName = async (departmentName) => {
try { try {

Loading…
Cancel
Save