ashok 1 month ago
commit 1e9d8137f2

@ -853,99 +853,93 @@ exports.getAllOfficesByCity = async (req, reply) => {
if (!city) { if (!city) {
return reply.code(400).send({ return reply.code(400).send({
status_code: 400, status_code: 400,
message: "city query param is required" message: "city query param is required",
}); });
} }
const cityRegex = new RegExp(city.trim(), "i"); const cityRegex = new RegExp(city.trim(), "i");
// 1) Try to find head offices directly in this city // 🔹 Step 1: Find all headOffices in this city
let headOffices = await City.find({ city: cityRegex }).lean(); const headOffices = await City.find({ city: cityRegex }).lean();
// 2) If no head office, check if branch exists in that city
if (!headOffices.length) {
const branchMatches = await Branch.find({ city: cityRegex }).lean();
if (!branchMatches.length) {
return reply.code(404).send({
status_code: 404,
message: `No headOffice or branch found for city ${city}`
});
}
// Take officeName(s) from branch to find headOffice
const officeNames = [...new Set(branchMatches.map(b => b.officeName))];
headOffices = await City.find({ // 🔹 Step 2: Find all branchOffices in this city
officeName: { $in: officeNames } const branchMatches = await Branch.find({ city: cityRegex }).lean();
}).lean();
// If still no headOffice found, fallback to just using branch data if (!headOffices.length && !branchMatches.length) {
if (!headOffices.length) { return reply.code(404).send({
return reply.code(200).send({ status_code: 404,
status_code: 200, message: `No headOffice or branch found for city ${city}`,
message: "Fetched successfully (branch only, no headOffice found)", });
data: branchMatches.map(br => ({
officeName: br.officeName,
city: br.city,
offices: [br]
}))
});
}
} }
// 🔹 Step 3: Collect all unique officeNames
const officeNames = [
...new Set([
...headOffices.map((ho) => ho.officeName.trim()),
...branchMatches.map((br) => br.officeName.trim()),
]),
];
// 3) Build response for each headOffice found
const finalResponse = []; const finalResponse = [];
for (const ho of headOffices) { // 🔹 Step 4: For each officeName, gather HO + Branches
// employee count for (const name of officeNames) {
const departments = await Deparments.find({ city: ho.city }).lean(); const ho = await City.findOne({
const employeeCount = departments.reduce((count, dep) => { officeName: new RegExp(name, "i"),
const mainPerson = 1; }).lean();
const subTeamCount = Array.isArray(dep?.team_member?.team_member)
? dep.team_member.team_member.length
: 0;
return count + mainPerson + subTeamCount;
}, 0);
// find all branches for this officeName // Get employee count for headOffice (if exists)
let employeeCount = 0;
if (ho) {
const departments = await Deparments.find({ city: ho.city }).lean();
employeeCount = departments.reduce((count, dep) => {
const mainPerson = 1;
const subTeamCount = Array.isArray(dep?.team_member?.team_member)
? dep.team_member.team_member.length
: 0;
return count + mainPerson + subTeamCount;
}, 0);
}
// Get all branches for this officeName
const branches = await Branch.find({ const branches = await Branch.find({
officeName: new RegExp(ho.officeName.trim(), "i") officeName: new RegExp(name, "i"),
}).lean(); }).lean();
// build office array
const offices = []; const offices = [];
// head office // Add headOffice if found
offices.push({ if (ho) {
officeType: "headOffice", offices.push({
officeName: ho.officeName?.trim() || "", officeType: "headOffice",
city: ho.city?.trim() || "", officeName: ho.officeName?.trim() || "",
cityId: ho.cityId || "", city: ho.city?.trim() || "",
employeeCount, cityId: ho.cityId || "",
phone: ho.phone || "", employeeCount,
address: ho.office_address1 || "", phone: ho.phone || "",
address2: ho.address2 || "", address: ho.office_address1 || "",
state: ho.state || "", address2: ho.address2 || "",
country: ho.country || "", state: ho.state || "",
pincode: ho.pincode || "", country: ho.country || "",
email: ho.email || "", pincode: ho.pincode || "",
latitude: ho.latitude || 0, email: ho.email || "",
longitude: ho.longitude || 0, latitude: ho.latitude || 0,
googleLocation: ho.googleLocation || "", longitude: ho.longitude || 0,
createdAt: ho.createdAt || "", googleLocation: ho.googleLocation || "",
updatedAt: ho.updatedAt || "" createdAt: ho.createdAt || "",
}); updatedAt: ho.updatedAt || "",
});
}
// branches // Add all branchOffices
branches.forEach(br => { branches.forEach((br) => {
offices.push({ offices.push({
officeType: "branchOffice", officeType: "branchOffice",
branchId: br.branchId || "", branchId: br.branchId || "",
officeName: br.officeName?.trim() || "", officeName: br.officeName?.trim() || "",
city: br.city?.trim() || "", city: br.city?.trim() || "",
employeeCount, // optional employeeCount, // using HO employee count (optional)
phone: br.phone || "", phone: br.phone || "",
address: br.office_address1 || "", address: br.office_address1 || "",
address2: br.address2 || "", address2: br.address2 || "",
@ -958,32 +952,35 @@ exports.getAllOfficesByCity = async (req, reply) => {
longitude: br.longitude || 0, longitude: br.longitude || 0,
googleLocation: br.googleLocation || "", googleLocation: br.googleLocation || "",
createdAt: br.createdAt || "", createdAt: br.createdAt || "",
updatedAt: br.updatedAt || "" updatedAt: br.updatedAt || "",
}); });
}); });
finalResponse.push({ finalResponse.push({
// officeName: ho.officeName?.trim() || "", officeName: name,
// city: ho.city?.trim() || "", city,
offices offices,
}); });
} }
return reply.code(200).send({ return reply.code(200).send({
status_code: 200, status_code: 200,
message: "Fetched successfully", message: "Fetched successfully",
data: finalResponse data: finalResponse,
}); });
} catch (error) { } catch (error) {
console.error("❌ Error in getAllOfficesByCity:", error); console.error("❌ Error in getAllOfficesByCity:", error);
return reply.code(500).send({ return reply.code(500).send({
status_code: 500, status_code: 500,
message: "Internal server error", message: "Internal server error",
error: error.message error: error.message,
}); });
} }
}; };
// exports.getAllOfficesByCity = async (req, reply) => { // exports.getAllOfficesByCity = async (req, reply) => {
// try { // try {
// const { city } = req.query; // const { city } = req.query;

