|
|
|
@ -262,7 +262,7 @@ exports.getallCompanyNames = async (req, reply) => {
|
|
|
|
|
.select("officeName -_id") // Select only officeName and exclude _id
|
|
|
|
|
.exec()
|
|
|
|
|
.then((docs) => {
|
|
|
|
|
const officeNames = docs.map((doc) => doc.officeName); // Extract only officeName values
|
|
|
|
|
const officeNames = ["ALL", ...docs.map((doc) => doc.officeName)]; // Prepend "ALL"
|
|
|
|
|
reply.send({ status_code: 200, data: officeNames, count: officeNames.length });
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
@ -1237,26 +1237,98 @@ const getDepartmentsByName = async (officeName, city, departmentName) => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getCitiesByOfficeName = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// let { officeName } = req.params;
|
|
|
|
|
|
|
|
|
|
// if (!officeName) {
|
|
|
|
|
// return reply.code(400).send({ error: "officeName is required" });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Split by comma and normalize names
|
|
|
|
|
// let officeNames = officeName.split(',').map(name =>
|
|
|
|
|
// name.trim().replace(/\s+/g, ' ')
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// // Handle "All" — fetch all cities from both collections
|
|
|
|
|
// if (officeNames.includes('All')) {
|
|
|
|
|
// const allCityDocs = await City.find().select("city -_id").lean();
|
|
|
|
|
// const allBranchDocs = await Branch.find().select("city -_id").lean();
|
|
|
|
|
|
|
|
|
|
// const allCities = [...new Set([
|
|
|
|
|
// ...allCityDocs.map(doc => doc.city),
|
|
|
|
|
// ...allBranchDocs.map(doc => doc.city),
|
|
|
|
|
// ])];
|
|
|
|
|
|
|
|
|
|
// return reply.send({ status_code: 200, data: allCities });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Build regex conditions for each office name
|
|
|
|
|
// const regexConditions = officeNames.map(name => ({
|
|
|
|
|
// officeName: { $regex: new RegExp(name.replace(/\s+/g, '\\s*'), 'i') }
|
|
|
|
|
// }));
|
|
|
|
|
|
|
|
|
|
// // Query both collections
|
|
|
|
|
// const cityResults = await City.find({ $or: regexConditions }).select("city -_id").lean();
|
|
|
|
|
// const branchResults = await Branch.find({ $or: regexConditions }).select("city -_id").lean();
|
|
|
|
|
|
|
|
|
|
// // Extract and merge unique city names
|
|
|
|
|
// const cityNames = [...new Set([
|
|
|
|
|
// ...cityResults.map(c => c.city),
|
|
|
|
|
// ...branchResults.map(b => b.city)
|
|
|
|
|
// ])];
|
|
|
|
|
|
|
|
|
|
// reply.send({ status_code: 200, data: cityNames });
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
// console.error("Error fetching cities:", err);
|
|
|
|
|
// reply.send({ error: err.message });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
exports.getCitiesByOfficeName = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
let { officeName } = req.params;
|
|
|
|
|
|
|
|
|
|
// Trim and normalize spaces
|
|
|
|
|
officeName = officeName.trim().replace(/\s+/g, ' '); // Replace multiple spaces with one
|
|
|
|
|
const regexOfficeName = new RegExp(officeName.replace(/\s+/g, '\\s*'), "i");
|
|
|
|
|
if (!officeName) {
|
|
|
|
|
return reply.code(400).send({ error: "officeName is required" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Split by comma and normalize names
|
|
|
|
|
let officeNames = officeName.split(',').map(name =>
|
|
|
|
|
name.trim().replace(/\s+/g, ' ')
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Debugging: Check all available office names in DB
|
|
|
|
|
console.log("All Cities with Office Name:", await City.find().select("officeName city").lean());
|
|
|
|
|
console.log("All Branches with Office Name:", await Branch.find().select("officeName city").lean());
|
|
|
|
|
// Handle "All" — fetch all cities from both collections
|
|
|
|
|
if (officeNames.includes('All')) {
|
|
|
|
|
const allCityDocs = await City.find().select("city -_id").lean();
|
|
|
|
|
const allBranchDocs = await Branch.find().select("city -_id").lean();
|
|
|
|
|
|
|
|
|
|
// Query both collections with case-insensitive regex
|
|
|
|
|
const cityResults = await City.find({ officeName: { $regex: officeName, $options: "i" } }).select("city -_id").lean();
|
|
|
|
|
const branchResults = await Branch.find({ officeName: { $regex: officeName, $options: "i" } }).select("city -_id").lean();
|
|
|
|
|
const allCities = [...new Set([
|
|
|
|
|
...allCityDocs.map(doc => doc.city),
|
|
|
|
|
...allBranchDocs.map(doc => doc.city),
|
|
|
|
|
])];
|
|
|
|
|
|
|
|
|
|
allCities.unshift("ALL"); // ✅ Add "ALL" at the beginning
|
|
|
|
|
|
|
|
|
|
return reply.send({ status_code: 200, data: allCities });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build regex conditions for each office name
|
|
|
|
|
const regexConditions = officeNames.map(name => ({
|
|
|
|
|
officeName: { $regex: new RegExp(name.replace(/\s+/g, '\\s*'), 'i') }
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Query both collections
|
|
|
|
|
const cityResults = await City.find({ $or: regexConditions }).select("city -_id").lean();
|
|
|
|
|
const branchResults = await Branch.find({ $or: regexConditions }).select("city -_id").lean();
|
|
|
|
|
|
|
|
|
|
// Extract and merge unique city names
|
|
|
|
|
const cityNames = [...new Set([...cityResults.map(c => c.city), ...branchResults.map(b => b.city)])];
|
|
|
|
|
const cityNames = [...new Set([
|
|
|
|
|
...cityResults.map(c => c.city),
|
|
|
|
|
...branchResults.map(b => b.city)
|
|
|
|
|
])];
|
|
|
|
|
|
|
|
|
|
console.log("Final City Results:", cityNames);
|
|
|
|
|
cityNames.unshift("ALL"); // ✅ Add "ALL" at the beginning
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, data: cityNames });
|
|
|
|
|
} catch (err) {
|
|
|
|
@ -1265,7 +1337,6 @@ const getDepartmentsByName = async (officeName, city, departmentName) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Helper function to fetch department names by city
|
|
|
|
|
const getDepartmentNamesByCity = async (city) => {
|
|
|
|
|
try {
|
|
|
|
@ -1274,7 +1345,7 @@ const getDepartmentsByName = async (officeName, city, departmentName) => {
|
|
|
|
|
const query = {
|
|
|
|
|
$or: [
|
|
|
|
|
{ city: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } },
|
|
|
|
|
{ officeName: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } }
|
|
|
|
|
// { officeName: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } }
|
|
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|