diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 78df232c..17005620 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -2,7 +2,7 @@ const boom = require("boom"); const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); const customJwtAuth = require("../customAuthJwt"); -const { Deparments } = require("../models/Department"); +const { Deparments, City, Branch } = require("../models/Department"); const { Install, SensorStock, SensorQuotation, Order, Insensors, MasterSlaveData, ElectrictyWorkPictures, PlumbingWorkPictures, MaterialRecievedPictures, Support, Repairorder } = require("../models/store"); const { Counter, User } = require("../models/User"); const { IotData, Tank } = require("../models/tanks"); @@ -299,33 +299,110 @@ exports.createTeamMember = async (req, 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(); + +// // Always start with Self +// const responseData = [{ firstName: "Self" }, ...(departments || [])]; + +// return reply.send({ +// simplydata: { +// error: false, +// message: departments.length +// ? "Departments retrieved successfully" +// : "No departments found with the given departmentName", +// data: responseData, + +// }, +// }); + +// } catch (err) { +// console.error("Error fetching departments:", err); +// return reply.status(500).send({ +// simplydata: { +// error: true, +// message: "Internal server error", +// }, +// }); +// } +// }; + exports.getAllDepartments = async (request, reply) => { try { - const { departmentName } = request.params; + const { officeName, city } = request.params; - if (!departmentName) { + if (!officeName || !city) { return reply.status(400).send({ simplydata: { error: true, - message: "departmentName is required in path params", + message: "officeName and city are required in path params", }, }); } - // Find all departments matching departmentName - const departments = await Deparments.find({ departmentName }).lean(); + // Prepare case-insensitive regex for matching + const nameRegex = new RegExp(`^${officeName.trim()}$`, "i"); + const cityRegex = new RegExp(`^${city.trim()}$`, "i"); + + // 1️⃣ Check Branch schema for match + const branchMatch = await Branch.findOne({ + officeName: nameRegex, + city: cityRegex, + }).lean(); + + // 2️⃣ Check City schema for match + const cityMatch = await City.findOne({ + officeName: nameRegex, + city: cityRegex, + }).lean(); + + // 3️⃣ Get all departments for this officeName & city + const departments = await Deparments.find({ + officeName: nameRegex, + city: cityRegex, + }).lean(); + + // Start response data with "Self" + const responseData = [{ firstName: "Self" }]; + + // If a Branch or City record is found, add it right after "Self" + if (branchMatch) { + responseData.push({ + officeType: "branch", + ...branchMatch + }); + } + if (cityMatch) { + responseData.push({ + officeType: "headOffice", + ...cityMatch + }); + } - // Always start with Self - const responseData = [{ firstName: "Self" }, ...(departments || [])]; + // Then add department docs + responseData.push(...departments); return reply.send({ simplydata: { error: false, - message: departments.length - ? "Departments retrieved successfully" - : "No departments found with the given departmentName", + message: + departments.length || branchMatch || cityMatch + ? "Data retrieved successfully" + : "No data found for the given officeName and city", data: responseData, - }, }); diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index b0a01f6d..19c6c4c0 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -32,17 +32,19 @@ module.exports = function (fastify, opts, next) { handler: installationController.createTeamMember, }); -fastify.get("/api/getAllDepartments/:departmentName", { +fastify.get("/api/getAllDepartments/:officeName/:city", { schema: { description: "Get full department details by department name", tags: ["Admin"], - summary: "Get all department details", + summary: "Get all department details fetch the managers list", params: { type: "object", properties: { - departmentName: { type: "string", description: "Department name to search" }, + officeName: { type: "string", }, + city: { type: "string", }, + }, - required: ["departmentName"], + // required: ["departmentName"], }, }, handler: installationController.getAllDepartments,