|
|
@ -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 {
|
|
|
|
try {
|
|
|
|
const { officeName } = req.query;
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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 (officeName && officeName.toUpperCase() !== "ALL") {
|
|
|
|
if (headOffices.length === 0 && branches.length === 0) {
|
|
|
|
// Partial and case-insensitive match
|
|
|
|
return reply.code(404).send({
|
|
|
|
filter.officeName = { $regex: new RegExp(officeName, "i") };
|
|
|
|
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: []
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const offices = await Branch.find(filter).lean();
|
|
|
|
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({
|
|
|
|
return reply.code(200).send({
|
|
|
|
status_code: 200,
|
|
|
|
status_code: 200,
|
|
|
|
message: "Fetched successfully",
|
|
|
|
message: "Fetched successfully",
|
|
|
|
data: offices
|
|
|
|
data: result
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching offices:", error);
|
|
|
|
console.error("Error fetching city offices:", error);
|
|
|
|
return reply.code(500).send({
|
|
|
|
return reply.code(500).send({
|
|
|
|
status_code: 500,
|
|
|
|
status_code: 500,
|
|
|
|
message: "Internal server error"
|
|
|
|
message: "Internal server error"
|
|
|
|