ashok 1 month ago
commit acf361b1c7

@ -520,90 +520,103 @@ exports.getallCompanyNames = async (req, reply) => {
exports.addDepartment = async (request, reply) => {
try {
const {
phone,
officeName,
alternativeContactNumber,
gender,
personalEmail,
city,
personal_city,
reportingManager_mobile_number,
reportingManager_email,
firstName,
lastName,
departmentName,
reportingManager,
email,
state,
password,
country,
zone,
address1,
address2,
pincode,
desginationName,
location,
picture,
dateOfJoin,
employeeType,
createdBy,
updatedBy,
} = request.body;
try {
const {
phone,
officeName,
alternativeContactNumber,
gender,
personalEmail,
city,
personal_city,
reportingManager_mobile_number,
reportingManager_email,
firstName,
lastName,
departmentName,
reportingManager,
email,
state,
password,
country,
zone,
address1,
address2,
pincode,
desginationName,
location,
picture,
dateOfJoin,
employeeType,
createdBy,
updatedBy,
} = request.body;
// Generate departmentId
const departmentId = await generateDepartmentId(city, departmentName);
// Check if the phone is already registered
const existingStore = await Deparments.findOne({ phone });
if (existingStore) {
return reply.status(400).send({ message: "Phone is already registered" });
}
// Generate desginationId
const departmentId = await generateDepartmentId(city, departmentName);
// Hash the password
const hashedPassword = await bcrypt.hash(password, 10);
// Check if the phone is already registered
const existingStore = await Deparments.findOne({ phone });
if (existingStore) {
return reply.status(400).send({ message: 'Phone is already registered' });
}
// 🟢 Handle reportingManager "Self"
let finalReportingManager = reportingManager;
let finalReportingManagerMobile = reportingManager_mobile_number;
let finalReportingManagerEmail = reportingManager_email;
// Hash the password
const hashedPassword = await bcrypt.hash(password, 10);
if (reportingManager?.toLowerCase() === "self") {
finalReportingManager = `${firstName || ""} ${lastName || ""} - (${phone}) - ${city}`;
finalReportingManagerMobile = phone;
finalReportingManagerEmail = email;
}
// Create a new designation
const department = new Deparments({
departmentId,
alternativeContactNumber,
officeName,
reportingManager_mobile_number,
reportingManager_email,
personal_city,
gender,
city,
firstName,
lastName,
email,
personalEmail,
reportingManager,
departmentName,
phone,
address1,
address2,
services: { password: { bcrypt: hashedPassword } },
state,
zone,
country,
pincode,
desginationName,
location,
picture,
dateOfJoin,
employeeType,
createdBy,
updatedBy,
});
// Create new department
const department = new Deparments({
departmentId,
alternativeContactNumber,
officeName,
reportingManager_mobile_number: finalReportingManagerMobile,
reportingManager_email: finalReportingManagerEmail,
personal_city,
gender,
city,
firstName,
lastName,
email,
personalEmail,
reportingManager: finalReportingManager,
departmentName,
phone,
address1,
address2,
services: { password: { bcrypt: hashedPassword } },
state,
zone,
country,
pincode,
desginationName,
location,
picture,
dateOfJoin,
employeeType,
createdBy,
updatedBy,
});
await department.save();
await department.save();
reply.send({ department, message: "Account Created Successfully" });
} catch (err) {
console.error("❌ Error in addDepartment:", err);
reply.status(500).send({ message: err.message });
}
};
reply.send({ department, message: 'Account Created Successfully' });
} catch (err) {
reply.status(500).send({ message: err.message });
}
};
exports.getDetails = async (request, reply) => {
try {

Loading…
Cancel
Save