From c34c3b41c93ef77c933cf19a701d4b39164cc887 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 29 Jan 2025 17:12:30 +0530 Subject: [PATCH 1/3] changes --- src/controllers/departmentController.js | 65 +++++++++++++------------ 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index b8e277c4..04897cc7 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -631,40 +631,43 @@ exports.addDepartment = async (request, reply) => { } }; - const getLocationsByZone = async (zone) => { - try { - const result = await City.aggregate([ - { - $match: { - zone: { $regex: `^${zone}$`, $options: "i" }, // Case-insensitive match for the zone - }, + const getLocationsByZone = async (zone) => { + try { + const result = await City.aggregate([ + { + $match: { + zone: { $regex: `^${zone}$`, $options: "i" }, // Case-insensitive match for the zone }, - { - $unwind: "$location" // Unwind the location field if it is an array + }, + { + $unwind: "$location" // Unwind the location field if it is an array + }, + { + $group: { + _id: "$zone", // Group by zone + locations: { + $addToSet: { + $toUpper: { $trim: { input: "$location" } } // Convert to uppercase and trim whitespace + } + }, }, - { - $group: { - _id: "$zone", // Group by zone - locations: { $addToSet: { $toUpper: "$location" } }, // Collect unique locations in uppercase - }, - }, - { - $project: { - _id: 0, // Exclude the _id field - zone: "$_id", // Include zone - locations: 1 // Just return the locations field as is - }, + }, + { + $project: { + _id: 0, // Exclude the _id field + zone: "$_id", // Include zone + locations: 1 // Return locations }, - ]); - - return result; - } catch (err) { - console.error(err); - throw new Error("Error fetching locations."); - } - }; - - + }, + ]); + + return result; + } catch (err) { + console.error(err); + throw new Error("Error fetching locations."); + } +}; + exports.getLocationsByZone = async (req, reply) => { From 46f30f5b397091c940032b1ec861853883557670 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 29 Jan 2025 17:17:06 +0530 Subject: [PATCH 2/3] changes --- src/routes/departmentRoute.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index cfeee179..dff9a456 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -374,8 +374,8 @@ module.exports = function (fastify, opts, next) { type: "object", required: ["city", "zone"], properties: { - zone: { type: "string" }, city: { type: "string" }, + zone: { type: "string" }, }, }, }, From 1ecd4b149c762101e28d62acf257d4f007a4b2f9 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 29 Jan 2025 17:27:02 +0530 Subject: [PATCH 3/3] based on city to get the zones --- src/controllers/departmentController.js | 51 +++++++++++++++++++++++++ src/routes/departmentRoute.js | 17 ++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 04897cc7..4719cbb5 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -684,4 +684,55 @@ exports.addDepartment = async (request, reply) => { reply.status(500).send({ message: err.message }); } }; + + + 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 }); + } + }; \ No newline at end of file diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index dff9a456..3c483985 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -399,6 +399,21 @@ module.exports = function (fastify, opts, next) { handler:departmentController.getLocationsByZone }); - + 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 + }); next(); }; \ No newline at end of file