diff --git a/src/index.js b/src/index.js index 5157cd71..a993faba 100644 --- a/src/index.js +++ b/src/index.js @@ -1021,102 +1021,108 @@ fastify.post("/api/installLogin", { }); - fastify.post("/api/teamMemberLogin", { - schema: { - description: "Login API for team members", - tags: ["Installation"], - summary: "Login as a Team Member", - body: { - type: "object", - required: ["type", "phone", "password"], - properties: { - type: { type: "string", description: "Role type of the user (e.g., 'team_member')" }, - phone: { type: "string", description: "Registered phone number of the team member" }, - password: { type: "string", description: "Password for authentication" }, - }, +fastify.post("/api/teamMemberLogin", { + schema: { + description: "Login API for team members", + tags: ["Installation"], + summary: "Login as a Team Member", + body: { + type: "object", + required: ["type", "phone", "password", "installationId"], + properties: { + type: { type: "string", description: "Role type of the user (e.g., 'team_member')" }, + phone: { type: "string", description: "Registered phone number of the team member" }, + password: { type: "string", description: "Password for authentication" }, + installationId: { type: "string", description: "Installation ID for verification" }, }, }, - async handler(request, reply) { - try { - const { type, phone, password } = request.body; - - // Find team member in any installation - const installation = await Install.findOne({ "team_member.team_member.phone": phone }); - - if (!installation) { - return reply.status(401).send({ - simplydata: { - error: true, - message: "Invalid phone number or password", - }, - }); - } - - // Find team member details - const teamMember = installation.team_member.team_member.find( - (member) => member.phone === phone - ); - - if (!teamMember) { - return reply.status(401).send({ - simplydata: { - error: true, - message: "Invalid phone number or password", - }, - }); - } - - // Verify password - const isPasswordValid = await bcrypt.compare(password, teamMember.password); - - if (!isPasswordValid) { - return reply.status(401).send({ - simplydata: { - error: true, - message: "Invalid phone number or password", - }, - }); - } - - // 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 + }, + async handler(request, reply) { + try { + const { type, phone, password, installationId } = request.body; + + // Find the installation where both phone and installationId match + const installation = await Install.findOne({ + "team_member.team_member": { + $elemMatch: { phone: phone, installationTeamMemId: installationId } } - - // Generate JWT token - const token = fastify.jwt.sign( - { phone: teamMember.phone, role: type, installationId: installation.installationId }, - process.env.JWT_SECRET, - { expiresIn: "1h" } - ); - - return reply.send({ + }); + + if (!installation) { + return reply.status(401).send({ simplydata: { - error: false, - message: "Login successful", - access_token: token, - phone: teamMember.phone, - firstName: teamMember.firstName, - teamMemberId: teamMember.teamMemberId, - alternativePhone: teamMember.alternativePhone || null, - email: teamMember.email || null, - status: teamMember.status || "active", - type: teamMember.type, // Returning the stored type + error: true, + message: "Invalid phone number or installation ID", }, }); - - } catch (err) { - console.error("Error logging in:", err); - reply.status(500).send({ + } + + // Find the specific team member inside the array + const teamMember = installation.team_member.team_member.find( + (member) => member.phone === phone && member.installationTeamMemId === installationId + ); + + if (!teamMember) { + return reply.status(401).send({ simplydata: { error: true, - message: "Internal server error", + message: "Invalid phone number or installation ID", }, }); } - }, - }); + + // Verify password + const isPasswordValid = await bcrypt.compare(password, teamMember.password); + + if (!isPasswordValid) { + return reply.status(401).send({ + simplydata: { + error: true, + message: "Invalid phone number or password", + }, + }); + } + + // 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 + } + + // Generate JWT token + const token = fastify.jwt.sign( + { phone: teamMember.phone, role: type, installationId: installation.installationId }, + process.env.JWT_SECRET, + { expiresIn: "1h" } + ); + + return reply.send({ + simplydata: { + error: false, + message: "Login successful", + access_token: token, + phone: teamMember.phone, + firstName: teamMember.firstName || null, + teamMemberId: teamMember.teamMemberId, + alternativePhone: teamMember.alternativePhone || null, + email: teamMember.email || null, + status: teamMember.status || "active", + type: teamMember.type, // Returning the stored type + }, + }); + + } catch (err) { + console.error("Error logging in:", err); + reply.status(500).send({ + simplydata: { + error: true, + message: "Internal server error", + }, + }); + } + }, +}); + fastify.post("/api/surveyLogin", { schema: {