diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 70a660cc..eeb7c36f 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -530,4 +530,49 @@ exports.assignTeamMemberToQuotation = 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, + }, + }); + + } catch (err) { + console.error("Error fetching department details:", err); + return reply.status(500).send({ + simplydata: { error: true, message: "Internal server error" }, + }); + } + }; + \ No newline at end of file diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 1e9b84fb..41404a45 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -212,6 +212,22 @@ module.exports = function (fastify, opts, next) { handler: installationController.deleteTeamMember }); + fastify.get("/api/getDepartmentByFirstName/:firstName", { + schema: { + description: "Get department details by first name", + tags: ["Installation"], + summary: "Fetch department's firstName and phone", + params: { + type: "object", + properties: { + firstName: { type: "string", description: "Department's first name" }, + }, + required: ["firstName"], + }, + }, + handler: installationController.getDepartmentByFirstName, + }); + next();