master^2
Bhaskar 8 months ago
parent c5e9b3be5a
commit d5b68b465f

@ -786,10 +786,10 @@ exports.addDepartment = async (request, reply) => {
const getLocationsByCityAndZone = async (city, zone) => {
try {
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") {
matchCondition.zone = zone.trim();
}
@ -797,13 +797,13 @@ const getLocationsByCityAndZone = async (city, zone) => {
const result = await Branch.aggregate([
{
$project: {
city: { $toLower: { $trim: { input: "$city" } } },
zone: { $trim: { input: "$zone" } },
city: { $toLower: { $trim: { input: "$city" } } }, // Normalize city name (lowercase & trim)
zone: { $trim: { input: "$zone" } }, // Trim zone field
location: 1,
},
},
{
$match: matchCondition,
$match: matchCondition, // Dynamic match condition for city & zone
},
{
$group: {
@ -820,37 +820,45 @@ const getLocationsByCityAndZone = async (city, zone) => {
$reduce: {
input: "$locations",
initialValue: [],
in: { $concatArrays: ["$$value", "$$this"] },
in: { $concatArrays: ["$$value", "$$this"] }, // Flatten nested arrays
},
},
},
},
{
$group: {
_id: "$zone",
locations: { $first: "$locations" },
_id: "$city", // Group all locations under the city
locations: { $push: "$locations" },
},
},
{
$project: {
_id: 0,
zone: "$_id",
city: "$_id",
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) {
return {
city,
zones: result, // Return locations grouped by zones
};
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, zones: [{ zone, locations: ["ALL"] }] };
return { city, locations: zone.trim().toUpperCase() === "ALL" ? ["ALL"] : [] }; // Return empty if no data
}
} catch (err) {
console.error(err);
@ -861,7 +869,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}"`);
console.log("Received City:", `"${city}"`, "Received Zone:", `"${zone}"`); // Debugging input
if (!city || !zone) {
return reply.status(400).send({ message: "City and zone are required." });
@ -881,7 +889,6 @@ exports.getZonebasedLocations = async (req, reply) => {
const getLocationsByZone = async (zone) => {
try {
const result = await City.aggregate([

Loading…
Cancel
Save