master^2
Bhaskar 8 months ago
parent d5b68b465f
commit 52800b7dc5

@ -782,14 +782,13 @@ exports.addDepartment = async (request, reply) => {
// } // }
// }; // };
const getLocationsByCityAndZone = async (city, zone) => { const getLocationsByCityAndZone = async (city, zone) => {
try { try {
const matchCondition = { 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") { if (zone.trim().toUpperCase() !== "ALL") {
matchCondition.zone = zone.trim(); matchCondition.zone = zone.trim();
} }
@ -797,40 +796,20 @@ const getLocationsByCityAndZone = async (city, zone) => {
const result = await Branch.aggregate([ const result = await Branch.aggregate([
{ {
$project: { $project: {
city: { $toLower: { $trim: { input: "$city" } } }, // Normalize city name (lowercase & trim) city: { $toLower: { $trim: { input: "$city" } } },
zone: { $trim: { input: "$zone" } }, // Trim zone field zone: { $trim: { input: "$zone" } },
location: 1, location: 1,
}, },
}, },
{ {
$match: matchCondition, // Dynamic match condition for city & zone $match: matchCondition,
}, },
{ {
$group: { $group: {
_id: { city: "$city", zone: "$zone" }, _id: "$city",
locations: { $push: "$location" }, locations: { $push: "$location" },
}, },
}, },
{
$project: {
_id: 0,
city: "$_id.city",
zone: "$_id.zone",
locations: {
$reduce: {
input: "$locations",
initialValue: [],
in: { $concatArrays: ["$$value", "$$this"] }, // Flatten nested arrays
},
},
},
},
{
$group: {
_id: "$city", // Group all locations under the city
locations: { $push: "$locations" },
},
},
{ {
$project: { $project: {
_id: 0, _id: 0,
@ -839,26 +818,26 @@ const getLocationsByCityAndZone = async (city, zone) => {
$reduce: { $reduce: {
input: "$locations", input: "$locations",
initialValue: [], initialValue: [],
in: { $concatArrays: ["$$value", "$$this"] }, // Flatten again after merging in: { $concatArrays: ["$$value", "$$this"] },
}, },
}, },
}, },
}, },
]); ]);
console.log("Query Result:", result); // Debugging output console.log("Query Result:", result);
if (result.length) { if (result.length) {
let locations = [...new Set(result[0].locations)]; // Remove duplicates let locations = [...new Set(result[0].locations)]; // Remove duplicates
// If zone is "ALL", include "ALL" in the locations list // Ensure "ALL" is always the first element
if (zone.trim().toUpperCase() === "ALL") { if (!locations.includes("ALL")) {
locations.unshift("ALL"); locations.unshift("ALL");
} }
return { city, locations }; return { city, locations };
} else { } else {
return { city, locations: zone.trim().toUpperCase() === "ALL" ? ["ALL"] : [] }; // Return empty if no data return { city, locations: ["ALL"] }; // If no data, return only "ALL"
} }
} catch (err) { } catch (err) {
console.error(err); console.error(err);
@ -869,7 +848,7 @@ const getLocationsByCityAndZone = async (city, zone) => {
exports.getZonebasedLocations = async (req, reply) => { exports.getZonebasedLocations = async (req, reply) => {
try { try {
const { city, zone } = req.query; 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) { if (!city || !zone) {
return reply.status(400).send({ message: "City and zone are required." }); return reply.status(400).send({ message: "City and zone are required." });
@ -888,7 +867,6 @@ exports.getZonebasedLocations = async (req, reply) => {
}; };
const getLocationsByZone = async (zone) => { const getLocationsByZone = async (zone) => {
try { try {
const result = await City.aggregate([ const result = await City.aggregate([

Loading…
Cancel
Save