officename added in list

master^2
Bhaskar 8 months ago
parent b3f9043f08
commit 70b25df6c0

@ -953,47 +953,53 @@ exports.getZonebasedLocations = async (req, reply) => {
// } // }
// }; // };
const getDepartmentsByName = async (departmentName, city) => { // Updated helper function that accepts all three parameters
try { const getDepartmentsByName = async (officeName, city, departmentName) => {
const trimmedDepartment = departmentName.trim(); try {
const trimmedCity = city.trim(); // Trim all parameters
const trimmedOfficeName = officeName.trim();
const query = { const trimmedCity = city.trim();
departmentName: { $regex: trimmedDepartment, $options: "i" }, const trimmedDepartment = departmentName.trim();
city: { $regex: trimmedCity, $options: "i" }
}; const query = {
officeName: { $regex: trimmedOfficeName, $options: "i" },
console.log("MongoDB Query:", JSON.stringify(query, null, 2)); departmentName: { $regex: trimmedDepartment, $options: "i" },
city: { $regex: trimmedCity, $options: "i" }
const result = await Deparments.find(query).lean(); };
console.log("Query Result:", result); console.log("MongoDB Query:", JSON.stringify(query, null, 2));
return result;
} catch (err) { const result = await Deparments.find(query).lean();
console.error("Error fetching department data:", err);
throw new Error("Error fetching department data."); console.log("Query Result:", result);
} return result;
}; } catch (err) {
console.error("Error fetching department data:", err);
throw new Error("Error fetching department data.");
}
};
// API Route // API Route
exports.getDepartments = async (req, reply) => { exports.getDepartments = async (req, reply) => {
try { try {
console.log("Request Params:", req.params); // Debugging log console.log("Request Params:", req.params); // Debugging log
let { departmentName, city } = req.params; let { departmentName, city, officeName } = req.params;
if (!departmentName || !city) { if (!departmentName || !city || !officeName) {
return reply.status(400).send({ message: "Department Name and City are required." }); return reply.status(400).send({ message: "Department Name, City, and Office Name are required." });
} }
departmentName = departmentName.trim(); departmentName = departmentName.trim();
city = city.trim(); city = city.trim();
officeName = officeName.trim();
const departments = await getDepartmentsByName(departmentName, city); // Note the order: officeName, city, departmentName
const departments = await getDepartmentsByName(officeName, city, departmentName);
if (departments.length === 0) { if (departments.length === 0) {
return reply.status(404).send({ message: "No departments found for the specified name and city." }); return reply.status(404).send({ message: "No departments found for the specified parameters." });
} }
reply.send({ status_code: 200, data: departments }); reply.send({ status_code: 200, data: departments });

@ -445,7 +445,7 @@ module.exports = function (fastify, opts, next) {
fastify.route({ fastify.route({
method: "GET", method: "GET",
url: "/api/departmentNamebaselist/:departmentName/:city", url: "/api/departmentNamebaselist/:officeName/:city/:departmentName",
schema: { schema: {
tags: ["Department"], tags: ["Department"],
description: "Department name based list", description: "Department name based list",
@ -453,8 +453,9 @@ module.exports = function (fastify, opts, next) {
params: { params: {
type: "object", type: "object",
properties: { properties: {
departmentName: { type: "string" }, officeName: { type: "string" },
city: { type: "string" }, city: { type: "string" },
departmentName: { type: "string" },
}, },
}, },
}, },

Loading…
Cancel
Save