Varun 9 months ago
commit ec0ed4ab75

@ -735,4 +735,79 @@ exports.addDepartment = async (request, reply) => {
reply.status(500).send({ message: err.message });
}
};
const getDepartmentsByName = async (departmentName) => {
try {
const result = await Deparments.find({
departmentName: { $regex: `^${departmentName}$`, $options: "i" }, // Case-insensitive search
});
return result;
} catch (err) {
console.error(err);
throw new Error("Error fetching department data.");
}
};
// API Route
exports.getDepartments = async (req, reply) => {
try {
const { departmentName } = req.params; // Get departmentName from request params
if (!departmentName) {
return reply.status(400).send({ message: "Department Name is required." });
}
const departments = await getDepartmentsByName(departmentName);
reply.send({ status_code: 200, data: departments });
} catch (err) {
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 });
}
};

@ -415,5 +415,40 @@ module.exports = function (fastify, opts, next) {
},
handler:departmentController.getZonesByCity
});
fastify.route({
method: "GET",
url: "/api/departmentNamebaselist/:departmentName",
schema: {
tags: ["Department"],
description: "Department name based list",
summary: "Department name based list",
params: {
type: "object",
properties: {
departmentName: { type: "string" },
},
},
},
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