master^2
Bhaskar 2 months ago
parent 600a1d4ef1
commit 3927a3d5aa

@ -882,20 +882,23 @@ exports.addDepartment = async (request, reply) => {
const getLocationsByCityZoneOffice = async (city, zone, officeName) => { const getLocationsByCityZoneOffice = async (city, zone, officeName) => {
try { try {
// Match condition // Build matchCondition dynamically
const matchCondition = { const matchCondition = {};
city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" }
}; // City filter
if (city.trim().toUpperCase() !== "ALL") {
matchCondition.city = { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" };
}
// Zone filter (skip if ALL) // Zone filter
if (zone.trim().toUpperCase() !== "ALL") { if (zone.trim().toUpperCase() !== "ALL") {
matchCondition.zone = zone.trim(); matchCondition.zone = zone.trim();
} }
// Office name filter (skip if ALL or not provided) // Office name filter
if (officeName && officeName.trim().toUpperCase() !== "ALL") { // if (officeName && officeName.trim().toUpperCase() !== "ALL") {
matchCondition.officeName = { $regex: `^${officeName.trim()}$`, $options: "i" }; // matchCondition.officeName = { $regex: `^${officeName.trim()}$`, $options: "i" };
} // }
const result = await Zone.aggregate([ const result = await Zone.aggregate([
{ {
@ -933,22 +936,23 @@ const getLocationsByCityZoneOffice = async (city, zone, officeName) => {
]); ]);
if (result.length) { if (result.length) {
let locations = [...new Set(result[0].locations)]; // remove duplicates // Flatten all locations from all offices if city/zone are ALL
let allLocations = [...new Set(result.flatMap(r => r.locations))];
// Ensure "ALL" at the top // Ensure "ALL" at the top
if (!locations.includes("ALL")) { if (!allLocations.includes("ALL")) {
locations.unshift("ALL"); allLocations.unshift("ALL");
} }
return { return {
city: result[0].city, city: city.trim().toUpperCase(),
officeName: result[0].officeName, officeName: officeName ? officeName.trim().toUpperCase() : "ALL",
locations locations: allLocations
}; };
} else { } else {
return { return {
city, city: city.trim().toUpperCase(),
officeName, officeName: officeName ? officeName.trim().toUpperCase() : "ALL",
locations: ["ALL"] locations: ["ALL"]
}; };
} }

Loading…
Cancel
Save