remove the duplicates in department Name

master^2
Bhaskar 8 months ago
parent 2dc6a64bf7
commit 4eb56f3466

@ -1117,42 +1117,43 @@ const getDepartmentsByName = async (officeName, city, departmentName) => {
const getDepartmentNamesByCity = async (city) => { const getDepartmentNamesByCity = async (city) => {
try { try {
const trimmedCity = city.trim(); const trimmedCity = city.trim();
// Allow for extra whitespace before or after the city value in the database // Allow for extra whitespace before or after the city value in the database
const query = { const query = {
city: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } city: { $regex: `^\\s*${trimmedCity}\\s*$`, $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) const result = await Deparments.find(query)
.select("departmentName -_id") .select("departmentName -_id")
.lean(); .lean();
return result.map(doc => doc.departmentName); // Remove duplicate department names
return [...new Set(result.map(doc => doc.departmentName))];
} catch (error) { } catch (error) {
console.error("Error fetching departments by city:", error); console.error("Error fetching departments by city:", error);
throw new Error("Error fetching departments by city."); throw new Error("Error fetching departments by city.");
} }
}; };
// API route handler
// API route handler exports.getDepartmentsByCity = async (req, reply) => {
exports.getDepartmentsByCity = async (req, reply) => { try {
try { const { city } = req.params;
const { city } = req.params; if (!city || city.trim() === "") {
if (!city || city.trim() === "") { return reply.status(400).send({ message: "City is required." });
return reply.status(400).send({ message: "City is required." }); }
}
const departmentNames = await getDepartmentNamesByCity(city);
const departmentNames = await getDepartmentNamesByCity(city);
if (departmentNames.length === 0) {
if (departmentNames.length === 0) { return reply.status(404).send({ message: "No departments found for the specified city." });
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 });
} }
};
reply.send({ status_code: 200, data: departmentNames });
} catch (error) {
console.error("API Error:", error);
reply.status(500).send({ message: error.message });
}
};

Loading…
Cancel
Save