From 822d86a9026921d74cc568989461313bce5a9f94 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 30 Jan 2025 12:40:07 +0530 Subject: [PATCH 1/2] based on department name to get the list --- src/controllers/departmentController.js | 30 +++++++++++++++++++++++++ src/routes/departmentRoute.js | 17 ++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 4719cbb5..12e5489d 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -735,4 +735,34 @@ 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 }); + } + }; \ No newline at end of file diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index 3c483985..f2cfc739 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -415,5 +415,22 @@ 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 + }); next(); }; \ No newline at end of file From 19401176945948f17f5283490c41d8316b60e283 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 30 Jan 2025 12:50:20 +0530 Subject: [PATCH 2/2] get all department names --- src/controllers/departmentController.js | 45 +++++++++++++++++++++++++ src/routes/departmentRoute.js | 18 ++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 12e5489d..939462d6 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -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 }); + } + }; \ No newline at end of file diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index f2cfc739..67f6dc14 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -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(); }; \ No newline at end of file