@ -6,7 +6,7 @@ module.exports = function (fastify, opts, next) {
method: "POST", method: "POST",
url: "/api/citySignup", url: "/api/citySignup",
schema: { schema: {
tags: ["Department"], tags: ["Admin"],
description: "This is for creating a new City account", description: "This is for creating a new City account",
summary: "This is for creating a new City account", summary: "This is for creating a new City account",
body: { body: {
@ -104,7 +104,7 @@ module.exports = function (fastify, opts, next) {
fastify.get("/api/getallcompanyNames", { fastify.get("/api/getallcompanyNames", {
schema: { schema: {
tags: ["Department"], tags: ["Admin"],
description: "This is for Get all Company Name in city schema ", description: "This is for Get all Company Name in city schema ",
summary: "This is for to Get all Company Name in city schema ", summary: "This is for to Get all Company Name in city schema ",
@ -212,7 +212,7 @@ fastify.route({
method: "PUT", method: "PUT",
url: "/api/updateBranchOrCompanydetails/:id", url: "/api/updateBranchOrCompanydetails/:id",
schema: { schema: {
tags: ["Department"], tags: ["Admin"],
description: "Update details of a branch or city", description: "Update details of a branch or city",
summary: "Edit department details by branchId or cityId", summary: "Edit department details by branchId or cityId",
params: { params: {
@ -549,7 +549,7 @@ fastify.route({
method: "GET", method: "GET",
url: "/api/zonebasedcity/:city/:officeName", url: "/api/zonebasedcity/:city/:officeName",
schema: { schema: {
tags: ["Department"], tags: ["Admin"],
description: "Get the zones by city and office", description: "Get the zones by city and office",
summary: "Get the zones by city and office", summary: "Get the zones by city and office",
params: { params: {
@ -621,7 +621,7 @@ fastify.route({
method: "GET", method: "GET",
url: "/api/departmentNamebaselist/:officeName/:city/:departmentName/:employeeType", url: "/api/departmentNamebaselist/:officeName/:city/:departmentName/:employeeType",
schema: { schema: {
tags: ["Department"], tags: ["Admin"],
description: "Department name based list", description: "Department name based list",
summary: "Department name based list", summary: "Department name based list",
params: { params: {
@ -658,7 +658,7 @@ fastify.route({
method: "POST", method: "POST",
url: "/api/branchSignup", url: "/api/branchSignup",
schema: { schema: {
tags: ["Department"], tags: ["Admin"],
description: "This is for creating a new Branch account", description: "This is for creating a new Branch account",
summary: "This is for creating a new Branch account", summary: "This is for creating a new Branch account",
body: { body: {
@ -699,7 +699,7 @@ fastify.route({
method: "POST", method: "POST",
url: "/api/zoneSignup", url: "/api/zoneSignup",
schema: { schema: {
tags: ["Department"], tags: ["Admin"],
description: "This is for creating a new Zone account", description: "This is for creating a new Zone account",
summary: "This is for creating a new Zone account", summary: "This is for creating a new Zone account",
body: { body: {
@ -784,7 +784,7 @@ fastify.route({
fastify.get("/api/getCitiesByOfficeName/:officeName", { fastify.get("/api/getCitiesByOfficeName/:officeName", {
schema: { schema: {
tags: ["Department"], tags: ["Admin"],
description: "This is for Get cities by OfficeName Data", description: "This is for Get cities by OfficeName Data",
summary: "This is to Get cities by OfficeName Data", summary: "This is to Get cities by OfficeName Data",
params: { params: {
@ -810,7 +810,7 @@ fastify.route({
method: "GET", method: "GET",
url: "/api/departmentNameList/:city/:officeName", url: "/api/departmentNameList/:city/:officeName",
schema: { schema: {
tags: ["Department"], tags: ["Admin"],
description: "Get a list of department names for a given city", description: "Get a list of department names for a given city",
summary: "Department names by city", summary: "Department names by city",
params: { params: {
@ -888,7 +888,7 @@ fastify.route({
method: "GET", method: "GET",
url: "/api/staffdepartments/:officeName/:city", url: "/api/staffdepartments/:officeName/:city",
schema: { schema: {
tags: ["Department"], tags: ["Admin"],
description: "This is for fetching department details based on officeName and city", description: "This is for fetching department details based on officeName and city",
summary: "This is for fetching department details based on officeName and city", summary: "This is for fetching department details based on officeName and city",
params: { params: {

@ -88,7 +88,7 @@ fastify.get("/api/getAllDepartments/:officeName/:city", {
fastify.get("/api/getTeamMembers/:officeName/:city/:departmentId", { fastify.get("/api/getTeamMembers/:officeName/:city/:departmentId", {
schema: { schema: {
description: "Get all team members under a specific department", description: "Get all team members under a specific department",
tags: ["Department"], tags: ["Admin"],
summary: "Get Team Members by Department ID", summary: "Get Team Members by Department ID",
params: { params: {
type: "object", type: "object",

Loading…
Cancel
Save