ashok 9 months ago
commit b70626e860

@ -819,4 +819,115 @@ exports.updateTeamMember = async (req, reply) => {
} catch (err) { } catch (err) {
throw boom.boomify(err); throw boom.boomify(err);
} }
}; };
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' });
}
};

@ -41,6 +41,7 @@ const generateBookingId = async () => {
return result.seq; return result.seq;
}; };
const userSchema = new mongoose.Schema( const userSchema = new mongoose.Schema(
{ {
installationId:{type:String}, installationId:{type:String},
@ -57,6 +58,23 @@ const userSchema = new mongoose.Schema(
emails: [{ email: String, verified: { type: Boolean, default: false } }], emails: [{ email: String, verified: { type: Boolean, default: false } }],
services: { password: { bcrypt: String } }, services: { password: { bcrypt: String } },
survey_status:{ type:String,default: "pending" }, 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: { profile: {
role: [{ type: String, default: "user" }], role: [{ type: String, default: "user" }],
firstName: { type: String, default: null }, firstName: { type: String, default: null },

@ -791,5 +791,118 @@ 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(); next();
}; };

Loading…
Cancel
Save