From 92a8960bf2b435903dd162b11cd2821831c85901 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 2 Sep 2025 12:35:04 +0530 Subject: [PATCH] changes --- src/controllers/departmentController.js | 179 +++++++++++++----------- 1 file changed, 96 insertions(+), 83 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index a0df820f..6a986275 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -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; - - // Generate desginationId - 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' }); - } - - // 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 }); + 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" }); } - }; + + // 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) => { try {