ashok 1 month ago
commit 508394657d

@ -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) => { exports.getAllDepartments = async (request, reply) => {
try { try {
const { officeName, city } = request.params; 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 // Regex for officeName (case-insensitive, flexible spaces)
const nameRegex = new RegExp(officeName.trim().replace(/\s+/g, "\\s*"), "i"); const nameRegex = new RegExp(
const cityRegex = new RegExp(city.trim().replace(/\s+/g, "\\s*"), "i"); 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 // 1⃣ Branch match (all branches for that officeName)
const branchMatch = await Branch.findOne({ const branchMatches = await Branch.find({
officeName: nameRegex, officeName: nameRegex,
city: cityRegex, ...cityFilter,
}).lean(); }).lean();
// 2⃣ City match // 2⃣ City (headOffice) match
const cityMatch = await City.findOne({ const cityMatches = await City.find({
officeName: nameRegex, officeName: nameRegex,
city: cityRegex, ...cityFilter,
}).lean(); }).lean();
// 3⃣ Departments // 3⃣ Departments (all matching officeName + city filter)
let departments = await Deparments.find({ let departments = await Deparments.find({
officeName: nameRegex, officeName: nameRegex,
city: cityRegex, ...cityFilter,
}).lean(); }).lean();
// 🔹 Add nameoftheContactPerson for departments // Add contactPerson to departments
departments = departments.map(dep => ({ departments = departments.map((dep) => ({
...dep, ...dep,
nameoftheContactPerson: `${(dep.firstName || "").trim()} ${(dep.lastName || "").trim()}`.trim() nameoftheContactPerson: `${(dep.firstName || "").trim()} ${
(dep.lastName || "").trim()
}`.trim(),
})); }));
// 🔹 Build response
const responseData = [{ firstName: "Self" }]; const responseData = [{ firstName: "Self" }];
if (branchMatch) { branchMatches.forEach((br) =>
responseData.push({ responseData.push({
officeType: "branch", officeType: "branch",
...branchMatch ...br,
}); })
} );
if (cityMatch) {
cityMatches.forEach((ho) =>
responseData.push({ responseData.push({
officeType: "headOffice", officeType: "headOffice",
...cityMatch ...ho,
}); })
} );
// Push modified departments
responseData.push(...departments); responseData.push(...departments);
return reply.send({ return reply.send({
simplydata: { simplydata: {
error: false, error: false,
message: message:
departments.length || branchMatch || cityMatch departments.length || branchMatches.length || cityMatches.length
? "Data retrieved successfully" ? "Data retrieved successfully"
: "No data found for the given officeName and city", : "No data found for the given officeName and city",
data: responseData, data: responseData,
}, },
}); });
} catch (err) { } catch (err) {
console.error("Error fetching departments:", err); console.error("Error fetching departments:", err);
return reply.status(500).send({ return reply.status(500).send({
@ -541,7 +632,6 @@ exports.getAllDepartments = async (request, reply) => {
} }
}; };
exports.assignTeamMemberToQuotation = async (request, reply) => { exports.assignTeamMemberToQuotation = async (request, reply) => {
try { try {
const { installationId } = request.params; const { installationId } = request.params;

Loading…
Cancel
Save