From 09abf286c4d3e2608818e19d76e5de04a516746f Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 9 Jan 2025 14:32:00 +0530 Subject: [PATCH] add edit and delete satff --- src/controllers/userController.js | 111 ++++++++++++++++++++++++++++- src/models/User.js | 17 +++++ src/routes/usersRoute.js | 112 ++++++++++++++++++++++++++++++ 3 files changed, 239 insertions(+), 1 deletion(-) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index e11a36d7..1eaf00fe 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -819,4 +819,113 @@ exports.updateTeamMember = async (req, reply) => { } catch (err) { throw boom.boomify(err); } -}; \ No newline at end of file +}; + + + + +exports.createstaff = async (request, reply) => { + try { + const { customerId } = request.params; + const { staff } = request.body; + + if (!staff || !Array.isArray(staff)) { + return reply.status(400).send({ error: 'Invalid staff data provided' }); + } + + // Find user by customerId + const user = await User.findOne({ customerId }); + + if (!user) { + return reply.status(404).send({ error: 'Customer not found' }); + } + + // Validate each staff entry and append it to the user's staff array + const newStaff = staff.map((member) => ({ + name: member.name || null, + phone: member.phone || null, + password: member.password || null, + status: "active", // Default status + })); + + // Update the user document with the new staff members + user.staff.staff.push(...newStaff); + + // Save the updated user document + await user.save(); + + reply.send({ message: 'Staff members added successfully', staff: user.staff.staff }); + } catch (error) { + console.error('Error creating staff:', error); + reply.status(500).send({ error: 'An error occurred while adding staff' }); + } +}; + + +exports.editStaff = async (request, reply) => { + try { + const { customerId, phone } = request.params; + const { name, password } = request.body; + + const user = await User.findOne({ customerId, "staff.staff.phone": phone }); + if (!user) { + return reply.status(404).send({ error: 'Staff member not found' }); + } + + const staffMember = user.staff.staff.find(member => member.phone === phone); + staffMember.name = name || staffMember.name; + staffMember.password = password || staffMember.password; + + await user.save(); + reply.send({ message: 'Staff member updated successfully', staff: staffMember }); + } catch (error) { + console.error('Error updating staff member:', error); + reply.status(500).send({ error: 'An error occurred while updating staff' }); + } +}; + + +exports.deleteStaff = async (request, reply) => { + try { + const { customerId, phone } = request.params; + + const user = await User.findOne({ customerId }); + if (!user) { + return reply.status(404).send({ error: 'Customer not found' }); + } + + const initialLength = user.staff.staff.length; + user.staff.staff = user.staff.staff.filter(member => member.phone !== phone); + + if (initialLength === user.staff.staff.length) { + return reply.status(404).send({ error: 'Staff member not found' }); + } + + await user.save(); + reply.send({ message: 'Staff member deleted successfully' }); + } catch (error) { + console.error('Error deleting staff member:', error); + reply.status(500).send({ error: 'An error occurred while deleting staff' }); + } +}; + + +exports.blockStaff = async (request, reply) => { + try { + const { customerId, phone } = request.params; + + const user = await User.findOne({ customerId, "staff.staff.phone": phone }); + if (!user) { + return reply.status(404).send({ error: 'Staff member not found' }); + } + + const staffMember = user.staff.staff.find(member => member.phone === phone); + staffMember.status = 'blocked'; + + await user.save(); + reply.send({ message: 'Staff member blocked successfully', staff: staffMember }); + } catch (error) { + console.error('Error blocking staff member:', error); + reply.status(500).send({ error: 'An error occurred while blocking staff' }); + } +}; diff --git a/src/models/User.js b/src/models/User.js index f25aeb54..995182c3 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -57,6 +57,23 @@ const userSchema = new mongoose.Schema( emails: [{ email: String, verified: { type: Boolean, default: false } }], services: { password: { bcrypt: String } }, survey_status:{ type:String,default: "pending" }, + staff: { + + staff: [ + { + name: { type: String }, + phone: { type: String }, + + password: { type: String, default: null }, + + status: { type: String, default: "active" }, + + + } + ], + + + }, profile: { role: [{ type: String, default: "user" }], firstName: { type: String, default: null }, diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index c5d03084..05f2d24b 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -719,5 +719,117 @@ module.exports = function (fastify, opts, next) { }); + fastify.route({ + method: "POST", + url: "/api/createstaff/:customerId", + schema: { + tags: ["User"], + description: "This is for cretae New staff", + summary: "This is for cretae New staff", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + body: { + type: "object", + properties: { + + staff: { + type: "array", + maxItems: 2500, + items: { + type: "object", + properties: { + name: { type: "string", default: null }, + phone: { type: "string", default: null }, + + password:{ type: "string" ,default: null}, + + }, + }, + }, + + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + //preHandler: fastify.auth([fastify.authenticate]), + handler: userController.createstaff, +}); + + +fastify.route({ + method: "PUT", + url: "/api/editstaff/:customerId/:phone", + schema: { + tags: ["User"], + description: "Edit an existing staff member", + params: { + type: "object", + properties: { + customerId: { type: "string", description: "Customer ID" }, + phone: { type: "string", description: "Staff phone number" } + }, + required: ["customerId", "phone"] + }, + body: { + type: "object", + properties: { + name: { type: "string" }, + password: { type: "string" } + }, + required: ["name", "password"] + } + }, + handler: userController.editStaff, +}); + +fastify.route({ + method: "DELETE", + url: "/api/deletestaff/:customerId/:phone", + schema: { + tags: ["User"], + description: "Delete a staff member", + params: { + type: "object", + properties: { + customerId: { type: "string", description: "Customer ID" }, + phone: { type: "string", description: "Staff phone number" } + }, + required: ["customerId", "phone"] + } + }, + handler: userController.deleteStaff, +}); + +fastify.route({ + method: "PATCH", + url: "/api/blockstaff/:customerId/:phone", + schema: { + tags: ["User"], + description: "Block a staff member by phone", + params: { + type: "object", + properties: { + customerId: { type: "string", description: "Customer ID" }, + phone: { type: "string", description: "Staff phone number" } + }, + required: ["customerId", "phone"] + } + }, + handler: userController.blockStaff, +}); + + next(); };