|
|
|
@ -81,7 +81,9 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
|
|
|
|
|
|
// Generate departmentId based on departmentName
|
|
|
|
|
// const prefix = departmentName.substring(0, 2).toUpperCase(); // Extract first two letters and convert to uppercase
|
|
|
|
|
const cityId = await generateCityId();
|
|
|
|
|
const c_id = await generateCityId();
|
|
|
|
|
const cityId = `AWCI${c_id}`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check for existing department
|
|
|
|
|
const existingStore = await City.findOne({ cityId });
|
|
|
|
@ -953,47 +955,53 @@ exports.getZonebasedLocations = async (req, reply) => {
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
const getDepartmentsByName = async (departmentName, city) => {
|
|
|
|
|
try {
|
|
|
|
|
const trimmedDepartment = departmentName.trim();
|
|
|
|
|
const trimmedCity = city.trim();
|
|
|
|
|
|
|
|
|
|
const query = {
|
|
|
|
|
departmentName: { $regex: trimmedDepartment, $options: "i" },
|
|
|
|
|
city: { $regex: trimmedCity, $options: "i" }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
console.log("MongoDB Query:", JSON.stringify(query, null, 2));
|
|
|
|
|
|
|
|
|
|
const result = await Deparments.find(query).lean();
|
|
|
|
|
|
|
|
|
|
console.log("Query Result:", result);
|
|
|
|
|
return result;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error fetching department data:", err);
|
|
|
|
|
throw new Error("Error fetching department data.");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Updated helper function that accepts all three parameters
|
|
|
|
|
const getDepartmentsByName = async (officeName, city, departmentName) => {
|
|
|
|
|
try {
|
|
|
|
|
// Trim all parameters
|
|
|
|
|
const trimmedOfficeName = officeName.trim();
|
|
|
|
|
const trimmedCity = city.trim();
|
|
|
|
|
const trimmedDepartment = departmentName.trim();
|
|
|
|
|
|
|
|
|
|
const query = {
|
|
|
|
|
officeName: { $regex: trimmedOfficeName, $options: "i" },
|
|
|
|
|
departmentName: { $regex: trimmedDepartment, $options: "i" },
|
|
|
|
|
city: { $regex: trimmedCity, $options: "i" }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
console.log("MongoDB Query:", JSON.stringify(query, null, 2));
|
|
|
|
|
|
|
|
|
|
const result = await Deparments.find(query).lean();
|
|
|
|
|
|
|
|
|
|
console.log("Query Result:", result);
|
|
|
|
|
return result;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error fetching department data:", err);
|
|
|
|
|
throw new Error("Error fetching department data.");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// API Route
|
|
|
|
|
exports.getDepartments = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
console.log("Request Params:", req.params); // Debugging log
|
|
|
|
|
|
|
|
|
|
let { departmentName, city } = req.params;
|
|
|
|
|
let { departmentName, city, officeName } = req.params;
|
|
|
|
|
|
|
|
|
|
if (!departmentName || !city) {
|
|
|
|
|
return reply.status(400).send({ message: "Department Name and City are required." });
|
|
|
|
|
if (!departmentName || !city || !officeName) {
|
|
|
|
|
return reply.status(400).send({ message: "Department Name, City, and Office Name are required." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
departmentName = departmentName.trim();
|
|
|
|
|
city = city.trim();
|
|
|
|
|
officeName = officeName.trim();
|
|
|
|
|
|
|
|
|
|
const departments = await getDepartmentsByName(departmentName, city);
|
|
|
|
|
// Note the order: officeName, city, departmentName
|
|
|
|
|
const departments = await getDepartmentsByName(officeName, city, departmentName);
|
|
|
|
|
|
|
|
|
|
if (departments.length === 0) {
|
|
|
|
|
return reply.status(404).send({ message: "No departments found for the specified name and city." });
|
|
|
|
|
return reply.status(404).send({ message: "No departments found for the specified parameters." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, data: departments });
|
|
|
|
@ -1078,4 +1086,48 @@ exports.getZonebasedLocations = async (req, reply) => {
|
|
|
|
|
reply.send({ error: err.message });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Helper function to fetch department names by city
|
|
|
|
|
const getDepartmentNamesByCity = async (city) => {
|
|
|
|
|
try {
|
|
|
|
|
const trimmedCity = city.trim();
|
|
|
|
|
|
|
|
|
|
// Allow for extra whitespace before or after the city value in the database
|
|
|
|
|
const query = {
|
|
|
|
|
city: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" }
|
|
|
|
|
};
|
|
|
|
|
console.log("MongoDB Query:", JSON.stringify(query, null, 2));
|
|
|
|
|
|
|
|
|
|
const result = await Deparments.find(query)
|
|
|
|
|
.select("departmentName -_id")
|
|
|
|
|
.lean();
|
|
|
|
|
|
|
|
|
|
return result.map(doc => doc.departmentName);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error fetching departments by city:", error);
|
|
|
|
|
throw new Error("Error fetching departments by city.");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// API route handler
|
|
|
|
|
exports.getDepartmentsByCity = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { city } = req.params;
|
|
|
|
|
if (!city || city.trim() === "") {
|
|
|
|
|
return reply.status(400).send({ message: "City is required." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const departmentNames = await getDepartmentNamesByCity(city);
|
|
|
|
|
|
|
|
|
|
if (departmentNames.length === 0) {
|
|
|
|
|
return reply.status(404).send({ message: "No departments found for the specified city." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, data: departmentNames });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("API Error:", error);
|
|
|
|
|
reply.status(500).send({ message: error.message });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|