|
|
|
const boom = require("boom");
|
|
|
|
const bcrypt = require('bcrypt');
|
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
const customJwtAuth = require("../customAuthJwt");
|
|
|
|
const { Deparments } = require("../models/Department");
|
|
|
|
const { Install } = require("../models/store");
|
|
|
|
const { Counter } = require("../models/User")
|
|
|
|
const fastify = require("fastify")({
|
|
|
|
logger: true,
|
|
|
|
//disableRequestLogging: true,
|
|
|
|
genReqId(req) {
|
|
|
|
// you get access to the req here if you need it - must be a synchronous function
|
|
|
|
return uuidv4();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const generateTeamMemberId = async () => {
|
|
|
|
var result = await Counter.findOneAndUpdate(
|
|
|
|
{ _id: 'teamMemberId_id' },
|
|
|
|
{ $inc: { seq: 1 } },
|
|
|
|
{ upsert: true, new: true }
|
|
|
|
);
|
|
|
|
|
|
|
|
return result.seq;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.createTeamMember = async (request, reply) => {
|
|
|
|
try {
|
|
|
|
const { installationId, name, phone, password } = request.body;
|
|
|
|
|
|
|
|
// Check if installation exists
|
|
|
|
const installation = await Install.findOne({ installationId });
|
|
|
|
|
|
|
|
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,
|
|
|
|
name,
|
|
|
|
phone,
|
|
|
|
installationTeamMemId: installationId,
|
|
|
|
password: hashedPassword,
|
|
|
|
status: "active",
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|