diff --git a/src/controllers/departmentController.js b/src/controllers/departmentController.js index 08a9ef45..5e2fefc3 100644 --- a/src/controllers/departmentController.js +++ b/src/controllers/departmentController.js @@ -71,4 +71,111 @@ exports.addDepartment = async (request, reply) => { } catch (err) { reply.status(500).send({ message: err.message }); } + }; + + exports.getSinledepartmentData = async (req, reply) => { + try { + const { departmentId } = req.params; + + const department = await Department.findOne({ departmentId: departmentId }); + + if (!department) { + return reply.code(404).send({ + success: false, + message: 'Department not found.' + }); + } + + reply.code(200).send({ + success: true, + message: 'Department data retrieved successfully.', + data: department + }); + } catch (error) { + console.error('Error fetching department data:', error); + reply.code(500).send({ + success: false, + message: 'Failed to retrieve department data.', + error: error.message, + }); + } + }; + + exports.getalldepartmants = async (req, reply) => { + try { + await Department.find() + .exec() + .then((docs) => { + reply.send({ status_code: 200, data: docs, count: docs.length }); + }) + .catch((err) => { + console.log(err); + reply.send({ error: err }); + }); + } catch (err) { + throw boom.boomify(err); + } + }; + + exports.deletedepartmentInfo = async (req, reply) => { + try { + const departmentId = req.params.departmentId; + + const department = await Department.findOneAndDelete({ departmentId:departmentId }); + + reply.send({ status_code: 200, message: 'Delete Sucessfully', department}); + } catch (err) { + throw boom.boomify(err); + } + }; + + + exports.editdepartment = async (request, reply) => { + try { + const { departmentId } = request.params; + const { + + phone, + city, + state, + country, + zone, + address1, + address2, + pincode, + departmentName + + } = request.body; + + + const existing = await Department.findOne({ departmentId }); + if (!existing) { + return reply.status(404).send({ message: 'Department not found' }); + } + + const phoneExists = await Department.findOne({ phone, departmentId: { $ne: departmentId } }); + if (phoneExists) { + return reply.status(400).send({ message: 'Phone is already registered to another user' }); + } + + + existing.phone = phone || existing.phone; + existing.city = city || existing.city; + existing.state = state || existing.state; + existing.country = country || existing.country; + existing.zone = zone || existing.zone; + existing.departmentName = departmentName || existing.departmentName; + existing.pincode = pincode || existing.pincode; + + existing.address1 = address1 || existing.address1; + existing.address2 = address2 || existing.address2; + + + + await existing.save(); + + reply.send({ message: 'Department user updated successfully' }); + } catch (err) { + reply.status(500).send({ message: err.message }); + } }; \ No newline at end of file diff --git a/src/routes/departmentRoute.js b/src/routes/departmentRoute.js index af3f8953..397fefbe 100644 --- a/src/routes/departmentRoute.js +++ b/src/routes/departmentRoute.js @@ -36,10 +36,101 @@ module.exports = function (fastify, opts, next) { handler: departmentController.addDepartment, }); - + fastify.get("/api/getSingledepartmentData/:departmentId", { + schema: { + tags: ["Department"], + description: "This is for Get Single departmentId Data", + summary: "This is to Get Single departmentId Data", + params: { + type: "object", + properties: { + departmentId: { + type: "string", + description: "departmentId", + }, + }, + }, + + security: [ + { + basicAuth: [], + }, + ], + }, + handler: departmentController.getSinledepartmentData, + }); + fastify.get("/api/getalldepartments", { + schema: { + tags: ["Department"], + description: "This is for Get all Department Data", + summary: "This is for to Get all Department Data", + + security: [ + { + basicAuth: [], + }, + ], + }, + //preHandler: fastify.auth([fastify.authenticate]), + handler: departmentController.getalldepartmants, + }); + fastify.delete("/api/deletedepartment/:departmentId", { + schema: { + description: "Delete a Department by departmentId", + tags: ["Department"], + summary: "Delete a user by departmentId", + params: { + type: "object", + properties: { + departmentId: { type: "string" }, // Customer ID + }, + required: ["departmentId"], + }, + response: { + 200: { + type: "object", + properties: { + success: { type: "boolean" }, + message: { type: "string" }, + } + } + } + }, + handler: departmentController.deletedepartmentInfo, + }); + fastify.put('/api/editdepartment/:departmentId', { + schema: { + description: "Edit Department details by departmentId", + tags: ["Department"], + summary: "Edit Department details.", + params: { + type: "object", + properties: { + departmentId: { type: "string" }, + }, + required: ["departmentId"], + }, + body: { + type: "object", + properties: { + phone: { type: "string" }, + city: { type: "string" }, + state: { type: "string" }, + country: { type: "string" }, + address1: { type: "string" }, + address2: { type: "string" }, + zone: { type: "string" }, + pincode: { type: "string" }, + departmentName: { type: "string" }, + + }, + } + }, + handler: departmentController.editdepartment, + }); next(); }; \ No newline at end of file