From 313f0d12e0920e52c06eab187b6b1b70dc548cd9 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Mon, 11 Aug 2025 12:05:50 +0530 Subject: [PATCH] Get the zones by city and office --- src/controllers/departmentController.js | 177 +++++++++++++++++------- src/routes/departmentRoute.js | 51 +++++-- 2 files changed, 160 insertions(+), 68 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index f58a1697..f39f9016 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -934,68 +934,139 @@ 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([ diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index e4e59d81..2aa55a08 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -428,22 +428,43 @@ module.exports = function (fastify, opts, next) { 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({ - method: "GET", - url: "/api/zonebasedcity/:city", - schema: { - tags: ["Department"], - description: "Get the zones by city", - summary: "Get the zones by city", - params: { - type: "object", - properties: { - city: { type: "string" }, - }, - }, - }, - handler:departmentController.getZonesByCity - }); + method: "GET", + url: "/api/zonebasedcity/:city/:officeName", + schema: { + tags: ["Department"], + description: "Get the zones by city and office", + summary: "Get the zones by city and office", + params: { + type: "object", + properties: { + city: { type: "string" }, + officeName: { type: "string" } + }, + required: ["city", "officeName"] + } + }, + handler: departmentController.getZonesByCityAndOffice // ✅ Match function name +}); fastify.route({ method: "GET",