based on city to get the zones

master^2
Bhaskar 8 months ago
parent 46f30f5b39
commit 1ecd4b149c

@ -684,4 +684,55 @@ exports.addDepartment = async (request, reply) => {
reply.status(500).send({ message: err.message });
}
};
const getZonesByCitys = async (city) => {
try {
const result = await City.aggregate([
{
$unwind: "$city" // Convert location array into separate documents
},
{
$match: {
city: { $regex: `^${city}$`, $options: "i" }, // Match city case-insensitively
},
},
{
$group: {
_id: {
$toUpper: { $trim: { input: "$city" } } // Normalize city name
},
zones: { $addToSet: "$zone" } // Collect unique zones
},
},
{
$project: {
_id: 0, // Exclude _id
city: "$_id", // Return city name
zones: 1, // Return collected zones
},
},
]);
return result;
} catch (err) {
console.error(err);
throw new Error("Error fetching zones.");
}
};
exports.getZonesByCity = async (req, reply) => {
try {
const { city } = req.params;
if (!city) {
return reply.status(400).send({ message: "City is required." });
}
const zones = await getZonesByCitys(city);
reply.send({ status_code: 200, data: zones });
} catch (err) {
reply.status(500).send({ message: err.message });
}
};

@ -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();
};
Loading…
Cancel
Save