From 7b1f207114904b40baf069c1033494ace5540511 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 7 Aug 2025 15:42:03 +0530 Subject: [PATCH] filters are added in office name and city to get the details --- src/controllers/departmentController.js | 81 +++++++++++++++++++------ src/routes/departmentRoute.js | 8 ++- 2 files changed, 66 insertions(+), 23 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index bece4ef3..a1ca1f30 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -1267,27 +1267,68 @@ const getDepartmentsByName = async (officeName, city, departmentName) => { // Helper function to fetch department names by city - const getDepartmentNamesByCity = async (city) => { - try { - const trimmedCity = city.trim(); - - // Allow for extra whitespace before or after the city value in the database - const query = { - city: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } - }; - console.log("MongoDB Query:", JSON.stringify(query, null, 2)); - - const result = await Deparments.find(query) - .select("departmentName -_id") - .lean(); - - // Remove duplicate department names - return [...new Set(result.map(doc => doc.departmentName))]; - } catch (error) { - console.error("Error fetching departments by city:", error); - throw new Error("Error fetching departments by city."); + const getDepartmentNamesByCity = async (city) => { + try { + const trimmedCity = city.trim(); + + const query = { + $or: [ + { city: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } }, + { officeName: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } } + ] + }; + + console.log("MongoDB Query:", JSON.stringify(query, null, 2)); + + const result = await Deparments.find(query) + .select("departmentName -_id") + .lean(); + + // Remove duplicate department names + return [...new Set(result.map(doc => doc.departmentName))]; + } catch (error) { + console.error("Error fetching departments by city or officeName:", error); + throw new Error("Error fetching departments by city or officeName."); + } +}; + +exports.getOffices = async (req, reply) => { + try { + const { officeName, city } = req.params; + + // Build dynamic filter + const filter = {}; + + if (officeName && officeName !== 'ALL') { + // support multiple office names as comma-separated + const officeNames = officeName.split(',').map(name => name.trim()); + filter.officeName = { $in: officeNames }; } - }; + + if (city && city !== 'ALL') { + // support multiple cities as comma-separated + const cities = city.split(',').map(c => c.trim()); + filter.city = { $in: cities }; + } + + const offices = await Deparments.find(filter).lean(); + + reply.send({ + status_code: 200, + message: "Fetched successfully", + data: offices, + }); + + } catch (error) { + console.error("Error in getOffices:", error); + reply.code(500).send({ + status_code: 500, + message: "Internal server error", + error: error.message, + }); + } +}; + // API route handler exports.getDepartmentsByCity = async (req, reply) => { diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index 15076d06..e4e59d81 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -667,7 +667,7 @@ module.exports = function (fastify, opts, next) { fastify.route({ method: "GET", - url: "/api/departmentNameList/:city", + url: "/api/departmentNameList/:city/:officeName", schema: { tags: ["Department"], description: "Get a list of department names for a given city", @@ -675,12 +675,14 @@ module.exports = function (fastify, opts, next) { params: { type: "object", properties: { - city: { type: "string" } + city: { type: "string" }, + officeName: { type: "string" }, + }, required: ["city"], }, }, - handler: departmentController.getDepartmentsByCity, + handler: departmentController.getOffices, }); fastify.route({