From b0839a18506f506f1b554b6da3b506a89c77c151 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 29 Aug 2025 12:49:19 +0530 Subject: [PATCH 1/3] chnages on city search --- src/controllers/admincontroller.js | 146 ++++++++++++++--------------- 1 file changed, 71 insertions(+), 75 deletions(-) diff --git a/src/controllers/admincontroller.js b/src/controllers/admincontroller.js index b1fe5a19..38027668 100644 --- a/src/controllers/admincontroller.js +++ b/src/controllers/admincontroller.js @@ -853,99 +853,92 @@ exports.getAllOfficesByCity = async (req, reply) => { if (!city) { return reply.code(400).send({ status_code: 400, - message: "city query param is required" + message: "city query param is required", }); } const cityRegex = new RegExp(city.trim(), "i"); - // 1) Try to find head offices directly in this city - let headOffices = await City.find({ city: cityRegex }).lean(); + // 🔹 Step 1: Find all headOffices in this city + const headOffices = await City.find({ city: cityRegex }).lean(); - // 2) If no head office, check if branch exists in that city - if (!headOffices.length) { - const branchMatches = await Branch.find({ city: cityRegex }).lean(); + // 🔹 Step 2: Find all branchOffices in this city + const branchMatches = await Branch.find({ city: cityRegex }).lean(); - if (!branchMatches.length) { - return reply.code(404).send({ - status_code: 404, - message: `No headOffice or branch found for city ${city}` - }); - } - - // Take officeName(s) from branch to find headOffice - const officeNames = [...new Set(branchMatches.map(b => b.officeName))]; - - headOffices = await City.find({ - officeName: { $in: officeNames } - }).lean(); - - // If still no headOffice found, fallback to just using branch data - if (!headOffices.length) { - return reply.code(200).send({ - status_code: 200, - message: "Fetched successfully (branch only, no headOffice found)", - data: branchMatches.map(br => ({ - officeName: br.officeName, - city: br.city, - offices: [br] - })) - }); - } + if (!headOffices.length && !branchMatches.length) { + return reply.code(404).send({ + status_code: 404, + message: `No headOffice or branch found for city ${city}`, + }); } + // 🔹 Step 3: Collect all unique officeNames + const officeNames = [ + ...new Set([ + ...headOffices.map((ho) => ho.officeName.trim()), + ...branchMatches.map((br) => br.officeName.trim()), + ]), + ]; - // 3) Build response for each headOffice found const finalResponse = []; - for (const ho of headOffices) { - // employee count - const departments = await Deparments.find({ city: ho.city }).lean(); - const employeeCount = departments.reduce((count, dep) => { - const mainPerson = 1; - const subTeamCount = Array.isArray(dep?.team_member?.team_member) - ? dep.team_member.team_member.length - : 0; - return count + mainPerson + subTeamCount; - }, 0); + // 🔹 Step 4: For each officeName, gather HO + Branches + for (const name of officeNames) { + const ho = await City.findOne({ + officeName: new RegExp(name, "i"), + }).lean(); + + // Get employee count for headOffice (if exists) + let employeeCount = 0; + if (ho) { + const departments = await Deparments.find({ city: ho.city }).lean(); + employeeCount = departments.reduce((count, dep) => { + const mainPerson = 1; + const subTeamCount = Array.isArray(dep?.team_member?.team_member) + ? dep.team_member.team_member.length + : 0; + return count + mainPerson + subTeamCount; + }, 0); + } - // find all branches for this officeName + // Get all branches for this officeName const branches = await Branch.find({ - officeName: new RegExp(ho.officeName.trim(), "i") + officeName: new RegExp(name, "i"), }).lean(); - // build office array const offices = []; - // head office - offices.push({ - officeType: "headOffice", - officeName: ho.officeName?.trim() || "", - city: ho.city?.trim() || "", - cityId: ho.cityId || "", - employeeCount, - phone: ho.phone || "", - address: ho.office_address1 || "", - address2: ho.address2 || "", - state: ho.state || "", - country: ho.country || "", - pincode: ho.pincode || "", - email: ho.email || "", - latitude: ho.latitude || 0, - longitude: ho.longitude || 0, - googleLocation: ho.googleLocation || "", - createdAt: ho.createdAt || "", - updatedAt: ho.updatedAt || "" - }); + // Add headOffice if found + if (ho) { + offices.push({ + officeType: "headOffice", + officeName: ho.officeName?.trim() || "", + city: ho.city?.trim() || "", + cityId: ho.cityId || "", + employeeCount, + phone: ho.phone || "", + address: ho.office_address1 || "", + address2: ho.address2 || "", + state: ho.state || "", + country: ho.country || "", + pincode: ho.pincode || "", + email: ho.email || "", + latitude: ho.latitude || 0, + longitude: ho.longitude || 0, + googleLocation: ho.googleLocation || "", + createdAt: ho.createdAt || "", + updatedAt: ho.updatedAt || "", + }); + } - // branches - branches.forEach(br => { + // Add all branchOffices + branches.forEach((br) => { offices.push({ officeType: "branchOffice", branchId: br.branchId || "", officeName: br.officeName?.trim() || "", city: br.city?.trim() || "", - employeeCount, // optional + employeeCount, // using HO employee count (optional) phone: br.phone || "", address: br.office_address1 || "", address2: br.address2 || "", @@ -958,32 +951,35 @@ exports.getAllOfficesByCity = async (req, reply) => { longitude: br.longitude || 0, googleLocation: br.googleLocation || "", createdAt: br.createdAt || "", - updatedAt: br.updatedAt || "" + updatedAt: br.updatedAt || "", }); }); finalResponse.push({ - // officeName: ho.officeName?.trim() || "", - // city: ho.city?.trim() || "", - offices + officeName: name, + city, + offices, }); } return reply.code(200).send({ status_code: 200, message: "Fetched successfully", - data: finalResponse + data: finalResponse, }); } catch (error) { console.error("❌ Error in getAllOfficesByCity:", error); return reply.code(500).send({ status_code: 500, message: "Internal server error", - error: error.message + error: error.message, }); } }; + + + // exports.getAllOfficesByCity = async (req, reply) => { // try { // const { city } = req.query; From 188ff245fa72611361ea8160964a84b2062fad51 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 29 Aug 2025 12:49:53 +0530 Subject: [PATCH 2/3] changes --- src/routes/departmentRoute.js | 20 ++++++++++---------- src/routes/installationRoute.js | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index eae2c014..cc51a3ee 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -6,7 +6,7 @@ module.exports = function (fastify, opts, next) { method: "POST", url: "/api/citySignup", schema: { - tags: ["Department"], + tags: ["Admin"], description: "This is for creating a new City account", summary: "This is for creating a new City account", body: { @@ -104,7 +104,7 @@ module.exports = function (fastify, opts, next) { fastify.get("/api/getallcompanyNames", { schema: { - tags: ["Department"], + tags: ["Admin"], description: "This is for Get all Company Name in city schema ", summary: "This is for to Get all Company Name in city schema ", @@ -212,7 +212,7 @@ fastify.route({ method: "PUT", url: "/api/updateBranchOrCompanydetails/:id", schema: { - tags: ["Department"], + tags: ["Admin"], description: "Update details of a branch or city", summary: "Edit department details by branchId or cityId", params: { @@ -549,7 +549,7 @@ fastify.route({ method: "GET", url: "/api/zonebasedcity/:city/:officeName", schema: { - tags: ["Department"], + tags: ["Admin"], description: "Get the zones by city and office", summary: "Get the zones by city and office", params: { @@ -621,7 +621,7 @@ fastify.route({ method: "GET", url: "/api/departmentNamebaselist/:officeName/:city/:departmentName/:employeeType", schema: { - tags: ["Department"], + tags: ["Admin"], description: "Department name based list", summary: "Department name based list", params: { @@ -658,7 +658,7 @@ fastify.route({ method: "POST", url: "/api/branchSignup", schema: { - tags: ["Department"], + tags: ["Admin"], description: "This is for creating a new Branch account", summary: "This is for creating a new Branch account", body: { @@ -699,7 +699,7 @@ fastify.route({ method: "POST", url: "/api/zoneSignup", schema: { - tags: ["Department"], + tags: ["Admin"], description: "This is for creating a new Zone account", summary: "This is for creating a new Zone account", body: { @@ -784,7 +784,7 @@ fastify.route({ fastify.get("/api/getCitiesByOfficeName/:officeName", { schema: { - tags: ["Department"], + tags: ["Admin"], description: "This is for Get cities by OfficeName Data", summary: "This is to Get cities by OfficeName Data", params: { @@ -810,7 +810,7 @@ fastify.route({ method: "GET", url: "/api/departmentNameList/:city/:officeName", schema: { - tags: ["Department"], + tags: ["Admin"], description: "Get a list of department names for a given city", summary: "Department names by city", params: { @@ -888,7 +888,7 @@ fastify.route({ method: "GET", url: "/api/staffdepartments/:officeName/:city", schema: { - tags: ["Department"], + tags: ["Admin"], description: "This is for fetching department details based on officeName and city", summary: "This is for fetching department details based on officeName and city", params: { diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index dbaf6976..49e5967a 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -88,7 +88,7 @@ fastify.get("/api/getAllDepartments/:officeName/:city", { fastify.get("/api/getTeamMembers/:officeName/:city/:departmentId", { schema: { description: "Get all team members under a specific department", - tags: ["Department"], + tags: ["Admin"], summary: "Get Team Members by Department ID", params: { type: "object", From dba910c44694653e5b57561a50b01858912f2850 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 29 Aug 2025 12:54:41 +0530 Subject: [PATCH 3/3] changes --- src/controllers/admincontroller.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/controllers/admincontroller.js b/src/controllers/admincontroller.js index 38027668..995602ca 100644 --- a/src/controllers/admincontroller.js +++ b/src/controllers/admincontroller.js @@ -858,6 +858,7 @@ exports.getAllOfficesByCity = async (req, reply) => { } const cityRegex = new RegExp(city.trim(), "i"); + // 🔹 Step 1: Find all headOffices in this city const headOffices = await City.find({ city: cityRegex }).lean();