From ace4629131d289ff82f85337a90a63cc93cf76ae Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 4 Feb 2025 16:52:09 +0530 Subject: [PATCH] get departments to added city in pathparams --- src/controllers/departmentController.js | 55 +++++++++++++++++++++---- src/routes/departmentRoute.js | 3 +- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index e1cecc4a..7343f0bc 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -939,35 +939,72 @@ exports.getZonebasedLocations = async (req, reply) => { - const getDepartmentsByName = async (departmentName) => { + // const getDepartmentsByName = async (departmentName, city) => { + // try { + // const result = await Deparments.find({ + // departmentName: { $regex: `^${departmentName.trim()}$`, $options: "i" }, // Case-insensitive search + // city: { $regex: `^${city.trim()}$`, $options: "i" }, // Case-insensitive search + // }).lean(); // Convert to plain JSON + + // return result; + // } catch (err) { + // console.error("Error fetching department data:", err); + // throw new Error("Error fetching department data."); + // } + // }; + + const getDepartmentsByName = async (departmentName, city) => { try { - const result = await Deparments.find({ - departmentName: { $regex: `^${departmentName}$`, $options: "i" }, // Case-insensitive search - }); + const trimmedDepartment = departmentName.trim(); + const trimmedCity = city.trim(); + + const query = { + departmentName: { $regex: trimmedDepartment, $options: "i" }, + city: { $regex: trimmedCity, $options: "i" } + }; + + console.log("MongoDB Query:", JSON.stringify(query, null, 2)); + + const result = await Deparments.find(query).lean(); + console.log("Query Result:", result); return result; } catch (err) { - console.error(err); + console.error("Error fetching department data:", err); throw new Error("Error fetching department data."); } }; + // API Route exports.getDepartments = async (req, reply) => { try { - const { departmentName } = req.params; // Get departmentName from request params + console.log("Request Params:", req.params); // Debugging log + + let { departmentName, city } = req.params; - if (!departmentName) { - return reply.status(400).send({ message: "Department Name is required." }); + if (!departmentName || !city) { + return reply.status(400).send({ message: "Department Name and City are required." }); + } + + departmentName = departmentName.trim(); + city = city.trim(); + + const departments = await getDepartmentsByName(departmentName, city); + + if (departments.length === 0) { + return reply.status(404).send({ message: "No departments found for the specified name and city." }); } - const departments = await getDepartmentsByName(departmentName); reply.send({ status_code: 200, data: departments }); } catch (err) { + console.error("API Error:", err); reply.status(500).send({ message: err.message }); } }; + + const getDepartmentNames = async () => { try { diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index 37cc5298..82ff9012 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -445,7 +445,7 @@ module.exports = function (fastify, opts, next) { fastify.route({ method: "GET", - url: "/api/departmentNamebaselist/:departmentName", + url: "/api/departmentNamebaselist/:departmentName/:city", schema: { tags: ["Department"], description: "Department name based list", @@ -454,6 +454,7 @@ module.exports = function (fastify, opts, next) { type: "object", properties: { departmentName: { type: "string" }, + city: { type: "string" }, }, }, },