diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index b654292b..81c1c459 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -1433,47 +1433,76 @@ exports.getZonesByCityAndOffice = async (req, reply) => { // } // }; - const getDepartmentsByName = async (officeName, city, departmentName) => { - try { - const query = {}; +// const getDepartmentsByName = async (officeName, city, departmentName) => { +// try { +// const query = {}; - if (officeName && officeName.trim().toUpperCase() !== "ALL") { - query.officeName = { $regex: officeName.trim(), $options: "i" }; - } +// if (officeName && officeName.trim().toUpperCase() !== "ALL") { +// query.officeName = { $regex: officeName.trim(), $options: "i" }; +// } - if (city && city.trim().toUpperCase() !== "ALL") { - query.city = { $regex: city.trim(), $options: "i" }; - } +// if (city && city.trim().toUpperCase() !== "ALL") { +// query.city = { $regex: city.trim(), $options: "i" }; +// } - if (departmentName && departmentName.trim().toUpperCase() !== "ALL") { - query.departmentName = { $regex: departmentName.trim(), $options: "i" }; - } +// if (departmentName && departmentName.trim().toUpperCase() !== "ALL") { +// query.departmentName = { $regex: departmentName.trim(), $options: "i" }; +// } - console.log("MongoDB Query:", JSON.stringify(query, null, 2)); +// 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("Error fetching department data:", err); +// throw new Error("Error fetching department data."); +// } +// }; + +// exports.getDepartments = async (req, reply) => { +// try { +// console.log("Request Params:", req.params); + +// let { departmentName, city, officeName } = req.params; + +// if (!departmentName || !city || !officeName) { +// return reply.status(400).send({ +// message: "Department Name, City, and Office Name are required.", +// }); +// } + +// const departments = await getDepartmentsByName(officeName, city, departmentName); + +// if (departments.length === 0) { +// return reply.status(404).send({ +// message: "No departments found for the specified parameters.", +// }); +// } + +// reply.send({ status_code: 200, data: departments }); +// } catch (err) { +// console.error("API Error:", err); +// reply.status(500).send({ message: err.message }); +// } +// }; - const result = await Deparments.find(query).lean(); - console.log("Query Result:", result); - return result; - } catch (err) { - console.error("Error fetching department data:", err); - throw new Error("Error fetching department data."); - } -}; exports.getDepartments = async (req, reply) => { try { console.log("Request Params:", req.params); - let { departmentName, city, officeName } = req.params; + let { departmentName, city, officeName, employeeType } = req.params; - if (!departmentName || !city || !officeName) { + if (!departmentName || !city || !officeName || !employeeType) { return reply.status(400).send({ - message: "Department Name, City, and Office Name are required.", + message: "Department Name, City, Office Name, and Employee Type are required.", }); } - const departments = await getDepartmentsByName(officeName, city, departmentName); + const departments = await getDepartmentsByName(officeName, city, departmentName, employeeType); if (departments.length === 0) { return reply.status(404).send({ @@ -1488,6 +1517,37 @@ exports.getDepartments = async (req, reply) => { } }; +const getDepartmentsByName = async (officeName, city, departmentName, employeeType) => { + try { + const query = {}; + + if (officeName && officeName.trim().toUpperCase() !== "ALL") { + query.officeName = { $regex: officeName.trim(), $options: "i" }; + } + + if (city && city.trim().toUpperCase() !== "ALL") { + query.city = { $regex: city.trim(), $options: "i" }; + } + + if (departmentName && departmentName.trim().toUpperCase() !== "ALL") { + query.departmentName = { $regex: departmentName.trim(), $options: "i" }; + } + + if (employeeType && employeeType.trim().toUpperCase() !== "ALL") { + query.employeeType = { $regex: `^${employeeType.trim()}$`, $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("Error fetching department data:", err); + throw new Error("Error fetching department data."); + } +}; const getDepartmentNames = async () => { diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index 428da809..76798205 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -596,25 +596,44 @@ fastify.route({ handler:departmentController.getZonesByArea }); - fastify.route({ - method: "GET", - url: "/api/departmentNamebaselist/:officeName/:city/:departmentName", - schema: { - tags: ["Department"], - description: "Department name based list", - summary: "Department name based list", - params: { - type: "object", - properties: { - officeName: { type: "string" }, - city: { type: "string" }, - departmentName: { type: "string" }, - }, - }, - }, - handler:departmentController.getDepartments - }); + // fastify.route({ + // method: "GET", + // url: "/api/departmentNamebaselist/:officeName/:city/:departmentName", + // schema: { + // tags: ["Department"], + // description: "Department name based list", + // summary: "Department name based list", + // params: { + // type: "object", + // properties: { + // officeName: { type: "string" }, + // city: { type: "string" }, + // departmentName: { type: "string" }, + // }, + // }, + // }, + // handler:departmentController.getDepartments + // }); + fastify.route({ + method: "GET", + url: "/api/departmentNamebaselist/:officeName/:city/:departmentName/:employeeType", + schema: { + tags: ["Department"], + description: "Department name based list", + summary: "Department name based list", + params: { + type: "object", + properties: { + officeName: { type: "string" }, + city: { type: "string" }, + departmentName: { type: "string" }, + employeeType: { type: "string", enum: ["Internal", "Consultant", "ALL"] }, + }, + }, + }, + handler: departmentController.getDepartments +}); fastify.get("/api/getalldepartmentNames", { schema: {