From 48328e58b13432d94fc2c4961219baa92ffd053a Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 22 Aug 2025 17:18:16 +0530 Subject: [PATCH] Fetch Head Offices and Branches by City --- src/controllers/admincontroller.js | 249 +++++++++++++++++++++++++++-- src/routes/adminRoute.js | 16 ++ 2 files changed, 254 insertions(+), 11 deletions(-) diff --git a/src/controllers/admincontroller.js b/src/controllers/admincontroller.js index 8c15b174..53f3511e 100644 --- a/src/controllers/admincontroller.js +++ b/src/controllers/admincontroller.js @@ -487,6 +487,114 @@ exports.getAllCompanys = async (req, reply) => { // } // }; +// exports.getAllOffices = async (req, reply) => { +// try { +// const { officeName } = req.query; + +// if (!officeName) { +// return reply.code(400).send({ +// status_code: 400, +// message: "officeName query param is required" +// }); +// } + +// const nameRegex = new RegExp(officeName.trim(), "i"); + +// // Fetch head offices, branches, and departments +// const [headOffices, branches, departments] = await Promise.all([ +// City.find({ officeName: nameRegex }).lean(), +// Branch.find({ officeName: nameRegex }).lean(), +// Deparments.find({ officeName: nameRegex }).lean() +// ]); + +// if (headOffices.length === 0 && branches.length === 0) { +// return reply.code(404).send({ +// status_code: 404, +// message: "No offices found for the given officeName" +// }); +// } + +// const allOffices = []; + +// // Process head offices (with employee count) +// headOffices.forEach(ho => { +// const officeNameTrimmed = ho.officeName.trim().toLowerCase(); + +// // Get all department docs for this office +// const matchingDepartments = departments.filter( +// d => d.officeName?.trim().toLowerCase() === officeNameTrimmed +// ); + +// // Count employees: 1 main person per doc + sub-team members +// const employeeCount = matchingDepartments.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); + +// allOffices.push({ +// officeType: "headOffice", +// officeName: ho.officeName.trim(), +// city: ho.city?.trim() || "", +// cityId: ho.cityId || "", +// employeeCount, +// phone: ho.phone || "", +// address: ho.office_address1 || "", +// state: ho.state || "", +// country: ho.country || "", +// pincode: ho.pincode || "", +// email: ho.email || "", +// latitude:ho.latitude || "", +// longitude: ho.longitude || "", +// googleLocation: ho.googleLocation || "", +// createdAt: ho.createdAt || "", +// updatedAt: ho.updatedAt || "" +// }); +// }); + +// // Process branches (no employee count here) +// branches.forEach(br => { +// allOffices.push({ +// officeType: "branch", +// branchId: br.branchId || "", +// officeName: br.officeName?.trim() || "", +// city: br.city?.trim() || "", +// zone: br.zone || "", +// location: br.location || [], +// phone: br.phone || "", +// address: br.office_address1 || "", +// address2: br.address2 || "", +// state: br.state || "", +// country: br.country || "", +// pincode: br.pincode || "", +// email: br.email || "", +// contactPerson: br.nameoftheContactPerson || "", +// latitude:br.latitude || "", +// longitude: br.longitude || "", +// googleLocation: br.googleLocation || "", +// createdAt: br.createdAt || "", +// updatedAt: br.updatedAt || "" +// }); +// }); + +// return reply.code(200).send({ +// status_code: 200, +// message: "Fetched successfully", +// data: allOffices +// }); + +// } catch (error) { +// console.error("Error fetching city offices:", error); +// return reply.code(500).send({ +// status_code: 500, +// message: "Internal server error" +// }); +// } +// }; + + exports.getAllOffices = async (req, reply) => { try { const { officeName } = req.query; @@ -498,19 +606,28 @@ exports.getAllOffices = async (req, reply) => { }); } - const nameRegex = new RegExp(officeName.trim(), "i"); - - // Fetch head offices, branches, and departments - const [headOffices, branches, departments] = await Promise.all([ - City.find({ officeName: nameRegex }).lean(), - Branch.find({ officeName: nameRegex }).lean(), - Deparments.find({ officeName: nameRegex }).lean() - ]); + let headOffices, branches, departments; + + if (officeName.trim().toUpperCase() === "ALL") { + // ✅ Fetch all without filtering + [headOffices, branches, departments] = await Promise.all([ + City.find().lean(), + Branch.find().lean(), + Deparments.find().lean() + ]); + } else { + const nameRegex = new RegExp(officeName.trim(), "i"); + [headOffices, branches, departments] = await Promise.all([ + City.find({ officeName: nameRegex }).lean(), + Branch.find({ officeName: nameRegex }).lean(), + Deparments.find({ officeName: nameRegex }).lean() + ]); + } if (headOffices.length === 0 && branches.length === 0) { return reply.code(404).send({ status_code: 404, - message: "No offices found for the given officeName" + message: "No offices found" }); } @@ -546,7 +663,7 @@ exports.getAllOffices = async (req, reply) => { country: ho.country || "", pincode: ho.pincode || "", email: ho.email || "", - latitude:ho.latitude || "", + latitude: ho.latitude || "", longitude: ho.longitude || "", googleLocation: ho.googleLocation || "", createdAt: ho.createdAt || "", @@ -571,7 +688,7 @@ exports.getAllOffices = async (req, reply) => { pincode: br.pincode || "", email: br.email || "", contactPerson: br.nameoftheContactPerson || "", - latitude:br.latitude || "", + latitude: br.latitude || "", longitude: br.longitude || "", googleLocation: br.googleLocation || "", createdAt: br.createdAt || "", @@ -595,6 +712,116 @@ exports.getAllOffices = async (req, reply) => { }; + + +exports.getAllOfficesByCity = async (req, reply) => { + try { + const { city } = req.query; + + if (!city) { + return reply.code(400).send({ + status_code: 400, + message: "city query param is required" + }); + } + + const cityRegex = new RegExp(city.trim(), "i"); + + // Fetch head offices, branches, and departments + const [headOffices, branches, departments] = await Promise.all([ + City.find({ city: cityRegex }).lean(), + Branch.find({ city: cityRegex }).lean(), + Deparments.find({ city: cityRegex }).lean() + ]); + + if (headOffices.length === 0 && branches.length === 0) { + return reply.code(404).send({ + status_code: 404, + message: "No offices found for the given city" + }); + } + + const allOffices = []; + + // Process head offices + headOffices.forEach(ho => { + const cityTrimmed = ho.city?.trim().toLowerCase(); + + // Get all department docs for this city + const matchingDepartments = departments.filter( + d => d.city?.trim().toLowerCase() === cityTrimmed + ); + + // Count employees + const employeeCount = matchingDepartments.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); + + allOffices.push({ + officeType: "headOffice", + officeName: ho.officeName?.trim() || "", + city: ho.city?.trim() || "", + cityId: ho.cityId || "", + employeeCount, + phone: ho.phone || "", + address: ho.office_address1 || "", + state: ho.state || "", + country: ho.country || "", + pincode: ho.pincode || "", + email: ho.email || "", + latitude: ho.latitude || "", + longitude: ho.longitude || "", + googleLocation: ho.googleLocation || "", + createdAt: ho.createdAt || "", + updatedAt: ho.updatedAt || "" + }); + }); + + // Process branches + branches.forEach(br => { + allOffices.push({ + officeType: "branch", + branchId: br.branchId || "", + officeName: br.officeName?.trim() || "", + city: br.city?.trim() || "", + zone: br.zone || "", + location: br.location || [], + phone: br.phone || "", + address: br.office_address1 || "", + address2: br.address2 || "", + state: br.state || "", + country: br.country || "", + pincode: br.pincode || "", + email: br.email || "", + contactPerson: br.nameoftheContactPerson || "", + latitude: br.latitude || "", + longitude: br.longitude || "", + googleLocation: br.googleLocation || "", + createdAt: br.createdAt || "", + updatedAt: br.updatedAt || "" + }); + }); + + return reply.code(200).send({ + status_code: 200, + message: "Fetched successfully", + data: allOffices + }); + + } catch (error) { + console.error("Error fetching city offices by city:", error); + return reply.code(500).send({ + status_code: 500, + message : "Internal server error" + }); + } +}; + + // exports.getCityOffices = async (req, reply) => { // try { // const { officeName } = req.query; diff --git a/src/routes/adminRoute.js b/src/routes/adminRoute.js index 821059c0..1fbabebe 100644 --- a/src/routes/adminRoute.js +++ b/src/routes/adminRoute.js @@ -311,6 +311,22 @@ fastify.get("/api/getBranchDetails", { handler: adminController.getAllOffices, }); + +fastify.get("/api/getOfficesByCity", { + schema: { + tags: ["Admin"], + description: "Get Offices by City", + summary: "Fetch Head Offices and Branches by City", + querystring: { + type: 'object', + required: ['city'], + properties: { + city: { type: 'string' } + } + } + }, + handler: adminController.getAllOfficesByCity, +}); fastify.put("/api/editTeamMember/:departmentId/:teamMemberId", { schema: { description: "Admin Edit Team Member",