diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index c059a2bd..bece4ef3 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -194,6 +194,7 @@ const generateDepartmentId = async (city, departmentName) => { location, city, zone, + area, createdBy, updatedBy, } = request.body; @@ -215,6 +216,7 @@ const generateDepartmentId = async (city, departmentName) => { location, city, zone, + area, createdBy, updatedBy, }); @@ -994,9 +996,129 @@ exports.getZonebasedLocations = async (req, reply) => { } }; + const getAreasByCitys = async (city) => { + try { + const result = await Zone.aggregate([ + { + $project: { + city: { $trim: { input: "$city" } }, // Trim city field in DB + area: 1 // Keep zone field + } + }, + { + $match: { + city: { $regex: `^${city.trim()}$`, $options: "i" }, // Trim & case-insensitive + } + }, + { + $group: { + _id: { $toUpper: "$city" }, // Normalize city name + areas: { $addToSet: "$area" } // Collect unique zones + } + }, + { + $project: { + _id: 0, // Exclude _id + city: "$_id", // Return city name + areas: 1 // Return collected zones + } + } + ]); + + // Add "ALL" to the zones array and sort it + result.forEach(item => { + item.areas = ["ALL", ...new Set(item.areas)].sort((a, b) => (a === "ALL" ? -1 : a - b)); + }); + return result; + } catch (err) { + console.error("Error fetching areas:", err); + throw new Error("Error fetching areas."); + } + }; + exports.getAreasByCity = async (req, reply) => { + try { + const { city } = req.params; + + if (!city || city.trim() === "") { + return reply.status(400).send({ message: "City is required." }); + } + + const zones = await getAreasByCitys(city.trim()); // Trim input + + if (zones.length === 0) { + return reply.status(404).send({ message: "No zones found for the specified city." }); + } + + reply.send({ status_code: 200, data: zones }); + } catch (err) { + reply.status(500).send({ message: err.message }); + } + }; + + const getZonesByArea = async (area) => { + try { + const result = await Zone.aggregate([ + { + $project: { + area: { $trim: { input: "$area" } }, // Trim area field in DB + zone: 1 // Keep zone field + } + }, + { + $match: { + area: { $regex: `^${area.trim()}$`, $options: "i" } // Case-insensitive match on area + } + }, + { + $group: { + _id: { $toUpper: "$area" }, // Normalize area name + zones: { $addToSet: "$zone" } // Collect unique zones + } + }, + { + $project: { + _id: 0, // Exclude _id + area: "$_id", + zones: 1 // Return collected zones + } + } + ]); + + // ✅ Correct sorting: "ALL" first, then other zones in ascending order + result.forEach(item => { + item.zones = ["ALL", ...item.zones.filter(z => z !== "ALL").sort((a, b) => a.localeCompare(b))]; + }); + + return result; + } catch (err) { + console.error("Error fetching zones:", err); + throw new Error("Error fetching zones."); + } + }; + + + // Fastify route handler to get zones based on area only + exports.getZonesByArea = async (req, reply) => { + try { + const { area } = req.params; + + if (!area || area.trim() === "") { + return reply.status(400).send({ message: "Area is required." }); + } + + const zones = await getZonesByArea(area.trim()); + + if (zones.length === 0) { + return reply.status(404).send({ message: "No zones found for the specified area." }); + } + + reply.send({ status_code: 200, data: zones }); + } catch (err) { + reply.status(500).send({ message: err.message }); + } + }; - // const getDepartmentsByName = async (departmentName, city) => { // try { // const result = await Deparments.find({ diff --git a/src/models/Department.js b/src/models/Department.js index d56e9399..926db21b 100644 --- a/src/models/Department.js +++ b/src/models/Department.js @@ -80,6 +80,7 @@ const citySchema = new mongoose.Schema( officeName: { type: String }, zone: { type: String , default: "ALL"}, city: { type: String }, + area: { type: String }, location: [{ type : String}], createdAt: { type: Date, diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index 8cf08556..0e9e02a8 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -445,6 +445,40 @@ module.exports = function (fastify, opts, next) { handler:departmentController.getZonesByCity }); + fastify.route({ + method: "GET", + url: "/api/areabasedcity/:city", + schema: { + tags: ["Department"], + description: "Get the Areas by city", + summary: "Get the Areas by city", + params: { + type: "object", + properties: { + city: { type: "string" }, + }, + }, + }, + handler:departmentController.getAreasByCity + }); + + fastify.route({ + method: "GET", + url: "/api/areabasedzones/:area", + schema: { + tags: ["Department"], + description: "Get the Areas based zones", + summary: "Get the Areas based zones", + params: { + type: "object", + properties: { + area: { type: "string" }, + }, + }, + }, + handler:departmentController.getZonesByArea + }); + fastify.route({ method: "GET", url: "/api/departmentNamebaselist/:officeName/:city/:departmentName", @@ -537,6 +571,7 @@ module.exports = function (fastify, opts, next) { items: { type: "string" }, }, zone: { type: "string" }, + area : {type: "string"} }, }, security: [