|
|
@ -786,10 +786,10 @@ exports.addDepartment = async (request, reply) => {
|
|
|
|
const getLocationsByCityAndZone = async (city, zone) => {
|
|
|
|
const getLocationsByCityAndZone = async (city, zone) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const matchCondition = {
|
|
|
|
const matchCondition = {
|
|
|
|
city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" }, // Case-insensitive city match
|
|
|
|
city: { $regex: `^${city.trim().toLowerCase()}$`, $options: "i" },
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// If zone is not "ALL", filter by the specific zone
|
|
|
|
// If a specific zone (not "ALL") is provided, filter by that zone
|
|
|
|
if (zone.trim().toUpperCase() !== "ALL") {
|
|
|
|
if (zone.trim().toUpperCase() !== "ALL") {
|
|
|
|
matchCondition.zone = zone.trim();
|
|
|
|
matchCondition.zone = zone.trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -797,13 +797,13 @@ const getLocationsByCityAndZone = async (city, zone) => {
|
|
|
|
const result = await Branch.aggregate([
|
|
|
|
const result = await Branch.aggregate([
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$project: {
|
|
|
|
$project: {
|
|
|
|
city: { $toLower: { $trim: { input: "$city" } } }, // Normalize city name (lowercase & trim)
|
|
|
|
city: { $toLower: { $trim: { input: "$city" } } },
|
|
|
|
zone: { $trim: { input: "$zone" } }, // Trim zone field
|
|
|
|
zone: { $trim: { input: "$zone" } },
|
|
|
|
location: 1,
|
|
|
|
location: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$match: matchCondition, // Dynamic match condition for city & zone
|
|
|
|
$match: matchCondition,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$group: {
|
|
|
|
$group: {
|
|
|
@ -820,45 +820,37 @@ const getLocationsByCityAndZone = async (city, zone) => {
|
|
|
|
$reduce: {
|
|
|
|
$reduce: {
|
|
|
|
input: "$locations",
|
|
|
|
input: "$locations",
|
|
|
|
initialValue: [],
|
|
|
|
initialValue: [],
|
|
|
|
in: { $concatArrays: ["$$value", "$$this"] }, // Flatten nested arrays
|
|
|
|
in: { $concatArrays: ["$$value", "$$this"] },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$group: {
|
|
|
|
$group: {
|
|
|
|
_id: "$city", // Group all locations under the city
|
|
|
|
_id: "$zone",
|
|
|
|
locations: { $push: "$locations" },
|
|
|
|
locations: { $first: "$locations" },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$project: {
|
|
|
|
$project: {
|
|
|
|
_id: 0,
|
|
|
|
_id: 0,
|
|
|
|
city: "$_id",
|
|
|
|
zone: "$_id",
|
|
|
|
locations: {
|
|
|
|
locations: {
|
|
|
|
$reduce: {
|
|
|
|
$concatArrays: [["ALL"], "$locations"], // Always add "ALL" to every zone
|
|
|
|
input: "$locations",
|
|
|
|
|
|
|
|
initialValue: [],
|
|
|
|
|
|
|
|
in: { $concatArrays: ["$$value", "$$this"] }, // Flatten again after merging
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
console.log("Query Result:", result); // Debugging output
|
|
|
|
console.log("Query Result:", result);
|
|
|
|
|
|
|
|
|
|
|
|
if (result.length) {
|
|
|
|
if (result.length) {
|
|
|
|
let locations = [...new Set(result[0].locations)]; // Remove duplicates
|
|
|
|
return {
|
|
|
|
|
|
|
|
city,
|
|
|
|
// If zone is "ALL", include "ALL" in the locations list
|
|
|
|
zones: result, // Return locations grouped by zones
|
|
|
|
if (zone.trim().toUpperCase() === "ALL") {
|
|
|
|
};
|
|
|
|
locations.unshift("ALL");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { city, locations };
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return { city, locations: zone.trim().toUpperCase() === "ALL" ? ["ALL"] : [] }; // Return empty if no data
|
|
|
|
return { city, zones: [{ zone, locations: ["ALL"] }] };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
console.error(err);
|
|
|
@ -869,7 +861,7 @@ const getLocationsByCityAndZone = async (city, zone) => {
|
|
|
|
exports.getZonebasedLocations = async (req, reply) => {
|
|
|
|
exports.getZonebasedLocations = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { city, zone } = req.query;
|
|
|
|
const { city, zone } = req.query;
|
|
|
|
console.log("Received City:", `"${city}"`, "Received Zone:", `"${zone}"`); // Debugging input
|
|
|
|
console.log("Received City:", `"${city}"`, "Received Zone:", `"${zone}"`);
|
|
|
|
|
|
|
|
|
|
|
|
if (!city || !zone) {
|
|
|
|
if (!city || !zone) {
|
|
|
|
return reply.status(400).send({ message: "City and zone are required." });
|
|
|
|
return reply.status(400).send({ message: "City and zone are required." });
|
|
|
@ -889,6 +881,7 @@ exports.getZonebasedLocations = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getLocationsByZone = async (zone) => {
|
|
|
|
const getLocationsByZone = async (zone) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const result = await City.aggregate([
|
|
|
|
const result = await City.aggregate([
|
|
|
|