diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 3ca23dfe..612140d0 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -28,10 +28,10 @@ const generateTeamMemberId = async () => { exports.createTeamMember = async (request, reply) => { 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 - const installation = await Install.findOne({ installationId }); + const installation = await Deparments.findOne({ departmentId }); if (!installation) { return reply.status(404).send({ @@ -68,7 +68,7 @@ exports.createTeamMember = async (request, reply) => { phone, email, alternativePhone, - installationTeamMemId: installationId, + installationTeamMemId: departmentId, password: hashedPassword, status, }; diff --git a/src/index.js b/src/index.js index f7e577b5..98c770f7 100644 --- a/src/index.js +++ b/src/index.js @@ -1040,12 +1040,12 @@ fastify.post("/api/teamMemberLogin", { try { const { type, phone, password } = request.body; - // Find the installation containing this team member - const installation = await Install.findOne({ + // ✅ Step 1: Find the team member in `Deparments` + const department = await Deparments.findOne({ "team_member.team_member.phone": phone }); - if (!installation) { + if (!department) { return reply.status(401).send({ simplydata: { error: true, @@ -1054,11 +1054,11 @@ fastify.post("/api/teamMemberLogin", { }); } - // Find the specific team member inside the array - const teamMember = installation.team_member.team_member.find( + // ✅ Step 2: Find the specific team member + const teamMember = department.team_member.team_member.find( (member) => member.phone === phone ); -console.log(installation.team_member.team_member.firstName) + if (!teamMember) { return reply.status(401).send({ 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); if (!isPasswordValid) { @@ -1080,18 +1080,47 @@ console.log(installation.team_member.team_member.firstName) }); } - // Store the `type` in the database (if not already stored) - if (!teamMember.type) { - teamMember.type = type; - await installation.save(); // Save the updated team member type + console.log("Team Member First Name:", teamMember.firstName); // ✅ Debugging + + // ✅ Step 4: Check if this team member already exists in `Install` + 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 - const installationId = installation.installationId; + // Check if team member already exists in Install schema + 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( - { phone: teamMember.phone, role: type, installationId }, + { phone: teamMember.phone, role: type, installationId: installation.installationId }, "JWT_SECRET", { expiresIn: "1h" } ); @@ -1102,13 +1131,13 @@ console.log(installation.team_member.team_member.firstName) message: "Login successful", access_token: token, phone: teamMember.phone, - firstName: teamMember.firstName || null, + firstName: teamMember.firstName || null, // ✅ Now included teamMemberId: teamMember.teamMemberId, alternativePhone: teamMember.alternativePhone || null, email: teamMember.email || null, status: teamMember.status || "active", - type: teamMember.type, // Returning the stored type - installationId: installationId // Now included in response + type: teamMember.type, + installationId: installation.installationId }, }); diff --git a/src/models/Department.js b/src/models/Department.js index e4c8451f..ce78d5f9 100644 --- a/src/models/Department.js +++ b/src/models/Department.js @@ -126,6 +126,22 @@ const citySchema = new mongoose.Schema( picture:{type:String}, dateOfJoin : { type: 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: { type: Date, default: function () { diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 16b7d1a0..59a8f639 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -9,9 +9,9 @@ module.exports = function (fastify, opts, next) { summary: "Create Team Member", body: { type: "object", - required: ["installationId", "firstName", "phone", "password"], + required: ["departmentId", "firstName", "phone", "password"], 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" }, phone: { type: "string", description: "Phone number of the team member" }, password: { type: "string", description: "Password for the team member" },