Get Branch Details

master^2
Bhaskar 2 months ago
parent ef022ed0e3
commit 6efaa1de19

@ -584,6 +584,118 @@ exports.getAllCompanys = async (req, reply) => {
// }
// };
// 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"
// });
// }
// const nameRegex = new RegExp(officeName.trim(), "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 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: 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;
@ -597,14 +709,12 @@ exports.getCityOffices = async (req, reply) => {
const nameRegex = new RegExp(officeName.trim(), "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();
// 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({
@ -616,26 +726,27 @@ exports.getCityOffices = async (req, reply) => {
const cityMap = {};
headOffices.forEach(ho => {
const officeNameTrimmed = ho.officeName.trim();
const officeNameTrimmed = ho.officeName.trim().toLowerCase();
// Find department for this office to get employee count
const departmentDoc = departments.find(
d => d.officeName?.trim().toLowerCase() === officeNameTrimmed.toLowerCase()
// Get all department docs for this office
const matchingDepartments = departments.filter(
d => d.officeName?.trim().toLowerCase() === officeNameTrimmed
);
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;
// Count employees: each department doc = 1 main person + sub-team members
const employeeCount = matchingDepartments.reduce((count, dep) => {
const mainPerson = 1; // the document itself
const subTeamCount = Array.isArray(dep?.team_member?.team_member)
? dep.team_member.team_member.length
: 0;
return count + mainPerson + subTeamCount;
}, 0);
cityMap[ho.city.trim().toLowerCase()] = {
// cityId: ho.cityId || "", // added cityId
city: ho.city.trim(),
headOffice: {
officeName: ho.officeName.trim(),
cityId: ho.cityId || "", // added cityId
cityId: ho.cityId || "",
employeeCount,
phone: ho.phone || "",
address: ho.office_address1 || "",
@ -648,19 +759,15 @@ exports.getCityOffices = async (req, reply) => {
};
});
// Step 5: Attach branches
// 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() || "",
@ -679,14 +786,11 @@ exports.getCityOffices = async (req, reply) => {
});
});
const result = Object.values(cityMap);
return reply.code(200).send({
status_code: 200,
message: "Fetched successfully",
data: result
data: Object.values(cityMap)
});
} catch (error) {
console.error("Error fetching city offices:", error);
return reply.code(500).send({

Loading…
Cancel
Save