|
|
|
@ -645,14 +645,18 @@ exports.addDepartment = async (request, reply) => {
|
|
|
|
|
{
|
|
|
|
|
$group: {
|
|
|
|
|
_id: "$zone", // Group by zone
|
|
|
|
|
locations: { $addToSet: { $toUpper: "$location" } }, // Collect unique locations in uppercase
|
|
|
|
|
locations: {
|
|
|
|
|
$addToSet: {
|
|
|
|
|
$toUpper: { $trim: { input: "$location" } } // Convert to uppercase and trim whitespace
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
|
_id: 0, // Exclude the _id field
|
|
|
|
|
zone: "$_id", // Include zone
|
|
|
|
|
locations: 1 // Just return the locations field as is
|
|
|
|
|
locations: 1 // Return locations
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
@ -666,7 +670,6 @@ exports.addDepartment = async (request, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getLocationsByZone = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { zone } = req.params; // Get zone from path params
|
|
|
|
@ -682,3 +685,54 @@ exports.addDepartment = async (request, reply) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getZonesByCitys = async (city) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await City.aggregate([
|
|
|
|
|
{
|
|
|
|
|
$unwind: "$city" // Convert location array into separate documents
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$match: {
|
|
|
|
|
city: { $regex: `^${city}$`, $options: "i" }, // Match city case-insensitively
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$group: {
|
|
|
|
|
_id: {
|
|
|
|
|
$toUpper: { $trim: { input: "$city" } } // Normalize city name
|
|
|
|
|
},
|
|
|
|
|
zones: { $addToSet: "$zone" } // Collect unique zones
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
|
_id: 0, // Exclude _id
|
|
|
|
|
city: "$_id", // Return city name
|
|
|
|
|
zones: 1, // Return collected zones
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
throw new Error("Error fetching zones.");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.getZonesByCity = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { city } = req.params;
|
|
|
|
|
|
|
|
|
|
if (!city) {
|
|
|
|
|
return reply.status(400).send({ message: "City is required." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const zones = await getZonesByCitys(city);
|
|
|
|
|
reply.send({ status_code: 200, data: zones });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|