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 { try {
// Match condition
const matchCondition = { 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") { if (zone.trim().toUpperCase() !== "ALL") {
matchCondition.zone = zone.trim(); 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([ const result = await Zone.aggregate([
{ {
$project: { $project: {
city: { $toLower: { $trim: { input: "$city" } } }, city: { $toLower: { $trim: { input: "$city" } } },
zone: { $trim: { input: "$zone" } }, zone: { $trim: { input: "$zone" } },
location: 1, officeName: { $trim: { input: "$officeName" } },
}, location: 1
}, }
{
$match: matchCondition,
}, },
{ $match: matchCondition },
{ {
$group: { $group: {
_id: "$city", _id: {
locations: { $push: "$location" }, city: "$city",
}, officeName: "$officeName"
},
locations: { $push: "$location" }
}
}, },
{ {
$project: { $project: {
_id: 0, _id: 0,
city: "$_id", city: "$_id.city",
officeName: "$_id.officeName",
locations: { locations: {
$reduce: { $reduce: {
input: "$locations", input: "$locations",
initialValue: [], initialValue: [],
in: { $concatArrays: ["$$value", "$$this"] }, in: { $concatArrays: ["$$value", "$$this"] }
}, }
}, }
}, }
}, }
]); ]);
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
// Ensure "ALL" is always the first element // Ensure "ALL" at the top
if (!locations.includes("ALL")) { if (!locations.includes("ALL")) {
locations.unshift("ALL"); locations.unshift("ALL");
} }
return { city, locations }; return {
city: result[0].city,
officeName: result[0].officeName,
locations
};
} else { } else {
return { city, locations: ["ALL"] }; // If no data, return only "ALL" return {
city,
officeName,
locations: ["ALL"]
};
} }
} catch (err) { } catch (err) {
console.error(err); console.error(err);
@ -860,14 +960,18 @@ 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, officeName } = req.query;
console.log("Received City:", `"${city}"`, "Received Zone:", `"${zone}"`); console.log("Received City:", `"${city}"`, "Zone:", `"${zone}"`, "Office:", `"${officeName}"`);
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." });
} }
const locations = await getLocationsByCityAndZone(city.trim(), zone.trim()); const locations = await getLocationsByCityZoneOffice(
city.trim(),
zone.trim(),
officeName ? officeName.trim() : "ALL"
);
if (!locations) { if (!locations) {
return reply.send({ status_code: 404, message: "No data found." }); return reply.send({ status_code: 404, message: "No data found." });
@ -1039,7 +1143,7 @@ exports.getZonebasedLocations = async (req, reply) => {
a === "ALL" ? -1 : b.localeCompare(a) a === "ALL" ? -1 : b.localeCompare(a)
); );
}); });
return result; return result;
} catch (err) { } catch (err) {

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

Loading…
Cancel
Save