From 64c99f9d07581322d2bf154565c0f94f2ac879a0 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 22 Jul 2025 11:34:37 +0530 Subject: [PATCH] getDepartmentDetailsByAdminAndName --- src/controllers/admincontroller.js | 56 +++++++++++++++++++++++++++++- src/routes/adminRoute.js | 23 ++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/controllers/admincontroller.js b/src/controllers/admincontroller.js index a4d3ac69..31399bd5 100644 --- a/src/controllers/admincontroller.js +++ b/src/controllers/admincontroller.js @@ -5,7 +5,8 @@ const bcrypt = require('bcrypt') const fastify = require("fastify"); -const { Tank, MotorData, IotData } = require('../models/tanks') +const { Tank, MotorData, IotData } = require('../models/tanks'); +const { Deparments } = require('../models/Department'); const JWT_SECRET = 'your-secret-key'; async function generateCustomerId(role) { @@ -368,3 +369,56 @@ exports.createUser = async (request, reply) => { } } + + exports.getDepartmentDetailsByAdminAndName = async (req, reply) => { + try { + const { adminId } = req.params; + const { departmentName, reportingManager } = req.body; + + if (!adminId) { + return reply.status(400).send({ + simplydata: { error: true, message: "adminId is required in path params" } + }); + } + + if (!departmentName || !reportingManager) { + return reply.status(400).send({ + simplydata: { error: true, message: "departmentName and reportingManager are required in body" } + }); + } + + // ✅ Find department by adminId, departmentName and reportingManager + const department = await Deparments.findOne({ + adminId, + departmentName, + reportingManager + }).lean(); + + if (!department) { + return reply.status(404).send({ + simplydata: { error: true, message: "Department not found with given criteria" } + }); + } + + // ✅ Build response data + const responseData = { + phone: department.phone, + firstName: department.firstName, + lastName: department.lastName, + email: department.email + }; + + return reply.send({ + simplydata: { + error: false, + message: "Department details fetched successfully", + data: responseData + } + }); + } catch (err) { + console.error("Error fetching department details:", err); + reply.status(500).send({ + simplydata: { error: true, message: "Internal server error" } + }); + } +}; diff --git a/src/routes/adminRoute.js b/src/routes/adminRoute.js index 1d0c1509..94010257 100644 --- a/src/routes/adminRoute.js +++ b/src/routes/adminRoute.js @@ -254,6 +254,29 @@ fastify.post("/api/integratingHardwareidToTank", { handler: adminController.integratingHardwareidToTank, }); +fastify.post("/api/getDepartmentDetails/:adminId", { + schema: { + description: "Get department details by adminId, departmentName and reportingManager", + tags: ["Admin"], + summary: "Get department details", + params: { + type: "object", + properties: { + adminId: { type: "string", description: "Admin ID" } + }, + required: ["adminId"] + }, + body: { + type: "object", + properties: { + departmentName: { type: "string" }, + reportingManager: { type: "string" } + }, + required: ["departmentName", "reportingManager"] + } + }, + handler: adminController.getDepartmentDetailsByAdminAndName +}); next();