diff --git a/src/index.js b/src/index.js index a993faba..f844a558 100644 --- a/src/index.js +++ b/src/index.js @@ -1028,45 +1028,42 @@ fastify.post("/api/teamMemberLogin", { summary: "Login as a Team Member", body: { type: "object", - required: ["type", "phone", "password", "installationId"], + 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" }, - installationId: { type: "string", description: "Installation ID for verification" }, }, }, }, async handler(request, reply) { try { - const { type, phone, password, installationId } = request.body; + const { type, phone, password } = request.body; - // Find the installation where both phone and installationId match + // Find the installation containing this team member const installation = await Install.findOne({ - "team_member.team_member": { - $elemMatch: { phone: phone, installationTeamMemId: installationId } - } + "team_member.team_member.phone": phone }); if (!installation) { return reply.status(401).send({ simplydata: { error: true, - message: "Invalid phone number or installation ID", + message: "Invalid phone number", }, }); } // Find the specific team member inside the array const teamMember = installation.team_member.team_member.find( - (member) => member.phone === phone && member.installationTeamMemId === installationId + (member) => member.phone === phone ); if (!teamMember) { return reply.status(401).send({ simplydata: { error: true, - message: "Invalid phone number or installation ID", + message: "Invalid phone number", }, }); } @@ -1089,9 +1086,12 @@ fastify.post("/api/teamMemberLogin", { await installation.save(); // Save the updated team member type } + // Extract installationId from the found installation document + const installationId = installation.installationId; + // Generate JWT token const token = fastify.jwt.sign( - { phone: teamMember.phone, role: type, installationId: installation.installationId }, + { phone: teamMember.phone, role: type, installationId }, process.env.JWT_SECRET, { expiresIn: "1h" } ); @@ -1108,6 +1108,7 @@ fastify.post("/api/teamMemberLogin", { email: teamMember.email || null, status: teamMember.status || "active", type: teamMember.type, // Returning the stored type + installationId: installationId // Now included in response }, }); @@ -1124,6 +1125,7 @@ fastify.post("/api/teamMemberLogin", { }); + fastify.post("/api/surveyLogin", { schema: { description: "This is for Login Survey",