From 9c3ece19527d9b9d5ebf97d096299ca38c653c2c Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Mon, 3 Feb 2025 17:38:44 +0530 Subject: [PATCH] get city based on office name --- src/controllers/departmentController.js | 26 +++++++++++++++++++++++++ src/routes/departmentRoute.js | 23 ++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 9b5538b9..f0ea51ff 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -996,4 +996,30 @@ exports.getZonebasedLocations = async (req, reply) => { reply.status(500).send({ message: err.message }); } }; + + + exports.getCitiesByOfficeName = async (req, reply) => { + try { + const { officeName } = req.params; + + // Case-insensitive search + const regexOfficeName = new RegExp(`^${officeName}$`, "i"); + + // Query both collections + const cityResults = await City.find({ officeName: regexOfficeName }).select("city -_id").lean(); + const branchResults = await Branch.find({ officeName: regexOfficeName }).select("city -_id").lean(); + + // Extract and merge unique city names + const cityNames = [...new Set([...cityResults.map(c => c.city), ...branchResults.map(b => b.city)])]; + + // Debugging + console.log("City Results:", cityResults); + console.log("Branch Results:", branchResults); + + reply.send({ status_code: 200, data: cityNames }); + } catch (err) { + console.error("Error fetching cities:", err); + reply.send({ error: err.message }); + } + }; \ No newline at end of file diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index 2911456b..e2a2ce08 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -565,5 +565,28 @@ module.exports = function (fastify, opts, next) { handler: departmentController.editBranch, }); + fastify.get("/api/getCitiesByOfficeName/:officeName", { + schema: { + tags: ["Department"], + description: "This is for Get cities by OfficeName Data", + summary: "This is to Get cities by OfficeName Data", + params: { + type: "object", + properties: { + officeName: { + type: "string", + description: "officeName", + }, + }, + }, + + security: [ + { + basicAuth: [], + }, + ], + }, + handler: departmentController.getCitiesByOfficeName, + }); next(); }; \ No newline at end of file