ashok 1 month ago
commit 9d63e6f86e

@ -487,6 +487,114 @@ 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"
// });
// }
// 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 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;
@ -498,19 +606,28 @@ exports.getAllOffices = async (req, reply) => {
});
}
const nameRegex = new RegExp(officeName.trim(), "i");
let headOffices, branches, departments;
// Fetch head offices, branches, and departments
const [headOffices, branches, departments] = await Promise.all([
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 for the given officeName"
message: "No offices found"
});
}
@ -546,7 +663,7 @@ exports.getAllOffices = async (req, reply) => {
country: ho.country || "",
pincode: ho.pincode || "",
email: ho.email || "",
latitude:ho.latitude || "",
latitude: ho.latitude || "",
longitude: ho.longitude || "",
googleLocation: ho.googleLocation || "",
createdAt: ho.createdAt || "",
@ -571,7 +688,7 @@ exports.getAllOffices = async (req, reply) => {
pincode: br.pincode || "",
email: br.email || "",
contactPerson: br.nameoftheContactPerson || "",
latitude:br.latitude || "",
latitude: br.latitude || "",
longitude: br.longitude || "",
googleLocation: br.googleLocation || "",
createdAt: br.createdAt || "",
@ -595,6 +712,117 @@ exports.getAllOffices = async (req, reply) => {
};
exports.getAllOfficesByCity = async (req, reply) => {
try {
const { city } = req.query;
if (!city) {
return reply.code(400).send({
status_code: 400,
message: "city query param is required"
});
}
const cityRegex = new RegExp(city.trim(), "i");
// Fetch head offices, branches, and departments
const [headOffices, branches, departments] = await Promise.all([
City.find({ city: cityRegex }).lean(),
Branch.find({ city: cityRegex }).lean(),
Deparments.find({ city: cityRegex }).lean()
]);
if (headOffices.length === 0 && branches.length === 0) {
return reply.code(404).send({
status_code: 404,
message: "No offices found for the given city"
});
}
const allOffices = [];
// Process head offices
headOffices.forEach(ho => {
const cityTrimmed = ho.city?.trim().toLowerCase();
// Get all department docs for this city
const matchingDepartments = departments.filter(
d => d.city?.trim().toLowerCase() === cityTrimmed
);
// Count employees
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
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 by city:", error);
return reply.code(500).send({
status_code: 500,
message : "Internal server error"
});
}
};
// exports.getCityOffices = async (req, reply) => {
// try {
// const { officeName } = req.query;

@ -311,6 +311,23 @@ fastify.get("/api/getBranchDetails", {
handler: adminController.getAllOffices,
});
fastify.get("/api/getOfficesByCity", {
schema: {
tags: ["Admin"],
description: "Get Offices by City",
summary: "Fetch Head Offices and Branches by City",
querystring: {
type: 'object',
required: ['city'],
properties: {
city: { type: 'string' }
}
}
},
handler: adminController.getAllOfficesByCity,
});
fastify.put("/api/editTeamMember/:departmentId/:teamMemberId", {
schema: {
description: "Admin Edit Team Member",

Loading…
Cancel
Save