From 600a1d4ef13a3448d5629a3f77df4c44ce879565 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Mon, 11 Aug 2025 12:32:14 +0530 Subject: [PATCH] Fetch locations by city and zone --- src/controllers/departmentController.js | 158 ++++++++++++++++++++---- src/routes/departmentRoute.js | 2 + 2 files changed, 133 insertions(+), 27 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 54d27cfa..cf516135 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -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." }); @@ -1039,7 +1143,7 @@ exports.getZonebasedLocations = async (req, reply) => { a === "ALL" ? -1 : b.localeCompare(a) ); }); - + return result; } catch (err) { diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index 2aa55a08..bcfca484 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -405,6 +405,8 @@ module.exports = function (fastify, opts, next) { properties: { city: { type: "string" }, zone: { type: "string" }, + officeName: { type: "string" }, + }, }, },