fetch the locations based on zone ANd city

master^2
Bhaskar 8 months ago
parent 1940117694
commit 586df75d02

@ -582,54 +582,63 @@ exports.addDepartment = async (request, reply) => {
// }; // };
const getLocationsByCityAndZone = async (city, zone) => { const getLocationsByCityAndZone = async (city, zone) => {
try { try {
const result = await City.aggregate([ const result = await City.aggregate([
{ {
$match: { $project: {
city: city, // Match documents with the same city city: { $trim: { input: "$city" } }, // Trim city field in DB
zone: zone, // Match documents with the same zone zone: { $trim: { input: "$zone" } }, // Trim zone field in DB
}, location: 1
}, }
{ },
$group: { {
_id: { city: "$city", zone: "$zone" }, // Group by city and zone $match: {
locations: { $push: "$location" }, // Collect all location arrays city: { $regex: `^${city.trim()}$`, $options: "i" }, // Trim & case-insensitive
}, zone: zone.trim() // Trim zone
}
},
{
$group: {
_id: { city: "$city", zone: "$zone" },
locations: { $push: "$location" },
}, },
{ },
$project: { {
_id: 0, // Exclude the _id field $project: {
city: "$_id.city", _id: 0,
zone: "$_id.zone", city: "$_id.city",
locations: { zone: "$_id.zone",
$reduce: { locations: {
input: "$locations", $reduce: {
initialValue: [], input: "$locations",
in: { $concatArrays: ["$$value", "$$this"] }, // Flatten the location arrays initialValue: [],
}, in: { $concatArrays: ["$$value", "$$this"] },
}, },
}, },
}, },
]); },
]);
return result;
} catch (err) { console.log("Query Result:", result); // Debugging output
console.error(err); return result;
throw new Error("Error fetching locations."); } catch (err) {
} console.error(err);
}; throw new Error("Error fetching locations.");
}
};
exports.getZonebasedLocations = async (req, reply) => {
try {
const { city, zone } = req.query;
console.log("Received City:", `"${city}"`, "Received Zone:", `"${zone}"`); // Debugging input
const locations = await getLocationsByCityAndZone(city.trim(), zone.trim());
reply.send({ status_code: 200, data: locations });
} catch (err) {
reply.status(500).send({ message: err.message });
}
};
exports.getZonebasedLocations = async (req, reply) => {
try {
const { city, zone } = req.query;
const locations = await getLocationsByCityAndZone(city, zone);
reply.send({ status_code: 200, data: locations });
} catch (err) {
reply.status(500).send({ message: err.message });
}
};
const getLocationsByZone = async (zone) => { const getLocationsByZone = async (zone) => {
try { try {

Loading…
Cancel
Save