department names by city

master^2
Bhaskar 8 months ago
parent ace4629131
commit 80da6870d2

@ -1078,4 +1078,48 @@ exports.getZonebasedLocations = async (req, reply) => {
reply.send({ error: err.message });
}
};
// 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();
return result.map(doc => doc.departmentName);
} catch (error) {
console.error("Error fetching departments by city:", error);
throw new Error("Error fetching departments by city.");
}
};
// API route handler
exports.getDepartmentsByCity = async (req, reply) => {
try {
const { city } = req.params;
if (!city || city.trim() === "") {
return reply.status(400).send({ message: "City is required." });
}
const departmentNames = await getDepartmentNamesByCity(city);
if (departmentNames.length === 0) {
return reply.status(404).send({ message: "No departments found for the specified city." });
}
reply.send({ status_code: 200, data: departmentNames });
} catch (error) {
console.error("API Error:", error);
reply.status(500).send({ message: error.message });
}
};

@ -597,5 +597,24 @@ module.exports = function (fastify, opts, next) {
},
handler: departmentController.getCitiesByOfficeName,
});
fastify.route({
method: "GET",
url: "/api/departmentNameList/:city",
schema: {
tags: ["Department"],
description: "Get a list of department names for a given city",
summary: "Department names by city",
params: {
type: "object",
properties: {
city: { type: "string" }
},
required: ["city"],
},
},
handler: departmentController.getDepartmentsByCity,
});
next();
};
Loading…
Cancel
Save