|
|
|
@ -765,4 +765,49 @@ exports.addDepartment = async (request, reply) => {
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getDepartmentNames = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const result = await Deparments.aggregate([
|
|
|
|
|
{
|
|
|
|
|
$group: {
|
|
|
|
|
_id: {
|
|
|
|
|
$toUpper: { $trim: { input: "$departmentName" } }, // Convert to uppercase & trim spaces
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$sort: { _id: 1 } // Sort alphabetically
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$group: {
|
|
|
|
|
_id: null,
|
|
|
|
|
departmentNames: { $addToSet: "$_id" } // Collect unique values in an array
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
|
_id: 0,
|
|
|
|
|
departmentNames: 1 // Return only the array
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return result.length > 0 ? result[0].departmentNames : []; // Return an empty array if no data
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
throw new Error("Error fetching department names.");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// API Route
|
|
|
|
|
exports.getAllDepartmentNames = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const departments = await getDepartmentNames();
|
|
|
|
|
reply.send({ status_code: 200, data: departments });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|