From c5e9b3be5a015fae262e587d7e8b8e3dba225c51 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 12 Feb 2025 11:01:06 +0530 Subject: [PATCH] chnages --- src/controllers/departmentController.js | 43 +++++++++++-------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index b5e8244e..b9cca0b4 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -786,10 +786,10 @@ exports.addDepartment = async (request, reply) => { const getLocationsByCityAndZone = async (city, zone) => { try { const matchCondition = { - city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" }, // Case-insensitive city match + city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" }, }; - // If zone is not "ALL", filter by the specific zone + // If a specific zone (not "ALL") is provided, filter by that zone if (zone.trim().toUpperCase() !== "ALL") { matchCondition.zone = zone.trim(); } @@ -797,13 +797,13 @@ const getLocationsByCityAndZone = async (city, zone) => { const result = await Branch.aggregate([ { $project: { - city: { $toLower: { $trim: { input: "$city" } } }, // Normalize city name (lowercase & trim) - zone: { $trim: { input: "$zone" } }, // Trim zone field + city: { $toLower: { $trim: { input: "$city" } } }, + zone: { $trim: { input: "$zone" } }, location: 1, }, }, { - $match: matchCondition, // Dynamic match condition for city & zone + $match: matchCondition, }, { $group: { @@ -820,45 +820,37 @@ const getLocationsByCityAndZone = async (city, zone) => { $reduce: { input: "$locations", initialValue: [], - in: { $concatArrays: ["$$value", "$$this"] }, // Flatten nested arrays + in: { $concatArrays: ["$$value", "$$this"] }, }, }, }, }, { $group: { - _id: "$city", // Group all locations under the city - locations: { $push: "$locations" }, + _id: "$zone", + locations: { $first: "$locations" }, }, }, { $project: { _id: 0, - city: "$_id", + zone: "$_id", locations: { - $reduce: { - input: "$locations", - initialValue: [], - in: { $concatArrays: ["$$value", "$$this"] }, // Flatten again after merging - }, + $concatArrays: [["ALL"], "$locations"], // Always add "ALL" to every zone }, }, }, ]); - console.log("Query Result:", result); // Debugging output + console.log("Query Result:", result); 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 }; + return { + city, + zones: result, // Return locations grouped by zones + }; } else { - return { city, locations: zone.trim().toUpperCase() === "ALL" ? ["ALL"] : [] }; // Return empty if no data + return { city, zones: [{ zone, locations: ["ALL"] }] }; } } catch (err) { console.error(err); @@ -869,7 +861,7 @@ const getLocationsByCityAndZone = async (city, zone) => { exports.getZonebasedLocations = async (req, reply) => { try { const { city, zone } = req.query; - console.log("Received City:", `"${city}"`, "Received Zone:", `"${zone}"`); // Debugging input + console.log("Received City:", `"${city}"`, "Received Zone:", `"${zone}"`); if (!city || !zone) { return reply.status(400).send({ message: "City and zone are required." }); @@ -889,6 +881,7 @@ exports.getZonebasedLocations = async (req, reply) => { + const getLocationsByZone = async (zone) => { try { const result = await City.aggregate([