master^2
Bhaskar 1 month ago
parent b3260759e1
commit 42d58b89a5

@ -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({

@ -34,7 +34,7 @@ module.exports = function (fastify, opts, next) {
handler: installationController.createTeamMember, handler: installationController.createTeamMember,
}); });
fastify.get("/api/getAllDepartments/:officeName/:city", { fastify.get("/api/getAllDepartments/:officeName", {
schema: { schema: {
description: "Get full department details by department name", description: "Get full department details by department name",
tags: ["Admin"], tags: ["Admin"],
@ -43,7 +43,7 @@ fastify.get("/api/getAllDepartments/:officeName/:city", {
type: "object", type: "object",
properties: { properties: {
officeName: { type: "string", }, officeName: { type: "string", },
city: { type: "string", }, // city: { type: "string", },
}, },
// required: ["departmentName"], // required: ["departmentName"],

Loading…
Cancel
Save