diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index b8e277c4..04897cc7 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -631,40 +631,43 @@ exports.addDepartment = async (request, reply) => { } }; - const getLocationsByZone = async (zone) => { - try { - const result = await City.aggregate([ - { - $match: { - zone: { $regex: `^${zone}$`, $options: "i" }, // Case-insensitive match for the zone - }, + const getLocationsByZone = async (zone) => { + try { + const result = await City.aggregate([ + { + $match: { + 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 + locations: { + $addToSet: { + $toUpper: { $trim: { input: "$location" } } // Convert to uppercase and trim whitespace + } + }, }, - { - $group: { - _id: "$zone", // Group by zone - locations: { $addToSet: { $toUpper: "$location" } }, // Collect unique locations in uppercase - }, - }, - { - $project: { - _id: 0, // Exclude the _id field - zone: "$_id", // Include zone - locations: 1 // Just return the locations field as is - }, + }, + { + $project: { + _id: 0, // Exclude the _id field + zone: "$_id", // Include zone + locations: 1 // Return locations }, - ]); - - return result; - } catch (err) { - console.error(err); - throw new Error("Error fetching locations."); - } - }; - - + }, + ]); + + return result; + } catch (err) { + console.error(err); + throw new Error("Error fetching locations."); + } +}; + exports.getLocationsByZone = async (req, reply) => {