ashok 1 month ago
commit acf361b1c7

@ -552,25 +552,36 @@ exports.addDepartment = async (request, reply) => {
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 // Hash the password
const hashedPassword = await bcrypt.hash(password, 10); const hashedPassword = await bcrypt.hash(password, 10);
// Create a new designation // 🟢 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({ const department = new Deparments({
departmentId, departmentId,
alternativeContactNumber, alternativeContactNumber,
officeName, officeName,
reportingManager_mobile_number, reportingManager_mobile_number: finalReportingManagerMobile,
reportingManager_email, reportingManager_email: finalReportingManagerEmail,
personal_city, personal_city,
gender, gender,
city, city,
@ -578,7 +589,7 @@ exports.addDepartment = async (request, reply) => {
lastName, lastName,
email, email,
personalEmail, personalEmail,
reportingManager, reportingManager: finalReportingManager,
departmentName, departmentName,
phone, phone,
address1, address1,
@ -599,12 +610,14 @@ exports.addDepartment = async (request, reply) => {
await department.save(); await department.save();
reply.send({ department, message: 'Account Created Successfully' }); reply.send({ department, message: "Account Created Successfully" });
} catch (err) { } catch (err) {
console.error("❌ Error in addDepartment:", err);
reply.status(500).send({ message: err.message }); reply.status(500).send({ message: err.message });
} }
}; };
exports.getDetails = async (request, reply) => { exports.getDetails = async (request, reply) => {
try { try {
const { id } = request.params; const { id } = request.params;

Loading…
Cancel
Save