remove the duplicates in department Name

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

@ -1128,31 +1128,32 @@ const getDepartmentsByName = async (officeName, city, departmentName) => {
.select("departmentName -_id")
.lean();
return result.map(doc => doc.departmentName);
// Remove duplicate department names
return [...new Set(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." });
}
// 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);
const departmentNames = await getDepartmentNamesByCity(city);
if (departmentNames.length === 0) {
return reply.status(404).send({ message: "No departments found for the specified 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 });
}
};
reply.send({ status_code: 200, data: departmentNames });
} catch (error) {
console.error("API Error:", error);
reply.status(500).send({ message: error.message });
}
};

Loading…
Cancel
Save