Fetch locations by city and zone

master^2
Bhaskar 2 months ago
parent da23cbd364
commit 600a1d4ef1

@ -795,62 +795,162 @@ exports.addDepartment = async (request, reply) => {
// }
// };
const getLocationsByCityAndZone = async (city, zone) => {
// const getLocationsByCityAndZone = async (city, zone) => {
// try {
// const matchCondition = {
// city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" },
// };
// // If a specific zone (not "ALL") is provided, filter by that zone
// if (zone.trim().toUpperCase() !== "ALL") {
// matchCondition.zone = zone.trim();
// }
// const result = await Zone.aggregate([
// {
// $project: {
// city: { $toLower: { $trim: { input: "$city" } } },
// zone: { $trim: { input: "$zone" } },
// location: 1,
// },
// },
// {
// $match: matchCondition,
// },
// {
// $group: {
// _id: "$city",
// locations: { $push: "$location" },
// },
// },
// {
// $project: {
// _id: 0,
// city: "$_id",
// locations: {
// $reduce: {
// input: "$locations",
// initialValue: [],
// in: { $concatArrays: ["$$value", "$$this"] },
// },
// },
// },
// },
// ]);
// console.log("Query Result:", result);
// if (result.length) {
// let locations = [...new Set(result[0].locations)]; // Remove duplicates
// // Ensure "ALL" is always the first element
// if (!locations.includes("ALL")) {
// locations.unshift("ALL");
// }
// return { city, locations };
// } else {
// return { city, locations: ["ALL"] }; // If no data, return only "ALL"
// }
// } 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}"`);
// if (!city || !zone) {
// return reply.status(400).send({ message: "City and zone are required." });
// }
// const locations = await getLocationsByCityAndZone(city.trim(), zone.trim());
// if (!locations) {
// return reply.send({ status_code: 404, message: "No data found." });
// }
// reply.send({ status_code: 200, data: locations });
// } catch (err) {
// reply.status(500).send({ message: err.message });
// }
// };
const getLocationsByCityZoneOffice = async (city, zone, officeName) => {
try {
// Match condition
const matchCondition = {
city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" },
city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" }
};
// If a specific zone (not "ALL") is provided, filter by that zone
// Zone filter (skip if ALL)
if (zone.trim().toUpperCase() !== "ALL") {
matchCondition.zone = zone.trim();
}
// Office name filter (skip if ALL or not provided)
if (officeName && officeName.trim().toUpperCase() !== "ALL") {
matchCondition.officeName = { $regex: `^${officeName.trim()}$`, $options: "i" };
}
const result = await Zone.aggregate([
{
$project: {
city: { $toLower: { $trim: { input: "$city" } } },
zone: { $trim: { input: "$zone" } },
location: 1,
},
},
{
$match: matchCondition,
officeName: { $trim: { input: "$officeName" } },
location: 1
}
},
{ $match: matchCondition },
{
$group: {
_id: "$city",
locations: { $push: "$location" },
},
_id: {
city: "$city",
officeName: "$officeName"
},
locations: { $push: "$location" }
}
},
{
$project: {
_id: 0,
city: "$_id",
city: "$_id.city",
officeName: "$_id.officeName",
locations: {
$reduce: {
input: "$locations",
initialValue: [],
in: { $concatArrays: ["$$value", "$$this"] },
},
},
},
},
in: { $concatArrays: ["$$value", "$$this"] }
}
}
}
}
]);
console.log("Query Result:", result);
if (result.length) {
let locations = [...new Set(result[0].locations)]; // Remove duplicates
let locations = [...new Set(result[0].locations)]; // remove duplicates
// Ensure "ALL" is always the first element
// Ensure "ALL" at the top
if (!locations.includes("ALL")) {
locations.unshift("ALL");
}
return { city, locations };
return {
city: result[0].city,
officeName: result[0].officeName,
locations
};
} else {
return { city, locations: ["ALL"] }; // If no data, return only "ALL"
return {
city,
officeName,
locations: ["ALL"]
};
}
} catch (err) {
console.error(err);
@ -860,14 +960,18 @@ const getLocationsByCityAndZone = async (city, zone) => {
exports.getZonebasedLocations = async (req, reply) => {
try {
const { city, zone } = req.query;
console.log("Received City:", `"${city}"`, "Received Zone:", `"${zone}"`);
const { city, zone, officeName } = req.query;
console.log("Received City:", `"${city}"`, "Zone:", `"${zone}"`, "Office:", `"${officeName}"`);
if (!city || !zone) {
return reply.status(400).send({ message: "City and zone are required." });
}
const locations = await getLocationsByCityAndZone(city.trim(), zone.trim());
const locations = await getLocationsByCityZoneOffice(
city.trim(),
zone.trim(),
officeName ? officeName.trim() : "ALL"
);
if (!locations) {
return reply.send({ status_code: 404, message: "No data found." });

@ -405,6 +405,8 @@ module.exports = function (fastify, opts, next) {
properties: {
city: { type: "string" },
zone: { type: "string" },
officeName: { type: "string" },
},
},
},

Loading…
Cancel
Save