ashok 1 month ago
commit 865af40683

@ -595,10 +595,127 @@ 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) => { exports.getAllOffices = async (req, reply) => {
try { try {
const { officeName } = req.query; const { officeName } = req.query;
if (!officeName) { if (!officeName) {
return reply.code(400).send({ return reply.code(400).send({
status_code: 400, status_code: 400,
@ -631,18 +748,18 @@ exports.getAllOffices = async (req, reply) => {
}); });
} }
const allOffices = []; // 🏢 Group by officeName
const grouped = {};
// Process head offices (with employee count) // Head offices
headOffices.forEach(ho => { 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( 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 employeeCount = matchingDepartments.reduce((count, dep) => {
const mainPerson = 1; const mainPerson = 1;
const subTeamCount = Array.isArray(dep?.team_member?.team_member) const subTeamCount = Array.isArray(dep?.team_member?.team_member)
@ -651,7 +768,7 @@ exports.getAllOffices = async (req, reply) => {
return count + mainPerson + subTeamCount; return count + mainPerson + subTeamCount;
}, 0); }, 0);
allOffices.push({ grouped[key].push({
officeType: "headOffice", officeType: "headOffice",
officeName: ho.officeName.trim(), officeName: ho.officeName.trim(),
city: ho.city?.trim() || "", city: ho.city?.trim() || "",
@ -659,27 +776,42 @@ exports.getAllOffices = async (req, reply) => {
employeeCount, employeeCount,
phone: ho.phone || "", phone: ho.phone || "",
address: ho.office_address1 || "", address: ho.office_address1 || "",
address2: ho.address2 || "",
state: ho.state || "", state: ho.state || "",
country: ho.country || "", country: ho.country || "",
pincode: ho.pincode || "", pincode: ho.pincode || "",
email: ho.email || "", email: ho.email || "",
latitude: ho.latitude || "", latitude: ho.latitude || 0,
longitude: ho.longitude || "", longitude: ho.longitude || 0,
googleLocation: ho.googleLocation || "", googleLocation: ho.googleLocation || "",
createdAt: ho.createdAt || "", createdAt: ho.createdAt || "",
updatedAt: ho.updatedAt || "" updatedAt: ho.updatedAt || ""
}); });
}); });
// Process branches (no employee count here) // Branches
branches.forEach(br => { branches.forEach(br => {
allOffices.push({ const key = br.officeName.trim().toLowerCase();
officeType: "branch", 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 || "", branchId: br.branchId || "",
officeName: br.officeName?.trim() || "", officeName: br.officeName?.trim() || "",
city: br.city?.trim() || "", city: br.city?.trim() || "",
zone: br.zone || "", employeeCount,
location: br.location || [],
phone: br.phone || "", phone: br.phone || "",
address: br.office_address1 || "", address: br.office_address1 || "",
address2: br.address2 || "", address2: br.address2 || "",
@ -688,18 +820,21 @@ exports.getAllOffices = async (req, reply) => {
pincode: br.pincode || "", pincode: br.pincode || "",
email: br.email || "", email: br.email || "",
contactPerson: br.nameoftheContactPerson || "", contactPerson: br.nameoftheContactPerson || "",
latitude: br.latitude || "", latitude: br.latitude || 0,
longitude: br.longitude || "", longitude: br.longitude || 0,
googleLocation: br.googleLocation || "", googleLocation: br.googleLocation || "",
createdAt: br.createdAt || "", createdAt: br.createdAt || "",
updatedAt: br.updatedAt || "" updatedAt: br.updatedAt || ""
}); });
}); });
// Convert grouped object into array
const result = Object.values(grouped).map(offices => ({ offices }));
return reply.code(200).send({ return reply.code(200).send({
status_code: 200, status_code: 200,
message: "Fetched successfully", message: "Fetched successfully",
data: allOffices data: result
}); });
} catch (error) { } catch (error) {

Loading…
Cancel
Save