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

Loading…
Cancel
Save