Chnages and ALL filed added cities

master^2
Bhaskar 2 months ago
parent 7b1f207114
commit b65c5cfa1d

@ -262,7 +262,7 @@ exports.getallCompanyNames = async (req, reply) => {
.select("officeName -_id") // Select only officeName and exclude _id .select("officeName -_id") // Select only officeName and exclude _id
.exec() .exec()
.then((docs) => { .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 }); reply.send({ status_code: 200, data: officeNames, count: officeNames.length });
}) })
.catch((err) => { .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) => { exports.getCitiesByOfficeName = async (req, reply) => {
try { try {
let { officeName } = req.params; let { officeName } = req.params;
// Trim and normalize spaces if (!officeName) {
officeName = officeName.trim().replace(/\s+/g, ' '); // Replace multiple spaces with one return reply.code(400).send({ error: "officeName is required" });
const regexOfficeName = new RegExp(officeName.replace(/\s+/g, '\\s*'), "i"); }
// 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 // Handle "All" — fetch all cities from both collections
console.log("All Cities with Office Name:", await City.find().select("officeName city").lean()); if (officeNames.includes('All')) {
console.log("All Branches with Office Name:", await Branch.find().select("officeName city").lean()); 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 allCities = [...new Set([
const cityResults = await City.find({ officeName: { $regex: officeName, $options: "i" } }).select("city -_id").lean(); ...allCityDocs.map(doc => doc.city),
const branchResults = await Branch.find({ officeName: { $regex: officeName, $options: "i" } }).select("city -_id").lean(); ...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 // 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 }); reply.send({ status_code: 200, data: cityNames });
} catch (err) { } catch (err) {
@ -1265,7 +1337,6 @@ 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 {
@ -1274,7 +1345,7 @@ const getDepartmentsByName = async (officeName, city, departmentName) => {
const query = { const query = {
$or: [ $or: [
{ city: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } }, { city: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } },
{ officeName: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } } // { officeName: { $regex: `^\\s*${trimmedCity}\\s*$`, $options: "i" } }
] ]
}; };

@ -676,13 +676,13 @@ module.exports = function (fastify, opts, next) {
type: "object", type: "object",
properties: { properties: {
city: { type: "string" }, city: { type: "string" },
officeName: { type: "string" }, // officeName: { type: "string" },
}, },
required: ["city"], required: ["city"],
}, },
}, },
handler: departmentController.getOffices, handler: departmentController.getDepartmentsByCity,
}); });
fastify.route({ fastify.route({

Loading…
Cancel
Save