|
|
@ -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,111 @@ 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");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 1️⃣ Check Branch schema for match
|
|
|
|
|
|
|
|
const branchMatch = await Branch.findOne({
|
|
|
|
|
|
|
|
officeName: nameRegex,
|
|
|
|
|
|
|
|
city: cityRegex,
|
|
|
|
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
|
|
// Always start with Self
|
|
|
|
|
|
|
|
const responseData = [{ firstName: "Self" }, ...(departments || [])];
|
|
|
|
// 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,
|
|
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|