team member login changes creation

master^2
Bhaskar 7 months ago
parent c150a8283d
commit c5dfa45709

@ -28,10 +28,10 @@ const generateTeamMemberId = async () => {
exports.createTeamMember = async (request, reply) => { exports.createTeamMember = async (request, reply) => {
try { try {
const { installationId, firstName, phone, password,email,alternativePhone ,status} = request.body; const { departmentId, firstName, phone, password,email,alternativePhone ,status} = request.body;
// Check if installation exists // Check if installation exists
const installation = await Install.findOne({ installationId }); const installation = await Deparments.findOne({ departmentId });
if (!installation) { if (!installation) {
return reply.status(404).send({ return reply.status(404).send({
@ -68,7 +68,7 @@ exports.createTeamMember = async (request, reply) => {
phone, phone,
email, email,
alternativePhone, alternativePhone,
installationTeamMemId: installationId, installationTeamMemId: departmentId,
password: hashedPassword, password: hashedPassword,
status, status,
}; };

@ -1040,12 +1040,12 @@ fastify.post("/api/teamMemberLogin", {
try { try {
const { type, phone, password } = request.body; const { type, phone, password } = request.body;
// Find the installation containing this team member // ✅ Step 1: Find the team member in `Deparments`
const installation = await Install.findOne({ const department = await Deparments.findOne({
"team_member.team_member.phone": phone "team_member.team_member.phone": phone
}); });
if (!installation) { if (!department) {
return reply.status(401).send({ return reply.status(401).send({
simplydata: { simplydata: {
error: true, error: true,
@ -1054,11 +1054,11 @@ fastify.post("/api/teamMemberLogin", {
}); });
} }
// Find the specific team member inside the array // ✅ Step 2: Find the specific team member
const teamMember = installation.team_member.team_member.find( const teamMember = department.team_member.team_member.find(
(member) => member.phone === phone (member) => member.phone === phone
); );
console.log(installation.team_member.team_member.firstName)
if (!teamMember) { if (!teamMember) {
return reply.status(401).send({ return reply.status(401).send({
simplydata: { simplydata: {
@ -1068,7 +1068,7 @@ console.log(installation.team_member.team_member.firstName)
}); });
} }
// Verify password // ✅ Step 3: Verify password
const isPasswordValid = await bcrypt.compare(password, teamMember.password); const isPasswordValid = await bcrypt.compare(password, teamMember.password);
if (!isPasswordValid) { if (!isPasswordValid) {
@ -1080,18 +1080,47 @@ console.log(installation.team_member.team_member.firstName)
}); });
} }
// Store the `type` in the database (if not already stored) console.log("Team Member First Name:", teamMember.firstName); // ✅ Debugging
if (!teamMember.type) {
teamMember.type = type; // ✅ Step 4: Check if this team member already exists in `Install`
await installation.save(); // Save the updated team member type let installation = await Install.findOne({
installationId: department.departmentId
});
if (!installation) {
return reply.status(404).send({
simplydata: {
error: true,
message: "Installation not found",
},
});
} }
// Extract installationId from the found installation document // Check if team member already exists in Install schema
const installationId = installation.installationId; const existingTeamMember = installation.team_member.team_member.find(
(member) => member.phone === phone
);
if (!existingTeamMember) {
// ✅ Step 5: Add team member details to `Install` schema
installation.team_member.team_member.push({
teamMemberId: teamMember.teamMemberId,
firstName: teamMember.firstName,
phone: teamMember.phone,
email: teamMember.email,
alternativePhone: teamMember.alternativePhone,
installationTeamMemId: installation.installationId,
password: teamMember.password, // Store hashed password
status: teamMember.status || "active",
type: type, // Store login type
});
await installation.save();
}
// Generate JWT token // ✅ Step 6: Generate JWT token
const token = fastify.jwt.sign( const token = fastify.jwt.sign(
{ phone: teamMember.phone, role: type, installationId }, { phone: teamMember.phone, role: type, installationId: installation.installationId },
"JWT_SECRET", "JWT_SECRET",
{ expiresIn: "1h" } { expiresIn: "1h" }
); );
@ -1102,13 +1131,13 @@ console.log(installation.team_member.team_member.firstName)
message: "Login successful", message: "Login successful",
access_token: token, access_token: token,
phone: teamMember.phone, phone: teamMember.phone,
firstName: teamMember.firstName || null, firstName: teamMember.firstName || null, // ✅ Now included
teamMemberId: teamMember.teamMemberId, teamMemberId: teamMember.teamMemberId,
alternativePhone: teamMember.alternativePhone || null, alternativePhone: teamMember.alternativePhone || null,
email: teamMember.email || null, email: teamMember.email || null,
status: teamMember.status || "active", status: teamMember.status || "active",
type: teamMember.type, // Returning the stored type type: teamMember.type,
installationId: installationId // Now included in response installationId: installation.installationId
}, },
}); });

@ -126,6 +126,22 @@ const citySchema = new mongoose.Schema(
picture:{type:String}, picture:{type:String},
dateOfJoin : { type: String }, dateOfJoin : { type: String },
services: { password: { bcrypt: String } }, services: { password: { bcrypt: String } },
team_member: {
team_member: [
{
teamMemberId: { type: String },
firstName: { type: String },
phone: { type: String },
installationTeamMemId: { type: String },
password: { type: String, default: null },
status: { type: String, default: "active" },
email: { type: String },
alternativePhone: { type: String },
}
],
},
createdAt: { createdAt: {
type: Date, type: Date,
default: function () { default: function () {

@ -9,9 +9,9 @@ module.exports = function (fastify, opts, next) {
summary: "Create Team Member", summary: "Create Team Member",
body: { body: {
type: "object", type: "object",
required: ["installationId", "firstName", "phone", "password"], required: ["departmentId", "firstName", "phone", "password"],
properties: { properties: {
installationId: { type: "string", description: "Installation ID to associate the team member with" }, departmentId: { type: "string", description: "Installation ID to associate the team member with" },
firstName: { type: "string", description: "Full name of the team member" }, firstName: { type: "string", description: "Full name of the team member" },
phone: { type: "string", description: "Phone number of the team member" }, phone: { type: "string", description: "Phone number of the team member" },
password: { type: "string", description: "Password for the team member" }, password: { type: "string", description: "Password for the team member" },

Loading…
Cancel
Save