|
|
@ -590,9 +590,9 @@ exports.getTeamMembers = async (request, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
exports.getAllDepartments = async (request, reply) => {
|
|
|
|
exports.getAllDepartments = async (request, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { officeName, city } = request.params;
|
|
|
|
const { officeName } = request.params;
|
|
|
|
|
|
|
|
|
|
|
|
if (!officeName || !city) {
|
|
|
|
if (!officeName ) {
|
|
|
|
return reply.status(400).send({
|
|
|
|
return reply.status(400).send({
|
|
|
|
simplydata: {
|
|
|
|
simplydata: {
|
|
|
|
error: true,
|
|
|
|
error: true,
|
|
|
@ -607,28 +607,28 @@ exports.getAllDepartments = async (request, reply) => {
|
|
|
|
"i"
|
|
|
|
"i"
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// If city === "ALL" → no filter, else regex
|
|
|
|
// // If city === "ALL" → no filter, else regex
|
|
|
|
const cityFilter =
|
|
|
|
// const cityFilter =
|
|
|
|
city.toUpperCase() === "ALL"
|
|
|
|
// city.toUpperCase() === "ALL"
|
|
|
|
? {}
|
|
|
|
// ? {}
|
|
|
|
: { city: new RegExp(city.trim().replace(/\s+/g, "\\s*"), "i") };
|
|
|
|
// : { city: new RegExp(city.trim().replace(/\s+/g, "\\s*"), "i") };
|
|
|
|
|
|
|
|
|
|
|
|
// 1️⃣ Branch match (all branches for that officeName)
|
|
|
|
// 1️⃣ Branch match (all branches for that officeName)
|
|
|
|
const branchMatches = await Branch.find({
|
|
|
|
const branchMatches = await Branch.find({
|
|
|
|
officeName: nameRegex,
|
|
|
|
officeName: nameRegex,
|
|
|
|
...cityFilter,
|
|
|
|
//...cityFilter,
|
|
|
|
}).lean();
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
// 2️⃣ City (headOffice) match
|
|
|
|
// 2️⃣ City (headOffice) match
|
|
|
|
const cityMatches = await City.find({
|
|
|
|
const cityMatches = await City.find({
|
|
|
|
officeName: nameRegex,
|
|
|
|
officeName: nameRegex,
|
|
|
|
...cityFilter,
|
|
|
|
//...cityFilter,
|
|
|
|
}).lean();
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
// 3️⃣ Departments (all matching officeName + city filter)
|
|
|
|
// 3️⃣ Departments (all matching officeName + city filter)
|
|
|
|
let departments = await Deparments.find({
|
|
|
|
let departments = await Deparments.find({
|
|
|
|
officeName: nameRegex,
|
|
|
|
officeName: nameRegex,
|
|
|
|
...cityFilter,
|
|
|
|
// ...cityFilter,
|
|
|
|
}).lean();
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
// Add contactPerson to departments
|
|
|
|
// Add contactPerson to departments
|
|
|
@ -642,12 +642,12 @@ exports.getAllDepartments = async (request, reply) => {
|
|
|
|
// 🔹 Build response
|
|
|
|
// 🔹 Build response
|
|
|
|
const responseData = [{ firstName: "Self" },{ reportingManager: "Self" }];
|
|
|
|
const responseData = [{ firstName: "Self" },{ reportingManager: "Self" }];
|
|
|
|
|
|
|
|
|
|
|
|
branchMatches.forEach((br) =>
|
|
|
|
// branchMatches.forEach((br) =>
|
|
|
|
responseData.push({
|
|
|
|
// responseData.push({
|
|
|
|
officeType: "branch",
|
|
|
|
// officeType: "branch",
|
|
|
|
...br,
|
|
|
|
// ...br,
|
|
|
|
})
|
|
|
|
// })
|
|
|
|
);
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
|
|
// cityMatches.forEach((ho) =>
|
|
|
|
// cityMatches.forEach((ho) =>
|
|
|
|
// responseData.push({
|
|
|
|
// responseData.push({
|
|
|
|