From e1c4a2e2bc5be650f6acf4401c72d043e3762720 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 12 Aug 2025 14:11:52 +0530 Subject: [PATCH] changes on branch --- src/controllers/admincontroller.js | 115 +++++++++++++++++++++++++++-- 1 file changed, 108 insertions(+), 7 deletions(-) diff --git a/src/controllers/admincontroller.js b/src/controllers/admincontroller.js index a12382d5..8db544e6 100644 --- a/src/controllers/admincontroller.js +++ b/src/controllers/admincontroller.js @@ -460,26 +460,127 @@ exports.getAllCompanys = 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.getAllOffices = 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" + }); + } - if (officeName && officeName.toUpperCase() !== "ALL") { - // Partial and case-insensitive match - filter.officeName = { $regex: new RegExp(officeName, "i") }; + 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 offices = await Branch.find(filter).lean(); + 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 || "", + 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 || "", + createdAt: br.createdAt || "", + updatedAt: br.updatedAt || "" + }); + }); return reply.code(200).send({ status_code: 200, message: "Fetched successfully", - data: offices + data: allOffices }); + } 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"