|
|
@ -29,75 +29,77 @@ const generateTeamMemberId = async () => {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.createTeamMember = async (request, reply) => {
|
|
|
|
exports.createTeamMember = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { departmentId, firstName, phone, password,email,alternativePhone ,status} = request.body;
|
|
|
|
const { adminId } = req.params;
|
|
|
|
|
|
|
|
const { departmentId, departmentName, firstName, phone, password, email, alternativePhone, status } = req.body;
|
|
|
|
// Check if installation exists
|
|
|
|
|
|
|
|
const installation = await Deparments.findOne({ departmentId });
|
|
|
|
if (!adminId) {
|
|
|
|
|
|
|
|
return reply.status(400).send({ simplydata: { error: true, message: "adminId is required in path params" } });
|
|
|
|
if (!installation) {
|
|
|
|
|
|
|
|
return reply.status(404).send({
|
|
|
|
|
|
|
|
simplydata: {
|
|
|
|
|
|
|
|
error: true,
|
|
|
|
|
|
|
|
message: "Installation not found",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check if phone number already exists in the team
|
|
|
|
|
|
|
|
const existingMember = installation.team_member.team_member.find(
|
|
|
|
|
|
|
|
(member) => member.phone === phone
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (existingMember) {
|
|
|
|
|
|
|
|
return reply.status(400).send({
|
|
|
|
|
|
|
|
simplydata: {
|
|
|
|
|
|
|
|
error: true,
|
|
|
|
|
|
|
|
message: "Phone number already exists in the team",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Hash password
|
|
|
|
|
|
|
|
const hashedPassword = await bcrypt.hash(password, 10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const c_id = await generateTeamMemberId();
|
|
|
|
|
|
|
|
const teamMemberId = `AWTM${c_id}`;
|
|
|
|
|
|
|
|
// Create new team member
|
|
|
|
|
|
|
|
const newTeamMember = {
|
|
|
|
|
|
|
|
teamMemberId,
|
|
|
|
|
|
|
|
firstName,
|
|
|
|
|
|
|
|
phone,
|
|
|
|
|
|
|
|
email,
|
|
|
|
|
|
|
|
alternativePhone,
|
|
|
|
|
|
|
|
installationTeamMemId: departmentId,
|
|
|
|
|
|
|
|
password: hashedPassword,
|
|
|
|
|
|
|
|
status,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add to team members array
|
|
|
|
|
|
|
|
installation.team_member.team_member.push(newTeamMember);
|
|
|
|
|
|
|
|
await installation.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
|
|
|
simplydata: {
|
|
|
|
|
|
|
|
error: false,
|
|
|
|
|
|
|
|
message: "Team member created successfully",
|
|
|
|
|
|
|
|
teamMemberId: newTeamMember.teamMemberId,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error("Error creating team member:", err);
|
|
|
|
|
|
|
|
reply.status(500).send({
|
|
|
|
|
|
|
|
simplydata: {
|
|
|
|
|
|
|
|
error: true,
|
|
|
|
|
|
|
|
message: "Internal server error",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Check if admin exists
|
|
|
|
|
|
|
|
const admin = await Admin.findOne({ adminId });
|
|
|
|
|
|
|
|
if (!admin) {
|
|
|
|
|
|
|
|
return reply.status(404).send({ simplydata: { error: true, message: "Admin not found" } });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check if department exists
|
|
|
|
|
|
|
|
const department = await Deparments.findOne({ departmentId });
|
|
|
|
|
|
|
|
if (!department) {
|
|
|
|
|
|
|
|
return reply.status(404).send({ simplydata: { error: true, message: "Department not found" } });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Update adminId in department (if needed)
|
|
|
|
|
|
|
|
department.adminId = adminId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check if phone already exists
|
|
|
|
|
|
|
|
const existingMember = department.team_member.team_member.find(member => member.phone === phone);
|
|
|
|
|
|
|
|
if (existingMember) {
|
|
|
|
|
|
|
|
return reply.status(400).send({ simplydata: { error: true, message: "Phone number already exists in the team" } });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Generate new teamMemberId
|
|
|
|
|
|
|
|
const c_id = await generateTeamMemberId();
|
|
|
|
|
|
|
|
const teamMemberId = `AWTM${c_id}`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Hash password
|
|
|
|
|
|
|
|
const hashedPassword = await bcrypt.hash(password, 10);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create new member
|
|
|
|
|
|
|
|
const newTeamMember = {
|
|
|
|
|
|
|
|
teamMemberId,
|
|
|
|
|
|
|
|
firstName,
|
|
|
|
|
|
|
|
phone,
|
|
|
|
|
|
|
|
email,
|
|
|
|
|
|
|
|
alternativePhone,
|
|
|
|
|
|
|
|
installationTeamMemId: departmentId,
|
|
|
|
|
|
|
|
departmentId,
|
|
|
|
|
|
|
|
departmentName,
|
|
|
|
|
|
|
|
password: hashedPassword,
|
|
|
|
|
|
|
|
status: status || "active",
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add to team_member array
|
|
|
|
|
|
|
|
department.team_member.team_member.push(newTeamMember);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Save the department with updated adminId and new member
|
|
|
|
|
|
|
|
await department.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
|
|
|
simplydata: {
|
|
|
|
|
|
|
|
error: false,
|
|
|
|
|
|
|
|
message: "Team member created successfully",
|
|
|
|
|
|
|
|
teamMemberId: newTeamMember.teamMemberId,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error("Error creating team member:", err);
|
|
|
|
|
|
|
|
reply.status(500).send({ simplydata: { error: true, message: "Internal server error" } });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getTeamMembers = async (request, reply) => {
|
|
|
|
exports.getTeamMembers = async (request, reply) => {
|
|
|
@ -138,6 +140,50 @@ exports.createTeamMember = async (request, reply) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getAllDepartments = async (request, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
const { departmentName } = request.params;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!departmentName) {
|
|
|
|
|
|
|
|
return reply.status(400).send({
|
|
|
|
|
|
|
|
simplydata: {
|
|
|
|
|
|
|
|
error: true,
|
|
|
|
|
|
|
|
message: "departmentName is required in path params",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Find all departments matching departmentName
|
|
|
|
|
|
|
|
const departments = await Deparments.find({ departmentName }).lean();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!departments.length) {
|
|
|
|
|
|
|
|
return reply.status(404).send({
|
|
|
|
|
|
|
|
simplydata: {
|
|
|
|
|
|
|
|
error: true,
|
|
|
|
|
|
|
|
message: "No departments found with the given departmentName",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
|
|
|
simplydata: {
|
|
|
|
|
|
|
|
error: false,
|
|
|
|
|
|
|
|
message: "Departments retrieved successfully",
|
|
|
|
|
|
|
|
data: departments,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
console.error("Error fetching departments:", err);
|
|
|
|
|
|
|
|
return reply.status(500).send({
|
|
|
|
|
|
|
|
simplydata: {
|
|
|
|
|
|
|
|
error: true,
|
|
|
|
|
|
|
|
message: "Internal server error",
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// exports.assignTeamMemberToQuotation = async (request, reply) => {
|
|
|
|
// exports.assignTeamMemberToQuotation = async (request, reply) => {
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
// const { installationId } = request.params; // Get installationId from URL params
|
|
|
|
// const { installationId } = request.params; // Get installationId from URL params
|
|
|
@ -5806,6 +5852,7 @@ exports.getIotDataByCustomerAndHardwareId = async (req, reply) => {
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
const cron = require("node-cron");
|
|
|
|
const cron = require("node-cron");
|
|
|
|
|
|
|
|
const Admin = require("../models/admin");
|
|
|
|
|
|
|
|
|
|
|
|
// const raiseATicketLikeLogic = async (customerId, connected_to) => {
|
|
|
|
// const raiseATicketLikeLogic = async (customerId, connected_to) => {
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|