|
|
|
@ -602,6 +602,8 @@ fastify.register(require("./routes/friendRequestRoute"));
|
|
|
|
|
fastify.register(require("./routes/adminRoute"));
|
|
|
|
|
fastify.register(require("./routes/storeRoute"));
|
|
|
|
|
fastify.register(require("./routes/departmentRoute.js"));
|
|
|
|
|
fastify.register(require("./routes/installationRoute.js"));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Testing route allows for retrieving a user by phone so one can see what is the phone verification code sent for a given user's phone
|
|
|
|
@ -926,7 +928,7 @@ fastify.post('/api/uploads-user/:customerId', async (request, reply) => {
|
|
|
|
|
fastify.post("/api/insatllLogin", {
|
|
|
|
|
schema: {
|
|
|
|
|
description: "This is for Login Install",
|
|
|
|
|
tags: ["Install"],
|
|
|
|
|
tags: ["Installation"],
|
|
|
|
|
summary: "This is for Login Install",
|
|
|
|
|
body: {
|
|
|
|
|
type: "object",
|
|
|
|
@ -1024,6 +1026,92 @@ fastify.post("/api/insatllLogin", {
|
|
|
|
|
}
|
|
|
|
|
},});
|
|
|
|
|
|
|
|
|
|
fastify.post("/api/teamMemberLogin", {
|
|
|
|
|
schema: {
|
|
|
|
|
description: "Login API for team members",
|
|
|
|
|
tags: ["Installation"],
|
|
|
|
|
summary: "Login as a Team Member",
|
|
|
|
|
body: {
|
|
|
|
|
type: "object",
|
|
|
|
|
required: ["phone", "password"],
|
|
|
|
|
properties: {
|
|
|
|
|
phone: { type: "string", description: "Registered phone number of the team member" },
|
|
|
|
|
password: { type: "string", description: "Password for authentication" },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
async handler(request, reply) {
|
|
|
|
|
try {
|
|
|
|
|
const { 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",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate JWT token
|
|
|
|
|
const token = fastify.jwt.sign(
|
|
|
|
|
{ phone: teamMember.phone, role: "team_member", installationId: installation.installationId },
|
|
|
|
|
"JWT_SECRET",
|
|
|
|
|
{ expiresIn: "1h" }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: false,
|
|
|
|
|
message: "Login successful",
|
|
|
|
|
access_token: token,
|
|
|
|
|
phone: teamMember.phone,
|
|
|
|
|
teamMemberId: teamMember.teamMemberId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error logging in:", err);
|
|
|
|
|
reply.status(500).send({
|
|
|
|
|
simplydata: {
|
|
|
|
|
error: true,
|
|
|
|
|
message: "Internal server error",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Run the server!
|
|
|
|
|
const start = async () => {
|
|
|
|
|
|
|
|
|
|