Varun 8 months ago
commit 75d6f5d19e

@ -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 { try {
const result = await City.aggregate([ const result = await City.aggregate([
{ {
$match: { $unwind: "$city" // Convert location array into separate documents
zone: { $regex: `^${zone}$`, $options: "i" }, // Case-insensitive match for the zone
},
}, },
{ {
$unwind: "$location" // Unwind the location field if it is an array $match: {
city: { $regex: `^${city}$`, $options: "i" }, // Match city case-insensitively
},
}, },
{ {
$group: { $group: {
_id: "$zone", // Group by zone _id: {
locations: { $addToSet: { $toUpper: "$location" } }, // Collect unique locations in uppercase $toUpper: { $trim: { input: "$city" } } // Normalize city name
},
zones: { $addToSet: "$zone" } // Collect unique zones
}, },
}, },
{ {
$project: { $project: {
_id: 0, // Exclude the _id field _id: 0, // Exclude _id
zone: "$_id", // Include zone city: "$_id", // Return city name
locations: 1 // Just return the locations field as is zones: 1, // Return collected zones
}, },
}, },
]); ]);
@ -660,23 +717,20 @@ exports.addDepartment = async (request, reply) => {
return result; return result;
} catch (err) { } catch (err) {
console.error(err); console.error(err);
throw new Error("Error fetching locations."); throw new Error("Error fetching zones.");
} }
}; };
exports.getZonesByCity = async (req, reply) => {
exports.getLocationsByZone = async (req, reply) => {
try { try {
const { zone } = req.params; // Get zone from path params const { city } = req.params;
if (!zone) { if (!city) {
return reply.status(400).send({ message: "Zone is required." }); return reply.status(400).send({ message: "City is required." });
} }
const locations = await getLocationsByZone(zone); const zones = await getZonesByCitys(city);
reply.send({ status_code: 200, data: locations }); reply.send({ status_code: 200, data: zones });
} catch (err) { } catch (err) {
reply.status(500).send({ message: err.message }); reply.status(500).send({ message: err.message });
} }

@ -374,8 +374,8 @@ module.exports = function (fastify, opts, next) {
type: "object", type: "object",
required: ["city", "zone"], required: ["city", "zone"],
properties: { properties: {
zone: { type: "string" },
city: { type: "string" }, city: { type: "string" },
zone: { type: "string" },
}, },
}, },
}, },
@ -399,6 +399,21 @@ module.exports = function (fastify, opts, next) {
handler:departmentController.getLocationsByZone 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(); next();
}; };
Loading…
Cancel
Save