get all department names

master^2
Bhaskar 8 months ago
parent af814bd203
commit 1940117694

@ -766,3 +766,48 @@ exports.addDepartment = async (request, reply) => {
}
};
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 });
}
};

@ -432,5 +432,23 @@ module.exports = function (fastify, opts, next) {
},
handler:departmentController.getDepartments
});
fastify.get("/api/getalldepartmentNames", {
schema: {
tags: ["Department"],
description: "This is for Get all departmentNames Data",
summary: "This is for to Get all departmentNames Data",
security: [
{
basicAuth: [],
},
],
},
//preHandler: fastify.auth([fastify.authenticate]),
handler: departmentController.getAllDepartmentNames,
});
next();
};
Loading…
Cancel
Save