From 0996eb7133f0b25cf3d71d8523dd260b007059f0 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 26 Aug 2025 13:28:09 +0530 Subject: [PATCH] changes on Get Branch Details --- src/controllers/admincontroller.js | 168 ++++++++++++++++++++++++++--- 1 file changed, 151 insertions(+), 17 deletions(-) diff --git a/src/controllers/admincontroller.js b/src/controllers/admincontroller.js index 961da75e..fa11cccc 100644 --- a/src/controllers/admincontroller.js +++ b/src/controllers/admincontroller.js @@ -595,6 +595,122 @@ 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" +// }); +// } + +// 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" +// }); +// } + +// 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; @@ -631,18 +747,18 @@ exports.getAllOffices = async (req, reply) => { }); } - const allOffices = []; + // 🏢 Group by officeName + const grouped = {}; - // Process head offices (with employee count) + // Head offices headOffices.forEach(ho => { - const officeNameTrimmed = ho.officeName.trim().toLowerCase(); + const key = ho.officeName.trim().toLowerCase(); + if (!grouped[key]) grouped[key] = []; - // Get all department docs for this office const matchingDepartments = departments.filter( - d => d.officeName?.trim().toLowerCase() === officeNameTrimmed + d => d.officeName?.trim().toLowerCase() === key ); - // 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) @@ -651,7 +767,7 @@ exports.getAllOffices = async (req, reply) => { return count + mainPerson + subTeamCount; }, 0); - allOffices.push({ + grouped[key].push({ officeType: "headOffice", officeName: ho.officeName.trim(), city: ho.city?.trim() || "", @@ -659,27 +775,42 @@ exports.getAllOffices = async (req, reply) => { 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 || "", - longitude: ho.longitude || "", + latitude: ho.latitude || 0, + longitude: ho.longitude || 0, googleLocation: ho.googleLocation || "", createdAt: ho.createdAt || "", updatedAt: ho.updatedAt || "" }); }); - // Process branches (no employee count here) + // Branches branches.forEach(br => { - allOffices.push({ - officeType: "branch", + const key = br.officeName.trim().toLowerCase(); + if (!grouped[key]) grouped[key] = []; + + const matchingDepartments = departments.filter( + d => d.officeName?.trim().toLowerCase() === key && d.city === br.city + ); + + 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); + + grouped[key].push({ + officeType: "branchOffice", branchId: br.branchId || "", officeName: br.officeName?.trim() || "", city: br.city?.trim() || "", - zone: br.zone || "", - location: br.location || [], + employeeCount, phone: br.phone || "", address: br.office_address1 || "", address2: br.address2 || "", @@ -688,18 +819,21 @@ exports.getAllOffices = async (req, reply) => { pincode: br.pincode || "", email: br.email || "", contactPerson: br.nameoftheContactPerson || "", - latitude: br.latitude || "", - longitude: br.longitude || "", + latitude: br.latitude || 0, + longitude: br.longitude || 0, googleLocation: br.googleLocation || "", createdAt: br.createdAt || "", updatedAt: br.updatedAt || "" }); }); + // Convert grouped object into array + const result = Object.values(grouped).map(offices => ({ offices })); + return reply.code(200).send({ status_code: 200, message: "Fetched successfully", - data: allOffices + data: result }); } catch (error) {