|
|
@ -770,82 +770,102 @@ exports.getBranchDetails = async (request, reply) => {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.editdepartment = async (request, reply) => {
|
|
|
|
exports.editdepartment = async (request, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { departmentId } = request.params;
|
|
|
|
const { departmentId } = request.params;
|
|
|
|
const {
|
|
|
|
const {
|
|
|
|
|
|
|
|
phone,
|
|
|
|
phone,
|
|
|
|
alternativeContactNumber,
|
|
|
|
alternativeContactNumber,
|
|
|
|
gender,
|
|
|
|
gender,
|
|
|
|
personalEmail,
|
|
|
|
personalEmail,
|
|
|
|
city,
|
|
|
|
city,
|
|
|
|
firstName,
|
|
|
|
firstName,
|
|
|
|
lastName,
|
|
|
|
lastName,
|
|
|
|
email,
|
|
|
|
email,
|
|
|
|
reportingManager,
|
|
|
|
reportingManager,
|
|
|
|
departmentName,
|
|
|
|
departmentName,
|
|
|
|
state,
|
|
|
|
state,
|
|
|
|
country,
|
|
|
|
country,
|
|
|
|
zone,
|
|
|
|
zone,
|
|
|
|
address1,
|
|
|
|
address1,
|
|
|
|
address2,
|
|
|
|
address2,
|
|
|
|
pincode,
|
|
|
|
pincode,
|
|
|
|
desginationName,
|
|
|
|
desginationName,
|
|
|
|
personal_city,
|
|
|
|
personal_city,
|
|
|
|
reportingManager_mobile_number,
|
|
|
|
reportingManager_mobile_number,
|
|
|
|
reportingManager_email,
|
|
|
|
reportingManager_email,
|
|
|
|
officeName,
|
|
|
|
officeName,
|
|
|
|
picture,
|
|
|
|
picture,
|
|
|
|
employeeType
|
|
|
|
employeeType
|
|
|
|
} = request.body;
|
|
|
|
|
|
|
|
|
|
|
|
} = request.body;
|
|
|
|
const existing = await Deparments.findOne({ departmentId });
|
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
|
|
|
|
return reply.status(404).send({ message: "Department not found" });
|
|
|
|
const existing = await Deparments.findOne({ departmentId });
|
|
|
|
}
|
|
|
|
if (!existing) {
|
|
|
|
|
|
|
|
return reply.status(404).send({ message: 'Department not found' });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const phoneExists = await Deparments.findOne({ phone, departmentId: { $ne: departmentId } });
|
|
|
|
|
|
|
|
if (phoneExists) {
|
|
|
|
|
|
|
|
return reply.status(400).send({ message: 'Phone is already registered to another user' });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
existing.phone = phone || existing.phone;
|
|
|
|
|
|
|
|
existing.alternativeContactNumber = alternativeContactNumber || existing.alternativeContactNumber;
|
|
|
|
|
|
|
|
existing.personalEmail = personalEmail || existing.personalEmail;
|
|
|
|
|
|
|
|
existing.gender = gender || existing.gender;
|
|
|
|
|
|
|
|
existing.city = city || existing.city;
|
|
|
|
|
|
|
|
existing.state = state || existing.state;
|
|
|
|
|
|
|
|
existing.country = country || existing.country;
|
|
|
|
|
|
|
|
existing.zone = zone || existing.zone;
|
|
|
|
|
|
|
|
existing.desginationName = desginationName || existing.desginationName;
|
|
|
|
|
|
|
|
existing.pincode = pincode || existing.pincode;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
existing.address1 = address1 || existing.address1;
|
|
|
|
const phoneExists = await Deparments.findOne({
|
|
|
|
existing.address2 = address2 || existing.address2;
|
|
|
|
phone,
|
|
|
|
|
|
|
|
departmentId: { $ne: departmentId },
|
|
|
|
existing.email = email || existing.email;
|
|
|
|
});
|
|
|
|
existing.firstName = firstName || existing.firstName;
|
|
|
|
if (phoneExists) {
|
|
|
|
existing.lastName = lastName || existing.lastName;
|
|
|
|
return reply
|
|
|
|
existing.departmentName = departmentName || existing.departmentName;
|
|
|
|
.status(400)
|
|
|
|
existing.reportingManager = reportingManager || existing.reportingManager
|
|
|
|
.send({ message: "Phone is already registered to another user" });
|
|
|
|
existing.personal_city = personal_city || existing.personal_city;
|
|
|
|
}
|
|
|
|
existing.reportingManager_mobile_number = reportingManager_mobile_number || existing.reportingManager_mobile_number;
|
|
|
|
|
|
|
|
existing.reportingManager_email = reportingManager_email || existing.reportingManager_email;
|
|
|
|
|
|
|
|
existing.officeName = officeName || existing.officeName
|
|
|
|
|
|
|
|
existing.picture = picture || existing.picture
|
|
|
|
|
|
|
|
existing.employeeType = employeeType || existing.employeeType
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await existing.save();
|
|
|
|
// 🟢 Handle reportingManager "Self"
|
|
|
|
|
|
|
|
let finalReportingManager = reportingManager || existing.reportingManager;
|
|
|
|
reply.send({ message: 'Department user updated successfully' });
|
|
|
|
let finalReportingManagerMobile =
|
|
|
|
} catch (err) {
|
|
|
|
reportingManager_mobile_number || existing.reportingManager_mobile_number;
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
let finalReportingManagerEmail =
|
|
|
|
|
|
|
|
reportingManager_email || existing.reportingManager_email;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (reportingManager?.toLowerCase() === "self") {
|
|
|
|
|
|
|
|
finalReportingManager = `${firstName || existing.firstName || ""} ${
|
|
|
|
|
|
|
|
lastName || existing.lastName || ""
|
|
|
|
|
|
|
|
} - (${phone || existing.phone}) - ${city || existing.city}`;
|
|
|
|
|
|
|
|
finalReportingManagerMobile = phone || existing.phone;
|
|
|
|
|
|
|
|
finalReportingManagerEmail = email || existing.email;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 🔹 Update fields
|
|
|
|
|
|
|
|
existing.phone = phone || existing.phone;
|
|
|
|
|
|
|
|
existing.alternativeContactNumber =
|
|
|
|
|
|
|
|
alternativeContactNumber || existing.alternativeContactNumber;
|
|
|
|
|
|
|
|
existing.personalEmail = personalEmail || existing.personalEmail;
|
|
|
|
|
|
|
|
existing.gender = gender || existing.gender;
|
|
|
|
|
|
|
|
existing.city = city || existing.city;
|
|
|
|
|
|
|
|
existing.state = state || existing.state;
|
|
|
|
|
|
|
|
existing.country = country || existing.country;
|
|
|
|
|
|
|
|
existing.zone = zone || existing.zone;
|
|
|
|
|
|
|
|
existing.desginationName = desginationName || existing.desginationName;
|
|
|
|
|
|
|
|
existing.pincode = pincode || existing.pincode;
|
|
|
|
|
|
|
|
existing.address1 = address1 || existing.address1;
|
|
|
|
|
|
|
|
existing.address2 = address2 || existing.address2;
|
|
|
|
|
|
|
|
existing.email = email || existing.email;
|
|
|
|
|
|
|
|
existing.firstName = firstName || existing.firstName;
|
|
|
|
|
|
|
|
existing.lastName = lastName || existing.lastName;
|
|
|
|
|
|
|
|
existing.departmentName = departmentName || existing.departmentName;
|
|
|
|
|
|
|
|
existing.personal_city = personal_city || existing.personal_city;
|
|
|
|
|
|
|
|
existing.officeName = officeName || existing.officeName;
|
|
|
|
|
|
|
|
existing.picture = picture || existing.picture;
|
|
|
|
|
|
|
|
existing.employeeType = employeeType || existing.employeeType;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 🔹 Assign formatted reportingManager
|
|
|
|
|
|
|
|
existing.reportingManager = finalReportingManager;
|
|
|
|
|
|
|
|
existing.reportingManager_mobile_number = finalReportingManagerMobile;
|
|
|
|
|
|
|
|
existing.reportingManager_email = finalReportingManagerEmail;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await existing.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ message: "Department user updated successfully" });
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error("❌ Error in editdepartment:", err);
|
|
|
|
|
|
|
|
reply.status(500).send({ message: err.message });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getAllDesignationsParticularFields = async (req, reply) => {
|
|
|
|
// exports.getAllDesignationsParticularFields = async (req, reply) => {
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
// const departments = await Desgination.find().exec();
|
|
|
|
// const departments = await Desgination.find().exec();
|
|
|
|