|
|
@ -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' });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|