master^2
Bhaskar 9 months ago
parent 56ee1db55b
commit c34c3b41c9

@ -631,39 +631,42 @@ exports.addDepartment = async (request, reply) => {
} }
}; };
const getLocationsByZone = async (zone) => { const getLocationsByZone = async (zone) => {
try { try {
const result = await City.aggregate([ const result = await City.aggregate([
{ {
$match: { $match: {
zone: { $regex: `^${zone}$`, $options: "i" }, // Case-insensitive match for the zone zone: { $regex: `^${zone}$`, $options: "i" }, // Case-insensitive match for the zone
},
}, },
{ },
$unwind: "$location" // Unwind the location field if it is an array {
}, $unwind: "$location" // Unwind the location field if it is an array
{ },
$group: { {
_id: "$zone", // Group by zone $group: {
locations: { $addToSet: { $toUpper: "$location" } }, // Collect unique locations in uppercase _id: "$zone", // Group by zone
locations: {
$addToSet: {
$toUpper: { $trim: { input: "$location" } } // Convert to uppercase and trim whitespace
}
}, },
}, },
{ },
$project: { {
_id: 0, // Exclude the _id field $project: {
zone: "$_id", // Include zone _id: 0, // Exclude the _id field
locations: 1 // Just return the locations field as is zone: "$_id", // Include zone
}, locations: 1 // Return locations
}, },
]); },
]);
return result;
} catch (err) { return result;
console.error(err); } catch (err) {
throw new Error("Error fetching locations."); console.error(err);
} throw new Error("Error fetching locations.");
}; }
};

Loading…
Cancel
Save