|
|
|
@ -629,4 +629,56 @@ exports.addDepartment = async (request, reply) => {
|
|
|
|
|
} catch (err) {
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$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
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
throw new Error("Error fetching locations.");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getLocationsByZone = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { zone } = req.params; // Get zone from path params
|
|
|
|
|
|
|
|
|
|
if (!zone) {
|
|
|
|
|
return reply.status(400).send({ message: "Zone is required." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const locations = await getLocationsByZone(zone);
|
|
|
|
|
reply.send({ status_code: 200, data: locations });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|