chnages on Get all department details fetch the managers list

master^2
Bhaskar 1 month ago
parent dba910c446
commit 314ed6cb41

@ -460,6 +460,87 @@ exports.getTeamMembers = async (request, reply) => {
// exports.getAllDepartments = async (request, reply) => {
// try {
// const { officeName, city } = request.params;
// if (!officeName || !city) {
// return reply.status(400).send({
// simplydata: {
// error: true,
// message: "officeName and city are required in path params",
// },
// });
// }
// // Case-insensitive regex without start/end anchors to avoid trailing space issues
// const nameRegex = new RegExp(officeName.trim().replace(/\s+/g, "\\s*"), "i");
// const cityRegex = new RegExp(city.trim().replace(/\s+/g, "\\s*"), "i");
// // 1⃣ Branch match
// const branchMatch = await Branch.findOne({
// officeName: nameRegex,
// city: cityRegex,
// }).lean();
// // 2⃣ City match
// const cityMatch = await City.findOne({
// officeName: nameRegex,
// city: cityRegex,
// }).lean();
// // 3⃣ Departments
// let departments = await Deparments.find({
// officeName: nameRegex,
// city: cityRegex,
// }).lean();
// // 🔹 Add nameoftheContactPerson for departments
// departments = departments.map(dep => ({
// ...dep,
// nameoftheContactPerson: `${(dep.firstName || "").trim()} ${(dep.lastName || "").trim()}`.trim()
// }));
// const responseData = [{ firstName: "Self" }];
// if (branchMatch) {
// responseData.push({
// officeType: "branch",
// ...branchMatch
// });
// }
// if (cityMatch) {
// responseData.push({
// officeType: "headOffice",
// ...cityMatch
// });
// }
// // Push modified departments
// responseData.push(...departments);
// return reply.send({
// simplydata: {
// error: false,
// message:
// departments.length || branchMatch || cityMatch
// ? "Data retrieved successfully"
// : "No data found for the given officeName and city",
// data: responseData,
// },
// });
// } catch (err) {
// console.error("Error fetching departments:", err);
// return reply.status(500).send({
// simplydata: {
// error: true,
// message: "Internal server error",
// },
// });
// }
// };
exports.getAllDepartments = async (request, reply) => {
try {
const { officeName, city } = request.params;
@ -473,63 +554,73 @@ exports.getAllDepartments = async (request, reply) => {
});
}
// Case-insensitive regex without start/end anchors to avoid trailing space issues
const nameRegex = new RegExp(officeName.trim().replace(/\s+/g, "\\s*"), "i");
const cityRegex = new RegExp(city.trim().replace(/\s+/g, "\\s*"), "i");
// Regex for officeName (case-insensitive, flexible spaces)
const nameRegex = new RegExp(
officeName.trim().replace(/\s+/g, "\\s*"),
"i"
);
// If city === "ALL" → no filter, else regex
const cityFilter =
city.toUpperCase() === "ALL"
? {}
: { city: new RegExp(city.trim().replace(/\s+/g, "\\s*"), "i") };
// 1⃣ Branch match
const branchMatch = await Branch.findOne({
// 1⃣ Branch match (all branches for that officeName)
const branchMatches = await Branch.find({
officeName: nameRegex,
city: cityRegex,
...cityFilter,
}).lean();
// 2⃣ City match
const cityMatch = await City.findOne({
// 2⃣ City (headOffice) match
const cityMatches = await City.find({
officeName: nameRegex,
city: cityRegex,
...cityFilter,
}).lean();
// 3⃣ Departments
// 3⃣ Departments (all matching officeName + city filter)
let departments = await Deparments.find({
officeName: nameRegex,
city: cityRegex,
...cityFilter,
}).lean();
// 🔹 Add nameoftheContactPerson for departments
departments = departments.map(dep => ({
// Add contactPerson to departments
departments = departments.map((dep) => ({
...dep,
nameoftheContactPerson: `${(dep.firstName || "").trim()} ${(dep.lastName || "").trim()}`.trim()
nameoftheContactPerson: `${(dep.firstName || "").trim()} ${
(dep.lastName || "").trim()
}`.trim(),
}));
// 🔹 Build response
const responseData = [{ firstName: "Self" }];
if (branchMatch) {
branchMatches.forEach((br) =>
responseData.push({
officeType: "branch",
...branchMatch
});
}
if (cityMatch) {
...br,
})
);
cityMatches.forEach((ho) =>
responseData.push({
officeType: "headOffice",
...cityMatch
});
}
...ho,
})
);
// Push modified departments
responseData.push(...departments);
return reply.send({
simplydata: {
error: false,
message:
departments.length || branchMatch || cityMatch
departments.length || branchMatches.length || cityMatches.length
? "Data retrieved successfully"
: "No data found for the given officeName and city",
data: responseData,
},
});
} catch (err) {
console.error("Error fetching departments:", err);
return reply.status(500).send({
@ -541,7 +632,6 @@ exports.getAllDepartments = async (request, reply) => {
}
};
exports.assignTeamMemberToQuotation = async (request, reply) => {
try {
const { installationId } = request.params;

Loading…
Cancel
Save