ashok 2 months ago
commit 31b1c780e9

@ -934,67 +934,139 @@ exports.getZonebasedLocations = async (req, reply) => {
} }
}; };
const getZonesByCitys = async (city) => { // const getZonesByCitys = async (city) => {
try { // try {
const result = await Zone.aggregate([ // const result = await Zone.aggregate([
{ // {
$project: { // $project: {
city: { $trim: { input: "$city" } }, // Trim city field in DB // city: { $trim: { input: "$city" } }, // Trim city field in DB
zone: 1 // Keep zone field // zone: 1 // Keep zone field
} // }
}, // },
{ // {
$match: { // $match: {
city: { $regex: `^${city.trim()}$`, $options: "i" }, // Trim & case-insensitive // city: { $regex: `^${city.trim()}$`, $options: "i" }, // Trim & case-insensitive
} // }
}, // },
{ // {
$group: { // $group: {
_id: { $toUpper: "$city" }, // Normalize city name // _id: { $toUpper: "$city" }, // Normalize city name
zones: { $addToSet: "$zone" } // Collect unique zones // zones: { $addToSet: "$zone" } // Collect unique zones
} // }
}, // },
{ // {
$project: { // $project: {
_id: 0, // Exclude _id // _id: 0, // Exclude _id
city: "$_id", // Return city name // city: "$_id", // Return city name
zones: 1 // Return collected zones // 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 // return result;
result.forEach(item => { // } catch (err) {
item.zones = ["ALL", ...new Set(item.zones)].sort((a, b) => (a === "ALL" ? -1 : a - b)); // 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) => { // if (!city || city.trim() === "") {
try { // return reply.status(400).send({ message: "City is required." });
const { city } = req.params; // }
if (!city || city.trim() === "") { // const zones = await getZonesByCitys(city.trim()); // Trim input
return reply.status(400).send({ message: "City is required." });
}
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) { // reply.send({ status_code: 200, data: zones });
return reply.status(404).send({ message: "No zones found for the specified city." }); // } 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
}
} }
]);
reply.send({ status_code: 200, data: zones }); // Add "ALL" to zones and sort
} catch (err) { result.forEach(item => {
reply.status(500).send({ message: err.message }); 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) => { const getAreasByCitys = async (city) => {
try { try {

@ -428,22 +428,43 @@ module.exports = function (fastify, opts, next) {
handler:departmentController.getLocationsByZone handler:departmentController.getLocationsByZone
}); });
// fastify.route({
// method: "GET",
// url: "/api/zonebasedcity/:city/:officeName",
// schema: {
// tags: ["Department"],
// description: "Get the zones by city",
// summary: "Get the zones by city",
// params: {
// type: "object",
// properties: {
// city: { type: "string" },
// officeName: { type: "string" },
// },
// },
// },
// handler:departmentController.getZonesByCity
// });
fastify.route({ fastify.route({
method: "GET", method: "GET",
url: "/api/zonebasedcity/:city", url: "/api/zonebasedcity/:city/:officeName",
schema: { schema: {
tags: ["Department"], tags: ["Department"],
description: "Get the zones by city", description: "Get the zones by city and office",
summary: "Get the zones by city", summary: "Get the zones by city and office",
params: { params: {
type: "object", type: "object",
properties: { properties: {
city: { type: "string" }, city: { type: "string" },
}, officeName: { type: "string" }
}, },
}, required: ["city", "officeName"]
handler:departmentController.getZonesByCity }
}); },
handler: departmentController.getZonesByCityAndOffice // ✅ Match function name
});
fastify.route({ fastify.route({
method: "GET", method: "GET",

Loading…
Cancel
Save