diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 88375e9e..7e7bc574 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -805,7 +805,19 @@ const getLocationsByCityAndZone = async (city, zone) => { ]); console.log("Query Result:", result); // Debugging output - return result.length ? result[0] : null; // Return a single object instead of an array + + if (result.length) { + let locations = [...new Set(result[0].locations)]; // Remove duplicates + + // If zone is "ALL", include "ALL" in the locations list + if (zone.trim().toUpperCase() === "ALL") { + locations.unshift("ALL"); + } + + return { city, locations }; + } else { + return { city, locations: zone.trim().toUpperCase() === "ALL" ? ["ALL"] : [] }; // Return empty if no data + } } catch (err) { console.error(err); throw new Error("Error fetching locations."); @@ -834,6 +846,7 @@ exports.getZonebasedLocations = async (req, reply) => { }; + const getLocationsByZone = async (zone) => { try { const result = await City.aggregate([