master^2
Bhaskar 8 months ago
parent c5e9b3be5a
commit d5b68b465f

@ -786,10 +786,10 @@ 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" }, city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" }, // Case-insensitive city match
}; };
// If a specific zone (not "ALL") is provided, filter by that zone // If zone is not "ALL", filter by the specific zone
if (zone.trim().toUpperCase() !== "ALL") { if (zone.trim().toUpperCase() !== "ALL") {
matchCondition.zone = zone.trim(); matchCondition.zone = zone.trim();
} }
@ -797,13 +797,13 @@ const getLocationsByCityAndZone = async (city, zone) => {
const result = await Branch.aggregate([ const result = await Branch.aggregate([
{ {
$project: { $project: {
city: { $toLower: { $trim: { input: "$city" } } }, city: { $toLower: { $trim: { input: "$city" } } }, // Normalize city name (lowercase & trim)
zone: { $trim: { input: "$zone" } }, zone: { $trim: { input: "$zone" } }, // Trim zone field
location: 1, location: 1,
}, },
}, },
{ {
$match: matchCondition, $match: matchCondition, // Dynamic match condition for city & zone
}, },
{ {
$group: { $group: {
@ -820,37 +820,45 @@ const getLocationsByCityAndZone = async (city, zone) => {
$reduce: { $reduce: {
input: "$locations", input: "$locations",
initialValue: [], initialValue: [],
in: { $concatArrays: ["$$value", "$$this"] }, in: { $concatArrays: ["$$value", "$$this"] }, // Flatten nested arrays
}, },
}, },
}, },
}, },
{ {
$group: { $group: {
_id: "$zone", _id: "$city", // Group all locations under the city
locations: { $first: "$locations" }, locations: { $push: "$locations" },
}, },
}, },
{ {
$project: { $project: {
_id: 0, _id: 0,
zone: "$_id", city: "$_id",
locations: { locations: {
$concatArrays: [["ALL"], "$locations"], // Always add "ALL" to every zone $reduce: {
input: "$locations",
initialValue: [],
in: { $concatArrays: ["$$value", "$$this"] }, // Flatten again after merging
},
}, },
}, },
}, },
]); ]);
console.log("Query Result:", result); console.log("Query Result:", result); // Debugging output
if (result.length) { if (result.length) {
return { let locations = [...new Set(result[0].locations)]; // Remove duplicates
city,
zones: result, // Return locations grouped by zones // If zone is "ALL", include "ALL" in the locations list
}; if (zone.trim().toUpperCase() === "ALL") {
locations.unshift("ALL");
}
return { city, locations };
} else { } else {
return { city, zones: [{ zone, locations: ["ALL"] }] }; return { city, locations: zone.trim().toUpperCase() === "ALL" ? ["ALL"] : [] }; // Return empty if no data
} }
} catch (err) { } catch (err) {
console.error(err); console.error(err);
@ -861,7 +869,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}"`); console.log("Received City:", `"${city}"`, "Received Zone:", `"${zone}"`); // Debugging input
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." });
@ -881,7 +889,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