|
|
|
@ -59,16 +59,6 @@ exports.adminSignUp = async (request, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check if an admin with the same phone number or username already exists
|
|
|
|
|
// const existingAdminUsername = await Admin.findOne({ username });
|
|
|
|
|
// const existingAdmin = await Admin.findOne({ phone });
|
|
|
|
|
|
|
|
|
|
// if (existingAdmin) {
|
|
|
|
|
// return reply.status(400).send({ message: 'Phone already registered' });
|
|
|
|
|
// }
|
|
|
|
|
// if (existingAdminUsername) {
|
|
|
|
|
// return reply.status(400).send({ message: 'Username already registered' });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Hash the password using bcrypt
|
|
|
|
|
const hashedPassword = await bcrypt.hash(password, 10);
|
|
|
|
@ -122,34 +112,6 @@ exports.editAdmin = async (request, reply) => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Admin Login Function (With Phone Number)
|
|
|
|
|
// exports.adminLogin = async (request, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { phone, password } = request.body;
|
|
|
|
|
|
|
|
|
|
// // Check if an admin with the phone number exists
|
|
|
|
|
// const admin = await Admin.findOne({ phone });
|
|
|
|
|
|
|
|
|
|
// if (!admin) {
|
|
|
|
|
// return reply.status(401).send({ message: 'Invalid phone number or password' });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Compare the password entered by the user with the hashed password stored in the database
|
|
|
|
|
// const isPasswordValid = await bcrypt.compare(password, admin.password);
|
|
|
|
|
|
|
|
|
|
// if (!isPasswordValid) {
|
|
|
|
|
// return reply.status(401).send({ message: 'Invalid phone number or password' });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Generate a JWT token for the authenticated admin
|
|
|
|
|
// const token = jwt.sign({ phone: admin.phone, role: 'admin' }, JWT_SECRET, { expiresIn: '1h' });
|
|
|
|
|
|
|
|
|
|
// return reply.send({ token, admin });
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
// reply.status(500).send({ message: err.message });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
exports.adminLogin = async (request, reply) => {
|
|
|
|
|
try {
|
|
|
|
@ -440,276 +402,10 @@ exports.getAllCompanys = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// exports.getBranchDetails = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { officeName } = req.params;
|
|
|
|
|
|
|
|
|
|
// const branchDetails = await Branch.find({
|
|
|
|
|
// officeName: { $regex: new RegExp(`^${officeName}$`, 'i') }
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// return reply.send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "Fetched successfully",
|
|
|
|
|
// data: branchDetails,
|
|
|
|
|
// });
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
// console.error("Error fetching branch details:", err);
|
|
|
|
|
// return reply.status(500).send({ error: "Internal server error" });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getAllOffices = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { officeName } = req.query;
|
|
|
|
|
|
|
|
|
|
// let filter = {};
|
|
|
|
|
|
|
|
|
|
// if (officeName && officeName.toUpperCase() !== "ALL") {
|
|
|
|
|
// // Partial and case-insensitive match
|
|
|
|
|
// filter.officeName = { $regex: new RegExp(officeName, "i") };
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const offices = await Branch.find(filter).lean();
|
|
|
|
|
|
|
|
|
|
// return reply.code(200).send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "Fetched successfully",
|
|
|
|
|
// data: offices
|
|
|
|
|
// });
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.error("Error fetching offices:", error);
|
|
|
|
|
// return reply.code(500).send({
|
|
|
|
|
// status_code: 500,
|
|
|
|
|
// message: "Internal server error"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// exports.getAllOffices = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { officeName } = req.query;
|
|
|
|
|
|
|
|
|
|
// if (!officeName) {
|
|
|
|
|
// return reply.code(400).send({
|
|
|
|
|
// status_code: 400,
|
|
|
|
|
// message: "officeName query param is required"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const nameRegex = new RegExp(officeName.trim(), "i");
|
|
|
|
|
|
|
|
|
|
// // Fetch head offices, branches, and departments
|
|
|
|
|
// const [headOffices, branches, departments] = await Promise.all([
|
|
|
|
|
// City.find({ officeName: nameRegex }).lean(),
|
|
|
|
|
// Branch.find({ officeName: nameRegex }).lean(),
|
|
|
|
|
// Deparments.find({ officeName: nameRegex }).lean()
|
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
|
|
// if (headOffices.length === 0 && branches.length === 0) {
|
|
|
|
|
// return reply.code(404).send({
|
|
|
|
|
// status_code: 404,
|
|
|
|
|
// message: "No offices found for the given officeName"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const allOffices = [];
|
|
|
|
|
|
|
|
|
|
// // Process head offices (with employee count)
|
|
|
|
|
// headOffices.forEach(ho => {
|
|
|
|
|
// const officeNameTrimmed = ho.officeName.trim().toLowerCase();
|
|
|
|
|
|
|
|
|
|
// // Get all department docs for this office
|
|
|
|
|
// const matchingDepartments = departments.filter(
|
|
|
|
|
// d => d.officeName?.trim().toLowerCase() === officeNameTrimmed
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// // Count employees: 1 main person per doc + sub-team members
|
|
|
|
|
// const employeeCount = matchingDepartments.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);
|
|
|
|
|
|
|
|
|
|
// allOffices.push({
|
|
|
|
|
// officeType: "headOffice",
|
|
|
|
|
// officeName: ho.officeName.trim(),
|
|
|
|
|
// city: ho.city?.trim() || "",
|
|
|
|
|
// cityId: ho.cityId || "",
|
|
|
|
|
// employeeCount,
|
|
|
|
|
// phone: ho.phone || "",
|
|
|
|
|
// address: ho.office_address1 || "",
|
|
|
|
|
// state: ho.state || "",
|
|
|
|
|
// country: ho.country || "",
|
|
|
|
|
// pincode: ho.pincode || "",
|
|
|
|
|
// email: ho.email || "",
|
|
|
|
|
// latitude:ho.latitude || "",
|
|
|
|
|
// longitude: ho.longitude || "",
|
|
|
|
|
// googleLocation: ho.googleLocation || "",
|
|
|
|
|
// createdAt: ho.createdAt || "",
|
|
|
|
|
// updatedAt: ho.updatedAt || ""
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // Process branches (no employee count here)
|
|
|
|
|
// branches.forEach(br => {
|
|
|
|
|
// allOffices.push({
|
|
|
|
|
// officeType: "branch",
|
|
|
|
|
// branchId: br.branchId || "",
|
|
|
|
|
// officeName: br.officeName?.trim() || "",
|
|
|
|
|
// city: br.city?.trim() || "",
|
|
|
|
|
// zone: br.zone || "",
|
|
|
|
|
// location: br.location || [],
|
|
|
|
|
// phone: br.phone || "",
|
|
|
|
|
// address: br.office_address1 || "",
|
|
|
|
|
// address2: br.address2 || "",
|
|
|
|
|
// state: br.state || "",
|
|
|
|
|
// country: br.country || "",
|
|
|
|
|
// pincode: br.pincode || "",
|
|
|
|
|
// email: br.email || "",
|
|
|
|
|
// contactPerson: br.nameoftheContactPerson || "",
|
|
|
|
|
// latitude:br.latitude || "",
|
|
|
|
|
// longitude: br.longitude || "",
|
|
|
|
|
// googleLocation: br.googleLocation || "",
|
|
|
|
|
// createdAt: br.createdAt || "",
|
|
|
|
|
// updatedAt: br.updatedAt || ""
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// return reply.code(200).send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "Fetched successfully",
|
|
|
|
|
// data: allOffices
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.error("Error fetching city offices:", error);
|
|
|
|
|
// return reply.code(500).send({
|
|
|
|
|
// status_code: 500,
|
|
|
|
|
// message: "Internal server error"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getAllOffices = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { officeName } = req.query;
|
|
|
|
|
|
|
|
|
|
// if (!officeName) {
|
|
|
|
|
// return reply.code(400).send({
|
|
|
|
|
// status_code: 400,
|
|
|
|
|
// message: "officeName query param is required"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// let headOffices, branches, departments;
|
|
|
|
|
|
|
|
|
|
// if (officeName.trim().toUpperCase() === "ALL") {
|
|
|
|
|
// // ✅ Fetch all without filtering
|
|
|
|
|
// [headOffices, branches, departments] = await Promise.all([
|
|
|
|
|
// City.find().lean(),
|
|
|
|
|
// Branch.find().lean(),
|
|
|
|
|
// Deparments.find().lean()
|
|
|
|
|
// ]);
|
|
|
|
|
// } else {
|
|
|
|
|
// const nameRegex = new RegExp(officeName.trim(), "i");
|
|
|
|
|
// [headOffices, branches, departments] = await Promise.all([
|
|
|
|
|
// City.find({ officeName: nameRegex }).lean(),
|
|
|
|
|
// Branch.find({ officeName: nameRegex }).lean(),
|
|
|
|
|
// Deparments.find({ officeName: nameRegex }).lean()
|
|
|
|
|
// ]);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// if (headOffices.length === 0 && branches.length === 0) {
|
|
|
|
|
// return reply.code(404).send({
|
|
|
|
|
// status_code: 404,
|
|
|
|
|
// message: "No offices found"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const allOffices = [];
|
|
|
|
|
|
|
|
|
|
// // Process head offices (with employee count)
|
|
|
|
|
// headOffices.forEach(ho => {
|
|
|
|
|
// const officeNameTrimmed = ho.officeName.trim().toLowerCase();
|
|
|
|
|
|
|
|
|
|
// // Get all department docs for this office
|
|
|
|
|
// const matchingDepartments = departments.filter(
|
|
|
|
|
// d => d.officeName?.trim().toLowerCase() === officeNameTrimmed
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// // Count employees: 1 main person per doc + sub-team members
|
|
|
|
|
// const employeeCount = matchingDepartments.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);
|
|
|
|
|
|
|
|
|
|
// allOffices.push({
|
|
|
|
|
// officeType: "headOffice",
|
|
|
|
|
// officeName: ho.officeName.trim(),
|
|
|
|
|
// city: ho.city?.trim() || "",
|
|
|
|
|
// cityId: ho.cityId || "",
|
|
|
|
|
// employeeCount,
|
|
|
|
|
// phone: ho.phone || "",
|
|
|
|
|
// address: ho.office_address1 || "",
|
|
|
|
|
// state: ho.state || "",
|
|
|
|
|
// country: ho.country || "",
|
|
|
|
|
// pincode: ho.pincode || "",
|
|
|
|
|
// email: ho.email || "",
|
|
|
|
|
// latitude: ho.latitude || "",
|
|
|
|
|
// longitude: ho.longitude || "",
|
|
|
|
|
// googleLocation: ho.googleLocation || "",
|
|
|
|
|
// createdAt: ho.createdAt || "",
|
|
|
|
|
// updatedAt: ho.updatedAt || ""
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // Process branches (no employee count here)
|
|
|
|
|
// branches.forEach(br => {
|
|
|
|
|
// allOffices.push({
|
|
|
|
|
// officeType: "branch",
|
|
|
|
|
// branchId: br.branchId || "",
|
|
|
|
|
// officeName: br.officeName?.trim() || "",
|
|
|
|
|
// city: br.city?.trim() || "",
|
|
|
|
|
// zone: br.zone || "",
|
|
|
|
|
// location: br.location || [],
|
|
|
|
|
// phone: br.phone || "",
|
|
|
|
|
// address: br.office_address1 || "",
|
|
|
|
|
// address2: br.address2 || "",
|
|
|
|
|
// state: br.state || "",
|
|
|
|
|
// country: br.country || "",
|
|
|
|
|
// pincode: br.pincode || "",
|
|
|
|
|
// email: br.email || "",
|
|
|
|
|
// contactPerson: br.nameoftheContactPerson || "",
|
|
|
|
|
// latitude: br.latitude || "",
|
|
|
|
|
// longitude: br.longitude || "",
|
|
|
|
|
// googleLocation: br.googleLocation || "",
|
|
|
|
|
// createdAt: br.createdAt || "",
|
|
|
|
|
// updatedAt: br.updatedAt || ""
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// return reply.code(200).send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "Fetched successfully",
|
|
|
|
|
// data: allOffices
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.error("Error fetching city offices:", error);
|
|
|
|
|
// return reply.code(500).send({
|
|
|
|
|
// status_code: 500,
|
|
|
|
|
// message: "Internal server error"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getAllOffices = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
@ -981,555 +677,6 @@ exports.getAllOfficesByCity = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getAllOfficesByCity = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { city } = req.query;
|
|
|
|
|
|
|
|
|
|
// if (!city) {
|
|
|
|
|
// return reply.code(400).send({
|
|
|
|
|
// status_code: 400,
|
|
|
|
|
// message: "city query param is required"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const cityRegex = new RegExp(city.trim(), "i");
|
|
|
|
|
|
|
|
|
|
// // 1) Find head offices (city schema)
|
|
|
|
|
// const headOffices = await City.find({ city: cityRegex }).lean();
|
|
|
|
|
|
|
|
|
|
// if (!headOffices.length) {
|
|
|
|
|
// return reply.code(404).send({
|
|
|
|
|
// status_code: 404,
|
|
|
|
|
// message: `No head office found for city ${city}`
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // 2) Build response for each headOffice
|
|
|
|
|
// const finalResponse = [];
|
|
|
|
|
|
|
|
|
|
// for (const ho of headOffices) {
|
|
|
|
|
// // (optional) Employee count logic
|
|
|
|
|
// const departments = await Deparments.find({ city: ho.city }).lean();
|
|
|
|
|
// const 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);
|
|
|
|
|
|
|
|
|
|
// // 3) Find branches with same officeName
|
|
|
|
|
// const branches = await Branch.find({
|
|
|
|
|
// officeName: new RegExp(ho.officeName.trim(), "i")
|
|
|
|
|
// }).lean();
|
|
|
|
|
|
|
|
|
|
// // 4) Construct office data
|
|
|
|
|
// const offices = [];
|
|
|
|
|
|
|
|
|
|
// // Head Office (from citySchema)
|
|
|
|
|
// offices.push({
|
|
|
|
|
// officeType: "headOffice",
|
|
|
|
|
// officeName: ho.officeName?.trim() || "",
|
|
|
|
|
// city: ho.city?.trim() || "",
|
|
|
|
|
// cityId: ho.cityId || "",
|
|
|
|
|
// employeeCount,
|
|
|
|
|
// phone: ho.phone || "",
|
|
|
|
|
// address: ho.office_address1 || "",
|
|
|
|
|
// address2: ho.address2 || "",
|
|
|
|
|
// state: ho.state || "",
|
|
|
|
|
// country: ho.country || "",
|
|
|
|
|
// pincode: ho.pincode || "",
|
|
|
|
|
// email: ho.email || "",
|
|
|
|
|
// latitude: ho.latitude || 0,
|
|
|
|
|
// longitude: ho.longitude || 0,
|
|
|
|
|
// googleLocation: ho.googleLocation || "",
|
|
|
|
|
// createdAt: ho.createdAt || "",
|
|
|
|
|
// updatedAt: ho.updatedAt || ""
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // Branches (from branchSchema)
|
|
|
|
|
// branches.forEach(br => {
|
|
|
|
|
// offices.push({
|
|
|
|
|
// officeType: "branchOffice",
|
|
|
|
|
// branchId: br.branchId || "",
|
|
|
|
|
// officeName: br.officeName?.trim() || "",
|
|
|
|
|
// city: br.city?.trim() || "",
|
|
|
|
|
// employeeCount, // optional: same count or separate
|
|
|
|
|
// phone: br.phone || "",
|
|
|
|
|
// address: br.office_address1 || "",
|
|
|
|
|
// address2: br.address2 || "",
|
|
|
|
|
// state: br.state || "",
|
|
|
|
|
// country: br.country || "",
|
|
|
|
|
// pincode: br.pincode || "",
|
|
|
|
|
// email: br.email || "",
|
|
|
|
|
// contactPerson: br.nameoftheContactPerson || "",
|
|
|
|
|
// latitude: br.latitude || 0,
|
|
|
|
|
// longitude: br.longitude || 0,
|
|
|
|
|
// googleLocation: br.googleLocation || "",
|
|
|
|
|
// createdAt: br.createdAt || "",
|
|
|
|
|
// updatedAt: br.updatedAt || ""
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // 5) Push into final response
|
|
|
|
|
// finalResponse.push({
|
|
|
|
|
// officeName: ho.officeName?.trim() || "",
|
|
|
|
|
// city: ho.city?.trim() || "",
|
|
|
|
|
// offices
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return reply.code(200).send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "Fetched successfully",
|
|
|
|
|
// data: finalResponse
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.error("❌ Error in getAllOfficesByCity:", error);
|
|
|
|
|
// return reply.code(500).send({
|
|
|
|
|
// status_code: 500,
|
|
|
|
|
// message: "Internal server error",
|
|
|
|
|
// error: error.message
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getAllOfficesByCity = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { city } = req.query;
|
|
|
|
|
|
|
|
|
|
// if (!city) {
|
|
|
|
|
// return reply.code(400).send({
|
|
|
|
|
// status_code: 400,
|
|
|
|
|
// message: "city query param is required"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const cityRegex = new RegExp(city.trim(), "i");
|
|
|
|
|
|
|
|
|
|
// // Fetch head offices, branches, and departments
|
|
|
|
|
// const [headOffices, branches, departments] = await Promise.all([
|
|
|
|
|
// City.find({ city: cityRegex }).lean(),
|
|
|
|
|
// Branch.find({ city: cityRegex }).lean(),
|
|
|
|
|
// Deparments.find({ city: cityRegex }).lean()
|
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
|
|
// if (!headOffices.length && !branches.length) {
|
|
|
|
|
// return reply.code(404).send({
|
|
|
|
|
// status_code: 404,
|
|
|
|
|
// message: `No offices found in city ${city}`
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const officeMap = new Map();
|
|
|
|
|
|
|
|
|
|
// // 🔹 Process Head Offices
|
|
|
|
|
// headOffices.forEach(ho => {
|
|
|
|
|
// const cityTrimmed = ho.city?.trim().toLowerCase();
|
|
|
|
|
// const matchingDepartments = departments.filter(
|
|
|
|
|
// d => d.city?.trim().toLowerCase() === cityTrimmed
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// // Count employees
|
|
|
|
|
// const employeeCount = matchingDepartments.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);
|
|
|
|
|
|
|
|
|
|
// const officeNameKey = ho.officeName?.trim().toLowerCase();
|
|
|
|
|
|
|
|
|
|
// if (!officeMap.has(officeNameKey)) {
|
|
|
|
|
// officeMap.set(officeNameKey, {
|
|
|
|
|
// officeName: ho.officeName?.trim() || "",
|
|
|
|
|
// city: ho.city?.trim() || "",
|
|
|
|
|
// offices: []
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// officeMap.get(officeNameKey).offices.push({
|
|
|
|
|
// officeType: "headOffice",
|
|
|
|
|
// officeName: ho.officeName?.trim() || "",
|
|
|
|
|
// city: ho.city?.trim() || "",
|
|
|
|
|
// cityId: ho.cityId || "",
|
|
|
|
|
// employeeCount,
|
|
|
|
|
// phone: ho.phone || "",
|
|
|
|
|
// address: ho.office_address1 || "",
|
|
|
|
|
// state: ho.state || "",
|
|
|
|
|
// country: ho.country || "",
|
|
|
|
|
// pincode: ho.pincode || "",
|
|
|
|
|
// email: ho.email || "",
|
|
|
|
|
// latitude: ho.latitude || "",
|
|
|
|
|
// longitude: ho.longitude || "",
|
|
|
|
|
// googleLocation: ho.googleLocation || "",
|
|
|
|
|
// createdAt: ho.createdAt || "",
|
|
|
|
|
// updatedAt: ho.updatedAt || ""
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // 🔹 Process Branches
|
|
|
|
|
// branches.forEach(br => {
|
|
|
|
|
// const officeNameKey = br.officeName?.trim().toLowerCase();
|
|
|
|
|
|
|
|
|
|
// if (!officeMap.has(officeNameKey)) {
|
|
|
|
|
// officeMap.set(officeNameKey, {
|
|
|
|
|
// officeName: br.officeName?.trim() || "",
|
|
|
|
|
// city: br.city?.trim() || "",
|
|
|
|
|
// offices: []
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// officeMap.get(officeNameKey).offices.push({
|
|
|
|
|
// officeType: "branch",
|
|
|
|
|
// branchId: br.branchId || "",
|
|
|
|
|
// officeName: br.officeName?.trim() || "",
|
|
|
|
|
// city: br.city?.trim() || "",
|
|
|
|
|
// zone: br.zone || "",
|
|
|
|
|
// location: Array.isArray(br.location) ? br.location : [],
|
|
|
|
|
// phone: br.phone || "",
|
|
|
|
|
// address: br.office_address1 || "",
|
|
|
|
|
// address2: br.address2 || "",
|
|
|
|
|
// state: br.state || "",
|
|
|
|
|
// country: br.country || "",
|
|
|
|
|
// pincode: br.pincode || "",
|
|
|
|
|
// email: br.email || "",
|
|
|
|
|
// contactPerson: br.nameoftheContactPerson || "",
|
|
|
|
|
// latitude: br.latitude || "",
|
|
|
|
|
// longitude: br.longitude || "",
|
|
|
|
|
// googleLocation: br.googleLocation || "",
|
|
|
|
|
// createdAt: br.createdAt || "",
|
|
|
|
|
// updatedAt: br.updatedAt || ""
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // 🔹 Final grouped response
|
|
|
|
|
// const finalResponse = Array.from(officeMap.values());
|
|
|
|
|
|
|
|
|
|
// return reply.code(200).send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "Fetched successfully",
|
|
|
|
|
// data: finalResponse
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.error("❌ Error in getAllOfficesByCity:", error);
|
|
|
|
|
// return reply.code(500).send({
|
|
|
|
|
// status_code: 500,
|
|
|
|
|
// message: "Internal server error",
|
|
|
|
|
// error: error.message
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getAllOfficesByCity = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { city } = req.query;
|
|
|
|
|
|
|
|
|
|
// if (!city) {
|
|
|
|
|
// return reply.code(400).send({
|
|
|
|
|
// status_code: 400,
|
|
|
|
|
// message: "city query param is required"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const cityRegex = new RegExp(city.trim(), "i");
|
|
|
|
|
|
|
|
|
|
// // Fetch head offices and branches in this city
|
|
|
|
|
// const headOffices = await City.find({ city: cityRegex }).lean();
|
|
|
|
|
// const branches = await Branch.find({ city: cityRegex }).lean();
|
|
|
|
|
|
|
|
|
|
// if (!headOffices.length && !branches.length) {
|
|
|
|
|
// return reply.code(404).send({
|
|
|
|
|
// status_code: 404,
|
|
|
|
|
// message: `No offices found in city ${city}`
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Group by officeName
|
|
|
|
|
// const officeMap = new Map();
|
|
|
|
|
|
|
|
|
|
// // Process head offices
|
|
|
|
|
// headOffices.forEach(ho => {
|
|
|
|
|
// const key = ho.officeName?.trim().toLowerCase();
|
|
|
|
|
// if (!officeMap.has(key)) {
|
|
|
|
|
// officeMap.set(key, {
|
|
|
|
|
// officeName: ho.officeName?.trim() || "",
|
|
|
|
|
// city: ho.city?.trim() || "",
|
|
|
|
|
// headOffices: []
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// officeMap.get(key).headOffices.push({
|
|
|
|
|
// officeType: "headOffice",
|
|
|
|
|
// city: ho.city?.trim() || "",
|
|
|
|
|
// employeeCount: ho.employeeCount || 0,
|
|
|
|
|
// phone: ho.phone || "",
|
|
|
|
|
// address: ho.address || "",
|
|
|
|
|
// state: ho.state || "",
|
|
|
|
|
// country: ho.country || "",
|
|
|
|
|
// pincode: ho.pincode || "",
|
|
|
|
|
// email: ho.email || ""
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // Process branches
|
|
|
|
|
// branches.forEach(br => {
|
|
|
|
|
// const key = br.officeName?.trim().toLowerCase();
|
|
|
|
|
// if (!officeMap.has(key)) {
|
|
|
|
|
// officeMap.set(key, {
|
|
|
|
|
// officeName: br.officeName?.trim() || "",
|
|
|
|
|
// city: br.city?.trim() || "",
|
|
|
|
|
// headOffices: []
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// officeMap.get(key).headOffices.push({
|
|
|
|
|
// officeType: "branch",
|
|
|
|
|
// branchId: br.branchId || "",
|
|
|
|
|
// city: br.city?.trim() || "",
|
|
|
|
|
// zone: br.zone || "",
|
|
|
|
|
// phone: br.phone || "",
|
|
|
|
|
// address: br.address || "",
|
|
|
|
|
// state: br.state || ""
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // Final response array
|
|
|
|
|
// const finalResponse = Array.from(officeMap.values());
|
|
|
|
|
|
|
|
|
|
// return reply.code(200).send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "Fetched successfully",
|
|
|
|
|
// data: finalResponse
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
// console.error("❌ Error in getAllOfficesByCity:", err);
|
|
|
|
|
// return reply.code(500).send({
|
|
|
|
|
// status_code: 500,
|
|
|
|
|
// message: "Internal server error",
|
|
|
|
|
// error: err.message
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getCityOffices = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { officeName } = req.query;
|
|
|
|
|
|
|
|
|
|
// if (!officeName) {
|
|
|
|
|
// return reply.code(400).send({
|
|
|
|
|
// status_code: 400,
|
|
|
|
|
// message: "officeName query param is required"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Regex filter for partial & case-insensitive match
|
|
|
|
|
// const nameRegex = new RegExp(officeName.trim(), "i");
|
|
|
|
|
|
|
|
|
|
// // Step 1: Fetch head offices matching the officeName
|
|
|
|
|
// const headOffices = await City.find({ officeName: nameRegex }).lean();
|
|
|
|
|
|
|
|
|
|
// // Step 2: Fetch branches matching the officeName
|
|
|
|
|
// const branches = await Branch.find({ officeName: nameRegex }).lean();
|
|
|
|
|
|
|
|
|
|
// // Step 3: Fetch departments matching the officeName
|
|
|
|
|
// const departments = await Deparments.find({ officeName: nameRegex }).lean();
|
|
|
|
|
|
|
|
|
|
// if (headOffices.length === 0 && branches.length === 0) {
|
|
|
|
|
// return reply.code(404).send({
|
|
|
|
|
// status_code: 404,
|
|
|
|
|
// message: "No offices found for the given officeName"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// // Step 4: Build city-wise result
|
|
|
|
|
// const cityMap = {};
|
|
|
|
|
|
|
|
|
|
// headOffices.forEach(ho => {
|
|
|
|
|
// const officeNameTrimmed = ho.officeName.trim();
|
|
|
|
|
|
|
|
|
|
// // Find department for this office to get employee count
|
|
|
|
|
// const departmentDoc = departments.find(
|
|
|
|
|
// d =>
|
|
|
|
|
// d.officeName &&
|
|
|
|
|
// d.officeName.trim().toLowerCase() === officeNameTrimmed.toLowerCase()
|
|
|
|
|
// );
|
|
|
|
|
// const employeeCount =
|
|
|
|
|
// departmentDoc?.team_member?.team_member?.length || 0;
|
|
|
|
|
|
|
|
|
|
// cityMap[ho.city.trim().toLowerCase()] = {
|
|
|
|
|
// city: ho.city.trim(),
|
|
|
|
|
// headOffice: {
|
|
|
|
|
// officeName: ho.officeName.trim(),
|
|
|
|
|
// employeeCount,
|
|
|
|
|
// phone: ho.phone || "",
|
|
|
|
|
// address: ho.office_address1 || "",
|
|
|
|
|
// state: ho.state || "",
|
|
|
|
|
// country: ho.country || "",
|
|
|
|
|
// pincode: ho.pincode || "",
|
|
|
|
|
// email: ho.email || ""
|
|
|
|
|
// },
|
|
|
|
|
// branches: []
|
|
|
|
|
// };
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // Step 5: Attach branches
|
|
|
|
|
// branches.forEach(br => {
|
|
|
|
|
// const cityKey = br.city.trim().toLowerCase();
|
|
|
|
|
// if (!cityMap[cityKey]) {
|
|
|
|
|
// cityMap[cityKey] = {
|
|
|
|
|
// city: br.city.trim(),
|
|
|
|
|
// headOffice: null,
|
|
|
|
|
// branches: []
|
|
|
|
|
// };
|
|
|
|
|
// }
|
|
|
|
|
// cityMap[cityKey].branches.push({
|
|
|
|
|
// officeName: br.officeName?.trim() || "",
|
|
|
|
|
// location: br.location || [],
|
|
|
|
|
// phone: br.phone || "",
|
|
|
|
|
// address: br.address1 || ""
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // Step 6: Convert to array
|
|
|
|
|
// const result = Object.values(cityMap);
|
|
|
|
|
|
|
|
|
|
// return reply.code(200).send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "Fetched successfully",
|
|
|
|
|
// data: result
|
|
|
|
|
// });
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.error("Error fetching city offices:", error);
|
|
|
|
|
// return reply.code(500).send({
|
|
|
|
|
// status_code: 500,
|
|
|
|
|
// message: "Internal server error"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// exports.getCityOffices = async (req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// const { officeName } = req.query;
|
|
|
|
|
|
|
|
|
|
// if (!officeName) {
|
|
|
|
|
// return reply.code(400).send({
|
|
|
|
|
// status_code: 400,
|
|
|
|
|
// message: "officeName query param is required"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const nameRegex = new RegExp(officeName.trim(), "i");
|
|
|
|
|
|
|
|
|
|
// // Step 1: Fetch head offices
|
|
|
|
|
// const headOffices = await City.find({ officeName: nameRegex }).lean();
|
|
|
|
|
|
|
|
|
|
// // Step 2: Fetch branches
|
|
|
|
|
// const branches = await Branch.find({ officeName: nameRegex }).lean();
|
|
|
|
|
|
|
|
|
|
// // Step 3: Fetch departments
|
|
|
|
|
// const departments = await Deparments.find({ officeName: nameRegex }).lean();
|
|
|
|
|
|
|
|
|
|
// if (headOffices.length === 0 && branches.length === 0) {
|
|
|
|
|
// return reply.code(404).send({
|
|
|
|
|
// status_code: 404,
|
|
|
|
|
// message: "No offices found for the given officeName"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// const cityMap = {};
|
|
|
|
|
|
|
|
|
|
// headOffices.forEach(ho => {
|
|
|
|
|
// const officeNameTrimmed = ho.officeName.trim();
|
|
|
|
|
|
|
|
|
|
// // Find department for this office to get employee count
|
|
|
|
|
// const departmentDoc = departments.find(
|
|
|
|
|
// d => d.officeName?.trim().toLowerCase() === officeNameTrimmed.toLowerCase()
|
|
|
|
|
// );
|
|
|
|
|
// console.log("departmentDoc",departmentDoc)
|
|
|
|
|
// const mainPersonCount = departmentDoc?.team_member?.main_person ? 1 : 0;
|
|
|
|
|
// const subTeamCount = Array.isArray(departmentDoc?.team_member?.team_member)
|
|
|
|
|
// ? departmentDoc.team_member.team_member.length
|
|
|
|
|
// : 0;
|
|
|
|
|
|
|
|
|
|
// const employeeCount = mainPersonCount + subTeamCount;
|
|
|
|
|
|
|
|
|
|
// cityMap[ho.city.trim().toLowerCase()] = {
|
|
|
|
|
// // cityId: ho.cityId || "", // added cityId
|
|
|
|
|
// city: ho.city.trim(),
|
|
|
|
|
// headOffice: {
|
|
|
|
|
// officeName: ho.officeName.trim(),
|
|
|
|
|
// cityId: ho.cityId || "", // added cityId
|
|
|
|
|
// employeeCount,
|
|
|
|
|
// phone: ho.phone || "",
|
|
|
|
|
// address: ho.office_address1 || "",
|
|
|
|
|
// state: ho.state || "",
|
|
|
|
|
// country: ho.country || "",
|
|
|
|
|
// pincode: ho.pincode || "",
|
|
|
|
|
// email: ho.email || ""
|
|
|
|
|
// },
|
|
|
|
|
// // branches: []
|
|
|
|
|
// };
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// // Step 5: Attach branches
|
|
|
|
|
// branches.forEach(br => {
|
|
|
|
|
// const cityKey = br.city.trim().toLowerCase();
|
|
|
|
|
|
|
|
|
|
// if (!cityMap[cityKey]) {
|
|
|
|
|
// cityMap[cityKey] = {
|
|
|
|
|
// //cityId: br.cityId || "",
|
|
|
|
|
// city: br.city.trim(),
|
|
|
|
|
// // headOffice: null,
|
|
|
|
|
// branches: []
|
|
|
|
|
// };
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// cityMap[cityKey].branches.push({
|
|
|
|
|
// branchId: br.branchId || "",
|
|
|
|
|
// officeName: br.officeName?.trim() || "",
|
|
|
|
|
// zone: br.zone || "",
|
|
|
|
|
// location: br.location || [],
|
|
|
|
|
// phone: br.phone || "",
|
|
|
|
|
// address: br.office_address1 || "",
|
|
|
|
|
// address2: br.address2 || "",
|
|
|
|
|
// state: br.state || "",
|
|
|
|
|
// country: br.country || "",
|
|
|
|
|
// pincode: br.pincode || "",
|
|
|
|
|
// email: br.email || "",
|
|
|
|
|
// contactPerson: br.nameoftheContactPerson || "",
|
|
|
|
|
// createdAt: br.createdAt || "",
|
|
|
|
|
// updatedAt: br.updatedAt || ""
|
|
|
|
|
// });
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// const result = Object.values(cityMap);
|
|
|
|
|
|
|
|
|
|
// return reply.code(200).send({
|
|
|
|
|
// status_code: 200,
|
|
|
|
|
// message: "Fetched successfully",
|
|
|
|
|
// data: result
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// } catch (error) {
|
|
|
|
|
// console.error("Error fetching city offices:", error);
|
|
|
|
|
// return reply.code(500).send({
|
|
|
|
|
// status_code: 500,
|
|
|
|
|
// message: "Internal server error"
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
exports.getCityOffices = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { officeName } = req.query;
|
|
|
|
|