chnages on city search

master^2
Bhaskar 1 month ago
parent 2b70c8045d
commit b0839a1850

@ -853,71 +853,63 @@ exports.getAllOfficesByCity = async (req, reply) => {
if (!city) {
return reply.code(400).send({
status_code: 400,
message: "city query param is required"
message: "city query param is required",
});
}
const cityRegex = new RegExp(city.trim(), "i");
// 1) Try to find head offices directly in this city
let headOffices = await City.find({ city: cityRegex }).lean();
// 🔹 Step 1: Find all headOffices in this city
const headOffices = await City.find({ city: cityRegex }).lean();
// 2) If no head office, check if branch exists in that city
if (!headOffices.length) {
// 🔹 Step 2: Find all branchOffices in this city
const branchMatches = await Branch.find({ city: cityRegex }).lean();
if (!branchMatches.length) {
if (!headOffices.length && !branchMatches.length) {
return reply.code(404).send({
status_code: 404,
message: `No headOffice or branch found for city ${city}`
message: `No headOffice or branch found for city ${city}`,
});
}
// Take officeName(s) from branch to find headOffice
const officeNames = [...new Set(branchMatches.map(b => b.officeName))];
// 🔹 Step 3: Collect all unique officeNames
const officeNames = [
...new Set([
...headOffices.map((ho) => ho.officeName.trim()),
...branchMatches.map((br) => br.officeName.trim()),
]),
];
headOffices = await City.find({
officeName: { $in: officeNames }
}).lean();
// If still no headOffice found, fallback to just using branch data
if (!headOffices.length) {
return reply.code(200).send({
status_code: 200,
message: "Fetched successfully (branch only, no headOffice found)",
data: branchMatches.map(br => ({
officeName: br.officeName,
city: br.city,
offices: [br]
}))
});
}
}
// 3) Build response for each headOffice found
const finalResponse = [];
for (const ho of headOffices) {
// employee count
// 🔹 Step 4: For each officeName, gather HO + Branches
for (const name of officeNames) {
const ho = await City.findOne({
officeName: new RegExp(name, "i"),
}).lean();
// Get employee count for headOffice (if exists)
let employeeCount = 0;
if (ho) {
const departments = await Deparments.find({ city: ho.city }).lean();
const employeeCount = departments.reduce((count, dep) => {
employeeCount = departments.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);
}
// find all branches for this officeName
// Get all branches for this officeName
const branches = await Branch.find({
officeName: new RegExp(ho.officeName.trim(), "i")
officeName: new RegExp(name, "i"),
}).lean();
// build office array
const offices = [];
// head office
// Add headOffice if found
if (ho) {
offices.push({
officeType: "headOffice",
officeName: ho.officeName?.trim() || "",
@ -935,17 +927,18 @@ exports.getAllOfficesByCity = async (req, reply) => {
longitude: ho.longitude || 0,
googleLocation: ho.googleLocation || "",
createdAt: ho.createdAt || "",
updatedAt: ho.updatedAt || ""
updatedAt: ho.updatedAt || "",
});
}
// branches
branches.forEach(br => {
// Add all branchOffices
branches.forEach((br) => {
offices.push({
officeType: "branchOffice",
branchId: br.branchId || "",
officeName: br.officeName?.trim() || "",
city: br.city?.trim() || "",
employeeCount, // optional
employeeCount, // using HO employee count (optional)
phone: br.phone || "",
address: br.office_address1 || "",
address2: br.address2 || "",
@ -958,32 +951,35 @@ exports.getAllOfficesByCity = async (req, reply) => {
longitude: br.longitude || 0,
googleLocation: br.googleLocation || "",
createdAt: br.createdAt || "",
updatedAt: br.updatedAt || ""
updatedAt: br.updatedAt || "",
});
});
finalResponse.push({
// officeName: ho.officeName?.trim() || "",
// city: ho.city?.trim() || "",
offices
officeName: name,
city,
offices,
});
}
return reply.code(200).send({
status_code: 200,
message: "Fetched successfully",
data: finalResponse
data: finalResponse,
});
} catch (error) {
console.error("❌ Error in getAllOfficesByCity:", error);
return reply.code(500).send({
status_code: 500,
message: "Internal server error",
error: error.message
error: error.message,
});
}
};
// exports.getAllOfficesByCity = async (req, reply) => {
// try {
// const { city } = req.query;

Loading…
Cancel
Save