diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index b8e277c4..4719cbb5 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -631,28 +631,85 @@ exports.addDepartment = async (request, reply) => { } }; - const getLocationsByZone = async (zone) => { + const getLocationsByZone = async (zone) => { + try { + const result = await City.aggregate([ + { + $match: { + zone: { $regex: `^${zone}$`, $options: "i" }, // Case-insensitive match for the zone + }, + }, + { + $unwind: "$location" // Unwind the location field if it is an array + }, + { + $group: { + _id: "$zone", // Group by zone + locations: { + $addToSet: { + $toUpper: { $trim: { input: "$location" } } // Convert to uppercase and trim whitespace + } + }, + }, + }, + { + $project: { + _id: 0, // Exclude the _id field + zone: "$_id", // Include zone + locations: 1 // Return locations + }, + }, + ]); + + return result; + } catch (err) { + console.error(err); + throw new Error("Error fetching locations."); + } +}; + + + + exports.getLocationsByZone = async (req, reply) => { + try { + const { zone } = req.params; // Get zone from path params + + if (!zone) { + return reply.status(400).send({ message: "Zone is required." }); + } + + const locations = await getLocationsByZone(zone); + reply.send({ status_code: 200, data: locations }); + } catch (err) { + reply.status(500).send({ message: err.message }); + } + }; + + + const getZonesByCitys = async (city) => { try { const result = await City.aggregate([ { - $match: { - zone: { $regex: `^${zone}$`, $options: "i" }, // Case-insensitive match for the zone - }, + $unwind: "$city" // Convert location array into separate documents }, { - $unwind: "$location" // Unwind the location field if it is an array + $match: { + city: { $regex: `^${city}$`, $options: "i" }, // Match city case-insensitively + }, }, { $group: { - _id: "$zone", // Group by zone - locations: { $addToSet: { $toUpper: "$location" } }, // Collect unique locations in uppercase + _id: { + $toUpper: { $trim: { input: "$city" } } // Normalize city name + }, + zones: { $addToSet: "$zone" } // Collect unique zones }, }, { $project: { - _id: 0, // Exclude the _id field - zone: "$_id", // Include zone - locations: 1 // Just return the locations field as is + _id: 0, // Exclude _id + city: "$_id", // Return city name + zones: 1, // Return collected zones }, }, ]); @@ -660,23 +717,20 @@ exports.addDepartment = async (request, reply) => { return result; } catch (err) { console.error(err); - throw new Error("Error fetching locations."); + throw new Error("Error fetching zones."); } }; - - - - exports.getLocationsByZone = async (req, reply) => { + exports.getZonesByCity = async (req, reply) => { try { - const { zone } = req.params; // Get zone from path params + const { city } = req.params; - if (!zone) { - return reply.status(400).send({ message: "Zone is required." }); + if (!city) { + return reply.status(400).send({ message: "City is required." }); } - const locations = await getLocationsByZone(zone); - reply.send({ status_code: 200, data: locations }); + const zones = await getZonesByCitys(city); + reply.send({ status_code: 200, data: zones }); } catch (err) { reply.status(500).send({ message: err.message }); } diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index cfeee179..3c483985 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -374,8 +374,8 @@ module.exports = function (fastify, opts, next) { type: "object", required: ["city", "zone"], properties: { - zone: { type: "string" }, city: { type: "string" }, + zone: { type: "string" }, }, }, }, @@ -399,6 +399,21 @@ module.exports = function (fastify, opts, next) { handler:departmentController.getLocationsByZone }); - + fastify.route({ + method: "GET", + url: "/api/zonebasedcity/:city", + schema: { + tags: ["Department"], + description: "Get the zones by city", + summary: "Get the zones by city", + params: { + type: "object", + properties: { + city: { type: "string" }, + }, + }, + }, + handler:departmentController.getZonesByCity + }); next(); }; \ No newline at end of file