diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index f97b53c2..45dc5979 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -140,49 +140,49 @@ exports.createTeamMember = async (req, reply) => { } }; -exports.getAllDepartments = async (request, reply) => { - try { - const { departmentName } = request.params; +// exports.getAllDepartments = async (request, reply) => { +// try { +// const { departmentName } = request.params; - if (!departmentName) { - return reply.status(400).send({ - simplydata: { - error: true, - message: "departmentName is required in path params", - }, - }); - } +// if (!departmentName) { +// return reply.status(400).send({ +// simplydata: { +// error: true, +// message: "departmentName is required in path params", +// }, +// }); +// } - // ✅ Find all departments matching departmentName - const departments = await Deparments.find({ departmentName }).lean(); +// // ✅ Find all departments matching departmentName +// const departments = await Deparments.find({ departmentName }).lean(); - if (!departments.length) { - return reply.status(404).send({ - simplydata: { - error: true, - message: "No departments found with the given departmentName", - }, - }); - } +// if (!departments.length) { +// return reply.status(404).send({ +// simplydata: { +// error: true, +// message: "No departments found with the given departmentName", +// }, +// }); +// } - return reply.send({ - simplydata: { - error: false, - message: "Departments retrieved successfully", - data: departments, - }, - }); +// return reply.send({ +// simplydata: { +// error: false, +// message: "Departments retrieved successfully", +// data: departments, +// }, +// }); - } catch (err) { - console.error("Error fetching departments:", err); - return reply.status(500).send({ - simplydata: { - error: true, - message: "Internal server error", - }, - }); - } -}; +// } catch (err) { +// console.error("Error fetching departments:", err); +// return reply.status(500).send({ +// simplydata: { +// error: true, +// message: "Internal server error", +// }, +// }); +// } +// }; // exports.assignTeamMemberToQuotation = async (request, reply) => { // try { @@ -250,6 +250,54 @@ exports.getAllDepartments = async (request, reply) => { // } // }; +exports.getAllDepartments = async (request, reply) => { + try { + const { departmentName } = request.params; + + if (!departmentName) { + return reply.status(400).send({ + simplydata: { + error: true, + message: "departmentName is required in path params", + }, + }); + } + + // Find all departments matching departmentName + const departments = await Deparments.find({ departmentName }).lean(); + + if (!departments.length) { + return reply.status(404).send({ + simplydata: { + error: true, + message: "No departments found with the given departmentName", + }, + }); + } + + // Add extra object { firstName: "Self" } at start + const extraObject = { firstName: "Self" }; + const responseData = [extraObject, ...departments]; + + return reply.send({ + simplydata: { + error: false, + message: "Departments retrieved successfully", + data: responseData, + }, + }); + + } catch (err) { + console.error("Error fetching departments:", err); + return reply.status(500).send({ + simplydata: { + error: true, + message: "Internal server error", + }, + }); + } +}; + exports.assignTeamMemberToQuotation = async (request, reply) => { try { const { installationId } = request.params; @@ -932,49 +980,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"], },