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