Get all department details fetch the managers list

master^2
Bhaskar 2 months ago
parent bf7f9530d1
commit d452f5b1eb

@ -2,7 +2,7 @@ const boom = require("boom");
const bcrypt = require('bcrypt'); const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken'); const jwt = require('jsonwebtoken');
const customJwtAuth = require("../customAuthJwt"); const customJwtAuth = require("../customAuthJwt");
const { Deparments } = require("../models/Department"); const { Deparments, City, Branch } = require("../models/Department");
const { Install, SensorStock, SensorQuotation, Order, Insensors, MasterSlaveData, ElectrictyWorkPictures, PlumbingWorkPictures, MaterialRecievedPictures, Support, Repairorder } = require("../models/store"); const { Install, SensorStock, SensorQuotation, Order, Insensors, MasterSlaveData, ElectrictyWorkPictures, PlumbingWorkPictures, MaterialRecievedPictures, Support, Repairorder } = require("../models/store");
const { Counter, User } = require("../models/User"); const { Counter, User } = require("../models/User");
const { IotData, Tank } = require("../models/tanks"); const { IotData, Tank } = require("../models/tanks");
@ -299,33 +299,110 @@ exports.createTeamMember = async (req, reply) => {
// } // }
// }; // };
// exports.getAllDepartments = async (request, reply) => {
// try {
// const { departmentName } = request.params;
// if (!departmentName) {
// return reply.status(400).send({
// simplydata: {
// error: true,
// message: "departmentName is required in path params",
// },
// });
// }
// // Find all departments matching departmentName
// const departments = await Deparments.find({ departmentName }).lean();
// // Always start with Self
// const responseData = [{ firstName: "Self" }, ...(departments || [])];
// return reply.send({
// simplydata: {
// error: false,
// message: departments.length
// ? "Departments retrieved successfully"
// : "No departments found with the given departmentName",
// 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 { departmentName } = request.params; const { officeName, city } = request.params;
if (!departmentName) { if (!officeName || !city) {
return reply.status(400).send({ return reply.status(400).send({
simplydata: { simplydata: {
error: true, error: true,
message: "departmentName is required in path params", message: "officeName and city are required in path params",
}, },
}); });
} }
// Find all departments matching departmentName // Prepare case-insensitive regex for matching
const departments = await Deparments.find({ departmentName }).lean(); const nameRegex = new RegExp(`^${officeName.trim()}$`, "i");
const cityRegex = new RegExp(`^${city.trim()}$`, "i");
// Always start with Self // 1⃣ Check Branch schema for match
const responseData = [{ firstName: "Self" }, ...(departments || [])]; const branchMatch = await Branch.findOne({
officeName: nameRegex,
city: cityRegex,
}).lean();
// 2⃣ Check City schema for match
const cityMatch = await City.findOne({
officeName: nameRegex,
city: cityRegex,
}).lean();
// 3⃣ Get all departments for this officeName & city
const departments = await Deparments.find({
officeName: nameRegex,
city: cityRegex,
}).lean();
// Start response data with "Self"
const responseData = [{ firstName: "Self" }];
// If a Branch or City record is found, add it right after "Self"
if (branchMatch) {
responseData.push({
officeType: "branch",
...branchMatch
});
}
if (cityMatch) {
responseData.push({
officeType: "headOffice",
...cityMatch
});
}
// Then add department docs
responseData.push(...departments);
return reply.send({ return reply.send({
simplydata: { simplydata: {
error: false, error: false,
message: departments.length message:
? "Departments retrieved successfully" departments.length || branchMatch || cityMatch
: "No departments found with the given departmentName", ? "Data retrieved successfully"
: "No data found for the given officeName and city",
data: responseData, data: responseData,
}, },
}); });

@ -32,17 +32,19 @@ module.exports = function (fastify, opts, next) {
handler: installationController.createTeamMember, handler: installationController.createTeamMember,
}); });
fastify.get("/api/getAllDepartments/:departmentName", { fastify.get("/api/getAllDepartments/:officeName/:city", {
schema: { schema: {
description: "Get full department details by department name", description: "Get full department details by department name",
tags: ["Admin"], tags: ["Admin"],
summary: "Get all department details", summary: "Get all department details fetch the managers list",
params: { params: {
type: "object", type: "object",
properties: { properties: {
departmentName: { type: "string", description: "Department name to search" }, officeName: { type: "string", },
city: { type: "string", },
}, },
required: ["departmentName"], // required: ["departmentName"],
}, },
}, },
handler: installationController.getAllDepartments, handler: installationController.getAllDepartments,

Loading…
Cancel
Save