From ef022ed0e31c86c0494c919b137517200e961787 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 12 Aug 2025 13:26:14 +0530 Subject: [PATCH] Get Branch Details --- src/controllers/admincontroller.js | 225 ++++++++++++++++++++++++++++- src/routes/adminRoute.js | 2 +- 2 files changed, 218 insertions(+), 9 deletions(-) diff --git a/src/controllers/admincontroller.js b/src/controllers/admincontroller.js index f0b8f109..1aac7be3 100644 --- a/src/controllers/admincontroller.js +++ b/src/controllers/admincontroller.js @@ -460,26 +460,235 @@ exports.getAllCompanys = async (req, reply) => { // }; -exports.getAllOffices = async (req, reply) => { +// exports.getAllOffices = async (req, reply) => { +// try { +// const { officeName } = req.query; + +// let filter = {}; + +// if (officeName && officeName.toUpperCase() !== "ALL") { +// // Partial and case-insensitive match +// filter.officeName = { $regex: new RegExp(officeName, "i") }; +// } + +// const offices = await Branch.find(filter).lean(); + +// return reply.code(200).send({ +// status_code: 200, +// message: "Fetched successfully", +// data: offices +// }); +// } catch (error) { +// console.error("Error fetching offices:", error); +// return reply.code(500).send({ +// status_code: 500, +// message: "Internal server error" +// }); +// } +// }; + + +// exports.getCityOffices = async (req, reply) => { +// try { +// const { officeName } = req.query; + +// if (!officeName) { +// return reply.code(400).send({ +// status_code: 400, +// message: "officeName query param is required" +// }); +// } + +// // Regex filter for partial & case-insensitive match +// const nameRegex = new RegExp(officeName.trim(), "i"); + +// // Step 1: Fetch head offices matching the officeName +// const headOffices = await City.find({ officeName: nameRegex }).lean(); + +// // Step 2: Fetch branches matching the officeName +// const branches = await Branch.find({ officeName: nameRegex }).lean(); + +// // Step 3: Fetch departments matching the officeName +// const departments = await 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" +// }); +// } + +// // Step 4: Build city-wise result +// const cityMap = {}; + +// headOffices.forEach(ho => { +// const officeNameTrimmed = ho.officeName.trim(); + +// // Find department for this office to get employee count +// const departmentDoc = departments.find( +// d => +// d.officeName && +// d.officeName.trim().toLowerCase() === officeNameTrimmed.toLowerCase() +// ); +// const employeeCount = +// departmentDoc?.team_member?.team_member?.length || 0; + +// cityMap[ho.city.trim().toLowerCase()] = { +// city: ho.city.trim(), +// headOffice: { +// officeName: ho.officeName.trim(), +// employeeCount, +// phone: ho.phone || "", +// address: ho.office_address1 || "", +// state: ho.state || "", +// country: ho.country || "", +// pincode: ho.pincode || "", +// email: ho.email || "" +// }, +// branches: [] +// }; +// }); + +// // Step 5: Attach branches +// branches.forEach(br => { +// const cityKey = br.city.trim().toLowerCase(); +// if (!cityMap[cityKey]) { +// cityMap[cityKey] = { +// city: br.city.trim(), +// headOffice: null, +// branches: [] +// }; +// } +// cityMap[cityKey].branches.push({ +// officeName: br.officeName?.trim() || "", +// location: br.location || [], +// phone: br.phone || "", +// address: br.address1 || "" +// }); +// }); + +// // Step 6: Convert to array +// const result = Object.values(cityMap); + +// return reply.code(200).send({ +// status_code: 200, +// message: "Fetched successfully", +// data: result +// }); +// } catch (error) { +// console.error("Error fetching city offices:", error); +// return reply.code(500).send({ +// status_code: 500, +// message: "Internal server error" +// }); +// } +// }; + +exports.getCityOffices = async (req, reply) => { try { const { officeName } = req.query; - let filter = {}; + if (!officeName) { + return reply.code(400).send({ + status_code: 400, + message: "officeName query param is required" + }); + } + + const nameRegex = new RegExp(officeName.trim(), "i"); - if (officeName && officeName.toUpperCase() !== "ALL") { - // Partial and case-insensitive match - filter.officeName = { $regex: new RegExp(officeName, "i") }; + // Step 1: Fetch head offices + const headOffices = await City.find({ officeName: nameRegex }).lean(); + + // Step 2: Fetch branches + const branches = await Branch.find({ officeName: nameRegex }).lean(); + + // Step 3: Fetch departments + const departments = await 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 offices = await Branch.find(filter).lean(); + const cityMap = {}; + + headOffices.forEach(ho => { + const officeNameTrimmed = ho.officeName.trim(); + + // Find department for this office to get employee count + const departmentDoc = departments.find( + d => d.officeName?.trim().toLowerCase() === officeNameTrimmed.toLowerCase() + ); + console.log("departmentDoc",departmentDoc) + const mainPersonCount = departmentDoc?.team_member?.main_person ? 1 : 0; + const subTeamCount = Array.isArray(departmentDoc?.team_member?.team_member) + ? departmentDoc.team_member.team_member.length + : 0; + + const employeeCount = mainPersonCount + subTeamCount; + + cityMap[ho.city.trim().toLowerCase()] = { + // cityId: ho.cityId || "", // added cityId + city: ho.city.trim(), + headOffice: { + officeName: ho.officeName.trim(), + cityId: ho.cityId || "", // added cityId + employeeCount, + phone: ho.phone || "", + address: ho.office_address1 || "", + state: ho.state || "", + country: ho.country || "", + pincode: ho.pincode || "", + email: ho.email || "" + }, + // branches: [] + }; + }); + + // Step 5: Attach branches + branches.forEach(br => { + const cityKey = br.city.trim().toLowerCase(); + + if (!cityMap[cityKey]) { + cityMap[cityKey] = { + //cityId: br.cityId || "", + city: br.city.trim(), + // headOffice: null, + branches: [] + }; + } + + cityMap[cityKey].branches.push({ + branchId: br.branchId || "", + officeName: br.officeName?.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 || "", + createdAt: br.createdAt || "", + updatedAt: br.updatedAt || "" + }); + }); + + const result = Object.values(cityMap); return reply.code(200).send({ status_code: 200, message: "Fetched successfully", - data: offices + data: result }); + } catch (error) { - console.error("Error fetching offices:", error); + console.error("Error fetching city offices:", error); return reply.code(500).send({ status_code: 500, message: "Internal server error" diff --git a/src/routes/adminRoute.js b/src/routes/adminRoute.js index ce6d59b6..e00ffcd8 100644 --- a/src/routes/adminRoute.js +++ b/src/routes/adminRoute.js @@ -308,7 +308,7 @@ fastify.get("/api/getBranchDetails", { } } }, - handler: adminController.getAllOffices, + handler: adminController.getCityOffices, }); fastify.get("/api/getOfficeDetails/:officeName/:city", {