master^2
Bhaskar 7 months ago
parent a81685ad49
commit 742445005e

@ -1028,45 +1028,42 @@ fastify.post("/api/teamMemberLogin", {
summary: "Login as a Team Member", summary: "Login as a Team Member",
body: { body: {
type: "object", type: "object",
required: ["type", "phone", "password", "installationId"], required: ["type", "phone", "password"],
properties: { properties: {
type: { type: "string", description: "Role type of the user (e.g., 'team_member')" }, type: { type: "string", description: "Role type of the user (e.g., 'team_member')" },
phone: { type: "string", description: "Registered phone number of the team member" }, phone: { type: "string", description: "Registered phone number of the team member" },
password: { type: "string", description: "Password for authentication" }, password: { type: "string", description: "Password for authentication" },
installationId: { type: "string", description: "Installation ID for verification" },
}, },
}, },
}, },
async handler(request, reply) { async handler(request, reply) {
try { 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({ const installation = await Install.findOne({
"team_member.team_member": { "team_member.team_member.phone": phone
$elemMatch: { phone: phone, installationTeamMemId: installationId }
}
}); });
if (!installation) { if (!installation) {
return reply.status(401).send({ return reply.status(401).send({
simplydata: { simplydata: {
error: true, error: true,
message: "Invalid phone number or installation ID", message: "Invalid phone number",
}, },
}); });
} }
// Find the specific team member inside the array // Find the specific team member inside the array
const teamMember = installation.team_member.team_member.find( const teamMember = installation.team_member.team_member.find(
(member) => member.phone === phone && member.installationTeamMemId === installationId (member) => member.phone === phone
); );
if (!teamMember) { if (!teamMember) {
return reply.status(401).send({ return reply.status(401).send({
simplydata: { simplydata: {
error: true, 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 await installation.save(); // Save the updated team member type
} }
// Extract installationId from the found installation document
const installationId = installation.installationId;
// Generate JWT token // Generate JWT token
const token = fastify.jwt.sign( const token = fastify.jwt.sign(
{ phone: teamMember.phone, role: type, installationId: installation.installationId }, { phone: teamMember.phone, role: type, installationId },
process.env.JWT_SECRET, process.env.JWT_SECRET,
{ expiresIn: "1h" } { expiresIn: "1h" }
); );
@ -1108,6 +1108,7 @@ fastify.post("/api/teamMemberLogin", {
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, // Returning the stored type
installationId: installationId // Now included in response
}, },
}); });
@ -1124,6 +1125,7 @@ fastify.post("/api/teamMemberLogin", {
}); });
fastify.post("/api/surveyLogin", { fastify.post("/api/surveyLogin", {
schema: { schema: {
description: "This is for Login Survey", description: "This is for Login Survey",

Loading…
Cancel
Save