ashok 2 months ago
commit a7a9bad00a

@ -340,6 +340,80 @@ exports.createTeamMember = async (req, 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
// const departments = await Deparments.find({
// officeName: nameRegex,
// city: cityRegex,
// }).lean();
// const responseData = [{ firstName: "Self" }];
// if (branchMatch) {
// responseData.push({
// officeType: "branch",
// ...branchMatch
// });
// }
// if (cityMatch) {
// responseData.push({
// officeType: "headOffice",
// ...cityMatch
// });
// }
// 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;
@ -370,11 +444,17 @@ exports.getAllDepartments = async (request, reply) => {
}).lean(); }).lean();
// 3⃣ Departments // 3⃣ Departments
const departments = await Deparments.find({ let departments = await Deparments.find({
officeName: nameRegex, officeName: nameRegex,
city: cityRegex, city: cityRegex,
}).lean(); }).lean();
// 🔹 Add nameoftheContactPerson for departments
departments = departments.map(dep => ({
...dep,
nameoftheContactPerson: `${(dep.firstName || "").trim()} ${(dep.lastName || "").trim()}`.trim()
}));
const responseData = [{ firstName: "Self" }]; const responseData = [{ firstName: "Self" }];
if (branchMatch) { if (branchMatch) {
@ -390,6 +470,7 @@ exports.getAllDepartments = async (request, reply) => {
}); });
} }
// Push modified departments
responseData.push(...departments); responseData.push(...departments);
return reply.send({ return reply.send({
@ -415,7 +496,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