|
|
|
@ -743,6 +743,15 @@ exports.addDepartment = async (request, reply) => {
|
|
|
|
|
|
|
|
|
|
const getLocationsByCityAndZone = async (city, zone) => {
|
|
|
|
|
try {
|
|
|
|
|
const matchCondition = {
|
|
|
|
|
city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" }, // Case-insensitive city match
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// If zone is not "ALL", filter by the specific zone
|
|
|
|
|
if (zone.trim().toUpperCase() !== "ALL") {
|
|
|
|
|
matchCondition.zone = zone.trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await Branch.aggregate([
|
|
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
@ -752,10 +761,7 @@ const getLocationsByCityAndZone = async (city, zone) => {
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$match: {
|
|
|
|
|
city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" }, // Case-insensitive & trimmed
|
|
|
|
|
zone: zone.trim(),
|
|
|
|
|
},
|
|
|
|
|
$match: matchCondition, // Dynamic match condition for city & zone
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$group: {
|
|
|
|
@ -779,15 +785,14 @@ const getLocationsByCityAndZone = async (city, zone) => {
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$group: {
|
|
|
|
|
_id: { city: "$city", zone: "$zone" }, // Merge all data into a single object
|
|
|
|
|
locations: { $push: "$locations" }, // Collect all locations
|
|
|
|
|
_id: "$city", // Group all locations under the city
|
|
|
|
|
locations: { $push: "$locations" },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
$project: {
|
|
|
|
|
_id: 0,
|
|
|
|
|
city: "$_id.city",
|
|
|
|
|
zone: "$_id.zone",
|
|
|
|
|
city: "$_id",
|
|
|
|
|
locations: {
|
|
|
|
|
$reduce: {
|
|
|
|
|
input: "$locations",
|
|
|
|
@ -811,6 +816,11 @@ exports.getZonebasedLocations = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { city, zone } = req.query;
|
|
|
|
|
console.log("Received City:", `"${city}"`, "Received Zone:", `"${zone}"`); // Debugging input
|
|
|
|
|
|
|
|
|
|
if (!city || !zone) {
|
|
|
|
|
return reply.status(400).send({ message: "City and zone are required." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const locations = await getLocationsByCityAndZone(city.trim(), zone.trim());
|
|
|
|
|
|
|
|
|
|
if (!locations) {
|
|
|
|
@ -902,19 +912,19 @@ exports.getZonebasedLocations = async (req, reply) => {
|
|
|
|
|
$project: {
|
|
|
|
|
_id: 0, // Exclude _id
|
|
|
|
|
city: "$_id", // Return city name
|
|
|
|
|
zones: 1 // Return collected zones (no sorting in aggregation)
|
|
|
|
|
zones: 1 // Return collected zones
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Sort the zones array in ascending order
|
|
|
|
|
// Add "ALL" to the zones array and sort it
|
|
|
|
|
result.forEach(item => {
|
|
|
|
|
item.zones.sort((a, b) => a - b); // Ensure zones are sorted numerically
|
|
|
|
|
item.zones = ["ALL", ...new Set(item.zones)].sort((a, b) => (a === "ALL" ? -1 : a - b));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error fetching zones:", err); // Detailed error logging
|
|
|
|
|
console.error("Error fetching zones:", err);
|
|
|
|
|
throw new Error("Error fetching zones.");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|