ashok 1 month ago
commit acf361b1c7

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

Loading…
Cancel
Save