|
|
@ -1267,27 +1267,68 @@ const getDepartmentsByName = async (officeName, city, departmentName) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Helper function to fetch department names by city
|
|
|
|
// Helper function to fetch department names by city
|
|
|
|
const getDepartmentNamesByCity = async (city) => {
|
|
|
|
const getDepartmentNamesByCity = async (city) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const trimmedCity = city.trim();
|
|
|
|
const trimmedCity = city.trim();
|
|
|
|
|
|
|
|
|
|
|
|
// Allow for extra whitespace before or after the city value in the database
|
|
|
|
const query = {
|
|
|
|
const query = {
|
|
|
|
$or: [
|
|
|
|
city: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" }
|
|
|
|
{ city: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } },
|
|
|
|
};
|
|
|
|
{ officeName: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } }
|
|
|
|
console.log("MongoDB Query:", JSON.stringify(query, null, 2));
|
|
|
|
]
|
|
|
|
|
|
|
|
};
|
|
|
|
const result = await Deparments.find(query)
|
|
|
|
|
|
|
|
.select("departmentName -_id")
|
|
|
|
console.log("MongoDB Query:", JSON.stringify(query, null, 2));
|
|
|
|
.lean();
|
|
|
|
|
|
|
|
|
|
|
|
const result = await Deparments.find(query)
|
|
|
|
// Remove duplicate department names
|
|
|
|
.select("departmentName -_id")
|
|
|
|
return [...new Set(result.map(doc => doc.departmentName))];
|
|
|
|
.lean();
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
console.error("Error fetching departments by city:", error);
|
|
|
|
// Remove duplicate department names
|
|
|
|
throw new Error("Error fetching departments by city.");
|
|
|
|
return [...new Set(result.map(doc => doc.departmentName))];
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
console.error("Error fetching departments by city or officeName:", error);
|
|
|
|
|
|
|
|
throw new Error("Error fetching departments by city or officeName.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getOffices = async (req, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const { officeName, city } = req.params;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Build dynamic filter
|
|
|
|
|
|
|
|
const filter = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (officeName && officeName !== 'ALL') {
|
|
|
|
|
|
|
|
// support multiple office names as comma-separated
|
|
|
|
|
|
|
|
const officeNames = officeName.split(',').map(name => name.trim());
|
|
|
|
|
|
|
|
filter.officeName = { $in: officeNames };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (city && city !== 'ALL') {
|
|
|
|
|
|
|
|
// support multiple cities as comma-separated
|
|
|
|
|
|
|
|
const cities = city.split(',').map(c => c.trim());
|
|
|
|
|
|
|
|
filter.city = { $in: cities };
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const offices = await Deparments.find(filter).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({
|
|
|
|
|
|
|
|
status_code: 200,
|
|
|
|
|
|
|
|
message: "Fetched successfully",
|
|
|
|
|
|
|
|
data: offices,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
console.error("Error in getOffices:", error);
|
|
|
|
|
|
|
|
reply.code(500).send({
|
|
|
|
|
|
|
|
status_code: 500,
|
|
|
|
|
|
|
|
message: "Internal server error",
|
|
|
|
|
|
|
|
error: error.message,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// API route handler
|
|
|
|
// API route handler
|
|
|
|
exports.getDepartmentsByCity = async (req, reply) => {
|
|
|
|
exports.getDepartmentsByCity = async (req, reply) => {
|
|
|
|