Get the zones by city and office

master^2
Bhaskar 2 months ago
parent 4e7dc19116
commit 313f0d12e0

@ -934,38 +934,110 @@ exports.getZonebasedLocations = async (req, reply) => {
} }
}; };
const getZonesByCitys = async (city) => { // 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));
// });
// 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;
// if (!city || city.trim() === "") {
// 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." });
// }
// reply.send({ status_code: 200, data: zones });
// } catch (err) {
// reply.status(500).send({ message: err.message });
// }
// };
const getZonesByCityAndOffice = async (city, officeName) => {
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
zone: 1 // Keep zone field officeName: { $trim: { input: "$officeName" } }, // Trim officeName
zone: 1
} }
}, },
{ {
$match: { $match: {
city: { $regex: `^${city.trim()}$`, $options: "i" }, // Trim & case-insensitive ...(city && city !== "ALL" ? { city: { $regex: `^${city.trim()}$`, $options: "i" } } : {}),
...(officeName && officeName !== "ALL" ? { officeName: { $regex: `^${officeName.trim()}$`, $options: "i" } } : {})
} }
}, },
{ {
$group: { $group: {
_id: { $toUpper: "$city" }, // Normalize city name _id: {
zones: { $addToSet: "$zone" } // Collect unique zones city: { $toUpper: "$city" },
officeName: { $toUpper: "$officeName" }
},
zones: { $addToSet: "$zone" }
} }
}, },
{ {
$project: { $project: {
_id: 0, // Exclude _id _id: 0,
city: "$_id", // Return city name city: "$_id.city",
zones: 1 // Return collected zones officeName: "$_id.officeName",
zones: 1
} }
} }
]); ]);
// Add "ALL" to the zones array and sort it // Add "ALL" to zones and sort
result.forEach(item => { result.forEach(item => {
item.zones = ["ALL", ...new Set(item.zones)].sort((a, b) => (a === "ALL" ? -1 : a - b)); item.zones = ["ALL", ...new Set(item.zones)].sort((a, b) =>
a === "ALL" ? -1 : b.localeCompare(a)
);
}); });
return result; return result;
@ -973,28 +1045,27 @@ exports.getZonebasedLocations = async (req, reply) => {
console.error("Error fetching zones:", err); console.error("Error fetching zones:", err);
throw new Error("Error fetching zones."); throw new Error("Error fetching zones.");
} }
}; };
exports.getZonesByCity = async (req, reply) => { exports.getZonesByCityAndOffice = async (req, reply) => {
try { try {
const { city } = req.params; const { city, officeName } = req.params;
if (!city || city.trim() === "") { if (!city || city.trim() === "" || !officeName || officeName.trim() === "") {
return reply.status(400).send({ message: "City is required." }); return reply.status(400).send({ message: "City and Office Name are required." });
} }
const zones = await getZonesByCitys(city.trim()); // Trim input const zones = await getZonesByCityAndOffice(city.trim(), officeName.trim());
if (zones.length === 0) { if (zones.length === 0) {
return reply.status(404).send({ message: "No zones found for the specified city." }); return reply.status(404).send({ message: "No zones found for the specified city and office." });
} }
reply.send({ status_code: 200, data: zones }); reply.send({ status_code: 200, data: zones });
} catch (err) { } catch (err) {
reply.status(500).send({ message: err.message }); 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.getZonesByCityAndOffice // ✅ Match function name
handler:departmentController.getZonesByCity });
});
fastify.route({ fastify.route({
method: "GET", method: "GET",

Loading…
Cancel
Save