|
|
|
@ -858,7 +858,7 @@ exports.getAllOfficesByCity = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const cityRegex = new RegExp(city.trim(), "i");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 🔹 Step 1: Find all headOffices in this city
|
|
|
|
|
const headOffices = await City.find({ city: cityRegex }).lean();
|
|
|
|
@ -1796,4 +1796,57 @@ exports.getOfficeDetails = async (req, reply) => {
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getCompanyCitiesByOffice = async (request, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { officeName } = request.params;
|
|
|
|
|
|
|
|
|
|
let cityList = [];
|
|
|
|
|
|
|
|
|
|
if (officeName.toUpperCase() === "ALL") {
|
|
|
|
|
// 🔹 Get all cities from both schemas
|
|
|
|
|
const branchCities = await Branch.distinct("city");
|
|
|
|
|
const headOfficeCities = await City.distinct("city");
|
|
|
|
|
|
|
|
|
|
cityList = [...branchCities, ...headOfficeCities];
|
|
|
|
|
} else {
|
|
|
|
|
// 🔹 Case-insensitive regex for officeName
|
|
|
|
|
const nameRegex = new RegExp(`^\\s*${officeName.trim()}\\s*$`, "i");
|
|
|
|
|
|
|
|
|
|
const branchCities = await Branch.distinct("city", {
|
|
|
|
|
officeName: nameRegex,
|
|
|
|
|
});
|
|
|
|
|
const headOfficeCities = await City.distinct("city", {
|
|
|
|
|
officeName: nameRegex,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cityList = [...branchCities, ...headOfficeCities];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 🔹 Remove duplicates + filter out empty/null
|
|
|
|
|
cityList = [...new Set(cityList.filter((c) => c && c.trim()))];
|
|
|
|
|
|
|
|
|
|
// 🔹 Always add "ALL" as the first option
|
|
|
|
|
if (!cityList.includes("ALL")) {
|
|
|
|
|
cityList.unshift("ALL");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message:
|
|
|
|
|
cityList.length > 0
|
|
|
|
|
? "Cities fetched successfully"
|
|
|
|
|
: "No cities found for given officeName",
|
|
|
|
|
data: cityList,
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("❌ Error fetching cities:", err);
|
|
|
|
|
return reply.status(500).send({
|
|
|
|
|
status_code: 500,
|
|
|
|
|
message: "Internal server error",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|