From fce4c49224cda4f6ebc9d17c14c533237aaca172 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 13 Dec 2024 10:34:08 +0530 Subject: [PATCH] deparment Id and designation Id changes --- src/controllers/departmentController.js | 55 ++++++++++++------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 36ae99e3..d753fdf3 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -11,59 +11,55 @@ const fastify = require("fastify")({ const { Counter} = require('../models/User') const {Department, Desgination} = require('../models/Department') - -const generateDepartmentId = async () => { +const generateDepartmentId = async (prefix) => { const result = await Counter.findOneAndUpdate( { _id: 'department_id' }, { $inc: { seq: 1 } }, { upsert: true, new: true } ); - return result.seq; + return `AW${prefix}${result.seq}`; }; - const generateDesginationId = async () => { + const generateDesginationId = async (prefix) => { const result = await Counter.findOneAndUpdate( { _id: 'desgination_id' }, { $inc: { seq: 1 } }, { upsert: true, new: true } ); - return result.seq; + return `AW${prefix}${result.seq}`; }; + -exports.addDepartment = async (request, reply) => { + exports.addDepartment = async (request, reply) => { try { - const d_id = await generateDepartmentId(); - const departmentId = `AWDP${d_id}`; - const { - //phone, + departmentName, city, state, - // password, - country, + country, zone, address1, address2, pincode, - departmentName, createdBy, updatedBy, } = request.body; + // Generate departmentId based on departmentName + const prefix = departmentName.substring(0, 2).toUpperCase(); // Extract first two letters and convert to uppercase + const departmentId = await generateDepartmentId(prefix); + // Check for existing department const existingStore = await Department.findOne({ departmentId }); if (existingStore) { return reply.status(400).send({ message: 'Department is already registered' }); } - // const hashedPassword = await bcrypt.hash(password, 10); - + // Create new department const department = new Department({ - departmentId: departmentId, + departmentId, city, - // phone, address1, address2, - // services: { password: { bcrypt: hashedPassword } }, state, zone, country, @@ -75,12 +71,11 @@ exports.addDepartment = async (request, reply) => { await department.save(); - reply.send({department, message: 'Account Created Successfully' }); + reply.send({ department, message: 'Account Created Successfully' }); } catch (err) { reply.status(500).send({ message: err.message }); } }; - exports.getSinledepartmentData = async (req, reply) => { try { const { departmentId } = req.params; @@ -219,9 +214,6 @@ exports.getAllDepartmentsParticularFields = async (req, reply) => { exports.addDesgination = async (request, reply) => { try { - const d_id = await generateDepartmentId(); - const desginationId = `AWDES${d_id}`; - const { phone, city, @@ -232,7 +224,7 @@ exports.getAllDepartmentsParticularFields = async (req, reply) => { email, state, password, - country, + country, zone, address1, address2, @@ -242,16 +234,22 @@ exports.getAllDepartmentsParticularFields = async (req, reply) => { updatedBy, } = request.body; + // Generate desginationId based on desginationName + const prefix = departmentName.substring(0, 2).toUpperCase(); + const desginationId = await generateDesginationId(prefix); + // Check if the phone is already registered const existingStore = await Desgination.findOne({ phone }); if (existingStore) { return reply.status(400).send({ message: 'Phone is already registered' }); } + // Hash the password const hashedPassword = await bcrypt.hash(password, 10); - const department = new Desgination({ - desginationId: desginationId, + // Create a new designation + const desgination = new Desgination({ + desginationId, city, firstName, lastName, @@ -271,15 +269,14 @@ exports.getAllDepartmentsParticularFields = async (req, reply) => { updatedBy, }); - await department.save(); + await desgination.save(); - reply.send({department, message: 'Account Created Successfully' }); + reply.send({ desgination, message: 'Account Created Successfully' }); } catch (err) { reply.status(500).send({ message: err.message }); } }; - exports.getSinledesginationData = async (req, reply) => { try { const { desginationId } = req.params;