team member login insatllationId field added

master^2
Bhaskar 7 months ago
parent 1a7d82a23f
commit a81685ad49

@ -1021,102 +1021,108 @@ fastify.post("/api/installLogin", {
}); });
fastify.post("/api/teamMemberLogin", { fastify.post("/api/teamMemberLogin", {
schema: { schema: {
description: "Login API for team members", description: "Login API for team members",
tags: ["Installation"], tags: ["Installation"],
summary: "Login as a Team Member", summary: "Login as a Team Member",
body: { body: {
type: "object", type: "object",
required: ["type", "phone", "password"], required: ["type", "phone", "password", "installationId"],
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) { },
try { async handler(request, reply) {
const { type, phone, password } = request.body; try {
const { type, phone, password, installationId } = request.body;
// Find team member in any installation
const installation = await Install.findOne({ "team_member.team_member.phone": phone }); // Find the installation where both phone and installationId match
const installation = await Install.findOne({
if (!installation) { "team_member.team_member": {
return reply.status(401).send({ $elemMatch: { phone: phone, installationTeamMemId: installationId }
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
} }
});
// Generate JWT token
const token = fastify.jwt.sign( if (!installation) {
{ phone: teamMember.phone, role: type, installationId: installation.installationId }, return reply.status(401).send({
process.env.JWT_SECRET,
{ expiresIn: "1h" }
);
return reply.send({
simplydata: { simplydata: {
error: false, error: true,
message: "Login successful", message: "Invalid phone number or installation ID",
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
}, },
}); });
}
} catch (err) {
console.error("Error logging in:", err); // Find the specific team member inside the array
reply.status(500).send({ const teamMember = installation.team_member.team_member.find(
(member) => member.phone === phone && member.installationTeamMemId === installationId
);
if (!teamMember) {
return reply.status(401).send({
simplydata: { simplydata: {
error: true, 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", { fastify.post("/api/surveyLogin", {
schema: { schema: {

Loading…
Cancel
Save