|
|
|
@ -934,68 +934,140 @@ exports.getZonebasedLocations = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getZonesByCitys = async (city) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await Zone.aggregate([
|
|
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
|
city: { $trim: { input: "$city" } }, // Trim city field in DB
|
|
|
|
|
zone: 1 // Keep zone field
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$match: {
|
|
|
|
|
city: { $regex: `^${city.trim()}$`, $options: "i" }, // Trim & case-insensitive
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$group: {
|
|
|
|
|
_id: { $toUpper: "$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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
// const getZonesByCitys = async (city) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const result = await Zone.aggregate([
|
|
|
|
|
// {
|
|
|
|
|
// $project: {
|
|
|
|
|
// city: { $trim: { input: "$city" } }, // Trim city field in DB
|
|
|
|
|
// zone: 1 // Keep zone field
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// $match: {
|
|
|
|
|
// city: { $regex: `^${city.trim()}$`, $options: "i" }, // Trim & case-insensitive
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// $group: {
|
|
|
|
|
// _id: { $toUpper: "$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
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
|
|
// // Add "ALL" to the zones array and sort it
|
|
|
|
|
// result.forEach(item => {
|
|
|
|
|
// item.zones = ["ALL", ...new Set(item.zones)].sort((a, b) => (a === "ALL" ? -1 : a - b));
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// Add "ALL" to the zones array and sort it
|
|
|
|
|
result.forEach(item => {
|
|
|
|
|
item.zones = ["ALL", ...new Set(item.zones)].sort((a, b) => (a === "ALL" ? -1 : a - b));
|
|
|
|
|
});
|
|
|
|
|
// return result;
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
// console.error("Error fetching zones:", err);
|
|
|
|
|
// throw new Error("Error fetching zones.");
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error fetching zones:", err);
|
|
|
|
|
throw new Error("Error fetching zones.");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// exports.getZonesByCity = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { city } = req.params;
|
|
|
|
|
|
|
|
|
|
exports.getZonesByCity = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { city } = req.params;
|
|
|
|
|
// if (!city || city.trim() === "") {
|
|
|
|
|
// return reply.status(400).send({ message: "City is required." });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if (!city || city.trim() === "") {
|
|
|
|
|
return reply.status(400).send({ message: "City is required." });
|
|
|
|
|
}
|
|
|
|
|
// 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." });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
if (zones.length === 0) {
|
|
|
|
|
return reply.status(404).send({ message: "No zones found for the specified city." });
|
|
|
|
|
}
|
|
|
|
|
// reply.send({ status_code: 200, data: zones });
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
// reply.status(500).send({ message: err.message });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, data: zones });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getZonesByCityAndOffice = async (city, officeName) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await Zone.aggregate([
|
|
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
|
city: { $trim: { input: "$city" } }, // Trim city
|
|
|
|
|
officeName: { $trim: { input: "$officeName" } }, // Trim officeName
|
|
|
|
|
zone: 1
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$match: {
|
|
|
|
|
...(city && city !== "ALL" ? { city: { $regex: `^${city.trim()}$`, $options: "i" } } : {}),
|
|
|
|
|
...(officeName && officeName !== "ALL" ? { officeName: { $regex: `^${officeName.trim()}$`, $options: "i" } } : {})
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$group: {
|
|
|
|
|
_id: {
|
|
|
|
|
city: { $toUpper: "$city" },
|
|
|
|
|
officeName: { $toUpper: "$officeName" }
|
|
|
|
|
},
|
|
|
|
|
zones: { $addToSet: "$zone" }
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
|
_id: 0,
|
|
|
|
|
city: "$_id.city",
|
|
|
|
|
officeName: "$_id.officeName",
|
|
|
|
|
zones: 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Add "ALL" to zones and sort
|
|
|
|
|
result.forEach(item => {
|
|
|
|
|
item.zones = ["ALL", ...new Set(item.zones)].sort((a, b) =>
|
|
|
|
|
a === "ALL" ? -1 : b.localeCompare(a)
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error fetching zones:", err);
|
|
|
|
|
throw new Error("Error fetching zones.");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.getZonesByCityAndOffice = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { city, officeName } = req.params;
|
|
|
|
|
|
|
|
|
|
if (!city || city.trim() === "" || !officeName || officeName.trim() === "") {
|
|
|
|
|
return reply.status(400).send({ message: "City and Office Name are required." });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const zones = await getZonesByCityAndOffice(city.trim(), officeName.trim());
|
|
|
|
|
|
|
|
|
|
if (zones.length === 0) {
|
|
|
|
|
return reply.status(404).send({ message: "No zones found for the specified city and office." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, data: zones });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getAreasByCitys = async (city) => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await Zone.aggregate([
|
|
|
|
|