zone base locations

master^2
Bhaskar 9 months ago
parent 1e9272faab
commit 1ee73f44b6

@ -629,4 +629,56 @@ exports.addDepartment = async (request, reply) => {
} catch (err) {
reply.status(500).send({ message: err.message });
}
};
};
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: "$location" } }, // Collect unique locations in uppercase
},
},
{
$project: {
_id: 0, // Exclude the _id field
zone: "$_id", // Include zone
locations: 1 // Just return the locations field as is
},
},
]);
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 });
}
};

@ -381,6 +381,23 @@ module.exports = function (fastify, opts, next) {
},
handler:departmentController.getZonebasedLocations
});
fastify.route({
method: "GET",
url: "/api/zonebasedlocations/:zone",
schema: {
tags: ["Department"],
description: "Get the locations by zone",
summary: "Get the locations by zone",
params: {
type: "object",
properties: {
zone: { type: "string" },
},
},
},
handler:departmentController.getLocationsByZone
});
next();

Loading…
Cancel
Save