|
|
@ -194,6 +194,7 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
location,
|
|
|
|
location,
|
|
|
|
city,
|
|
|
|
city,
|
|
|
|
zone,
|
|
|
|
zone,
|
|
|
|
|
|
|
|
area,
|
|
|
|
createdBy,
|
|
|
|
createdBy,
|
|
|
|
updatedBy,
|
|
|
|
updatedBy,
|
|
|
|
} = request.body;
|
|
|
|
} = request.body;
|
|
|
@ -215,6 +216,7 @@ const generateDepartmentId = async (city, departmentName) => {
|
|
|
|
location,
|
|
|
|
location,
|
|
|
|
city,
|
|
|
|
city,
|
|
|
|
zone,
|
|
|
|
zone,
|
|
|
|
|
|
|
|
area,
|
|
|
|
createdBy,
|
|
|
|
createdBy,
|
|
|
|
updatedBy,
|
|
|
|
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) => {
|
|
|
|
// const getDepartmentsByName = async (departmentName, city) => {
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
// const result = await Deparments.find({
|
|
|
|
// const result = await Deparments.find({
|
|
|
|