diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index cf516135..32c06500 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -882,20 +882,23 @@ exports.addDepartment = async (request, reply) => { const getLocationsByCityZoneOffice = async (city, zone, officeName) => { try { - // Match condition - const matchCondition = { - city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" } - }; + // Build matchCondition dynamically + const matchCondition = {}; + + // 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") { matchCondition.zone = zone.trim(); } - // Office name filter (skip if ALL or not provided) - if (officeName && officeName.trim().toUpperCase() !== "ALL") { - matchCondition.officeName = { $regex: `^${officeName.trim()}$`, $options: "i" }; - } + // Office name filter + // if (officeName && officeName.trim().toUpperCase() !== "ALL") { + // matchCondition.officeName = { $regex: `^${officeName.trim()}$`, $options: "i" }; + // } const result = await Zone.aggregate([ { @@ -933,22 +936,23 @@ const getLocationsByCityZoneOffice = async (city, zone, officeName) => { ]); 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 - if (!locations.includes("ALL")) { - locations.unshift("ALL"); + if (!allLocations.includes("ALL")) { + allLocations.unshift("ALL"); } return { - city: result[0].city, - officeName: result[0].officeName, - locations + city: city.trim().toUpperCase(), + officeName: officeName ? officeName.trim().toUpperCase() : "ALL", + locations: allLocations }; } else { return { - city, - officeName, + city: city.trim().toUpperCase(), + officeName: officeName ? officeName.trim().toUpperCase() : "ALL", locations: ["ALL"] }; }