From 7184fb1cb0577b06142e090c06c16a5db73b178f Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 22 Jul 2025 17:31:56 +0530 Subject: [PATCH] changes on Fetch department's firstName and phone --- src/controllers/installationController.js | 90 +++++++++++++---------- src/routes/installationRoute.js | 3 +- 2 files changed, 52 insertions(+), 41 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index f97b53c2..324a5d92 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -932,49 +932,59 @@ exports.getQuotationsByInstallationAndTeamMember = async (request, reply) => { }; - exports.getDepartmentByFirstName = async (req, reply) => { - try { - let { firstName } = req.params; - - if (!firstName) { - return reply.status(400).send({ - simplydata: { error: true, message: "firstName is required" }, - }); - } - - // Trim and convert to lowercase - firstName = firstName.trim().toLowerCase(); - console.log("Searching for firstName:", firstName); // Debugging log - - // Search for the department with case-insensitive and space-tolerant regex - const department = await Deparments.findOne({ - firstName: { $regex: `^\\s*${firstName}\\s*$`, $options: "i" } - }).lean(); - - console.log("Department found:", department); // Debugging log - - if (!department) { - return reply.status(404).send({ - simplydata: { error: true, message: "Department not found" }, - }); - } - - return reply.send({ - simplydata: { - error: false, - message: "Department details fetched successfully", - firstName: department.firstName, - phone: department.phone, - }, +exports.getDepartmentByFirstName = async (req, reply) => { + try { + let { departmentName, firstName } = req.params; + + if (!departmentName) { + return reply.status(400).send({ + simplydata: { error: true, message: "departmentName is required" }, }); - - } catch (err) { - console.error("Error fetching department details:", err); - return reply.status(500).send({ - simplydata: { error: true, message: "Internal server error" }, + } + if (!firstName) { + return reply.status(400).send({ + simplydata: { error: true, message: "firstName is required" }, }); } - }; + + departmentName = departmentName.trim(); + firstName = firstName.trim(); + + console.log("Searching for:", { departmentName, firstName }); + + const department = await Deparments.findOne({ + firstName: { $regex: `^\\s*${firstName}\\s*$`, $options: "i" }, + departmentName: { $regex: `^${departmentName}$`, $options: "i" } + }).lean(); + + console.log("Department found:", department); + + if (!department) { + return reply.status(404).send({ + simplydata: { error: true, message: "Department not found" }, + }); + } + + return reply.send({ + simplydata: { + error: false, + message: "Department details fetched successfully", + firstName: department.firstName, + phone: department.phone, + lastName: department.lastName, + email: department.email + }, + }); + + } catch (err) { + console.error("Error fetching department details:", err); + return reply.status(500).send({ + simplydata: { error: true, message: "Internal server error" }, + }); + } +}; + + // const moment = require('moment-timezone'); diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 0780df90..b0a01f6d 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -235,7 +235,7 @@ fastify.get("/api/getAllDepartments/:departmentName", { handler: installationController.deleteTeamMember }); - fastify.get("/api/getDepartmentByFirstName/:firstName", { + fastify.get("/api/getDepartmentByFirstName/:departmentName/:firstName", { schema: { description: "Get department details by first name", tags: ["Installation"], @@ -244,6 +244,7 @@ fastify.get("/api/getAllDepartments/:departmentName", { type: "object", properties: { firstName: { type: "string", description: "Department's first name" }, + departmentName: { type: "string"} }, required: ["firstName"], },