From 061c02a162873f99fd2a3a37d6dc71248eeda7ab Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 13 Aug 2025 11:20:57 +0530 Subject: [PATCH] city based fetch state --- src/controllers/departmentController.js | 48 +++++++++++++++++++++++++ src/routes/departmentRoute.js | 31 +++++++++++++++- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 42b699e2..5af936bf 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -1742,6 +1742,26 @@ exports.getOffices = async (req, reply) => { reply.status(500).send({ error: "Failed to fetch states" }); } }; + + exports.getAllCities = async (req, reply) => { + try { + // Fetch only the majorCities field from all documents + const docs = await IndianLocations.find({}, "majorCities").lean(); + + // Flatten the array of arrays and remove duplicates + const cities = [...new Set(docs.flatMap(doc => doc.majorCities))]; + + // Sort alphabetically (case-insensitive) + cities.sort((a, b) => a.localeCompare(b, undefined, { sensitivity: "base" })); + + reply.send(cities); + + } catch (err) { + console.error("Error fetching cities:", err); + reply.status(500).send({ error: "Failed to fetch cities" }); + } +}; + exports.getStaeBasedCites = async (request, reply) => { try { @@ -1759,4 +1779,32 @@ exports.getOffices = async (req, reply) => { }; +exports.getCitiesBasedState = async (request, reply) => { + try { + // Match the param name from the route exactly + const { majorcities } = request.params; + + if (!majorcities) { + return reply.status(400).send({ error: "majorcities param is required" }); + } + + // Case-insensitive regex match against elements in the array + const stateDoc = await IndianLocations.findOne( + { majorCities: { $regex: new RegExp(`^${majorcities.trim()}$`, "i") } }, + "state" + ).lean(); + + if (!stateDoc) { + return reply.status(404).send({ error: "City not found" }); + } + + reply.send({ state: stateDoc.state }); + + } catch (err) { + console.error("Error fetching state:", err); + reply.status(500).send({ error: "Failed to fetch state" }); + } +}; + + \ No newline at end of file diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index 755ef74b..12304d98 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -727,12 +727,23 @@ module.exports = function (fastify, opts, next) { }, handler:departmentController.getAllStates }); + + fastify.route({ + method: "GET", + url: "/api/Cities", + schema: { + tags: ["Admin"], + description: "Get the Cities", + summary: "Get the Cities", + }, + handler:departmentController.getAllCities + }); fastify.route({ method: "GET", url: "/api/states/cities/:stateName", schema: { - tags: ["Department"], + tags: ["Admin"], description: "Get the States by cities", summary: "Get the states by cites", params: { @@ -745,5 +756,23 @@ module.exports = function (fastify, opts, next) { }, handler:departmentController.getStaeBasedCites }); + + fastify.route({ + method: "GET", + url: "/api/cities/states/:majorcities", + schema: { + tags: ["Admin"], + description: "Get the Cities by state", + summary: "Get the cities by state", + params: { + type: "object", + properties: { + majorcities: { type: "string" }, + + }, + }, + }, + handler:departmentController.getCitiesBasedState + }); next(); }; \ No newline at end of file