From 0709c0a56c524dda2e3c4dc3ef65bc55160572a5 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 28 Feb 2025 16:47:32 +0530 Subject: [PATCH] type added in login installation --- src/controllers/installationController.js | 6 +- src/index.js | 235 ++++++++++++---------- src/models/store.js | 5 +- src/routes/installationRoute.js | 4 + 4 files changed, 141 insertions(+), 109 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index fa8f8197..9a3c0e47 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -28,7 +28,7 @@ const generateTeamMemberId = async () => { exports.createTeamMember = async (request, reply) => { try { - const { installationId, name, phone, password } = request.body; + const { installationId, name, phone, password,email,alternativePhone ,status} = request.body; // Check if installation exists const installation = await Install.findOne({ installationId }); @@ -66,9 +66,11 @@ exports.createTeamMember = async (request, reply) => { teamMemberId, name, phone, + email, + alternativePhone, installationTeamMemId: installationId, password: hashedPassword, - status: "active", + status, }; // Add to team members array diff --git a/src/index.js b/src/index.js index e628e3b4..f90f4a78 100644 --- a/src/index.js +++ b/src/index.js @@ -925,43 +925,44 @@ fastify.post('/api/uploads-user/:customerId', async (request, reply) => { } }); -fastify.post("/api/insatllLogin", { +fastify.post("/api/installLogin", { schema: { description: "This is for Login Install", tags: ["Installation"], summary: "This is for Login Install", body: { type: "object", - required: ["phone", "password"], + required: ["type", "phone", "password"], properties: { - phone: { type: "string" }, - password: { type: "string" }, + type: { type: "string", description: "User role type (e.g., 'admin', 'manager')" }, + phone: { type: "string", description: "Registered phone number" }, + password: { type: "string", description: "Password for authentication" }, }, }, }, async handler(req, reply) { try { - const { phone, password } = req.body; - + const { type, phone, password } = req.body; + // Check if user exists in the Department Schema const user = await Deparments.findOne({ phone }); - + if (!user) { return reply.code(400).send({ message: "User not found" }); } - + // Verify Password const isMatch = await bcrypt.compare(password, user.services.password.bcrypt); - + if (!isMatch) { return reply.code(400).send({ message: "Invalid credentials" }); } - - // Check if department details already exist in installationschema + + // Check if department details already exist in installation schema let installation = await Install.findOne({ phone }); - + if (!installation) { - // Create a new entry in installationschema with departmentId as installationId + // Create a new entry in installation schema with departmentId as installationId installation = new Install({ phone: user.phone, installationId: user.departmentId, // Store departmentId in installationId @@ -979,20 +980,29 @@ fastify.post("/api/insatllLogin", { profile: { state: user.state, country: user.country, + role: type, // Store type in profile.role }, }); - + await installation.save(); } - + + // Ensure `type` is stored in `profile.role` + if (!installation.profile?.role) { + installation.profile.role = type; + await installation.save(); // Save the updated type + } + // Fetch profile picture if available const profilePicture = await ProfilePictureInstall.findOne({ customerId: installation._id }); - + // Generate JWT Token - const token =fastify.jwt.sign({ userId: user._id, phone: user.phone }, "your_secret_key", { - expiresIn: "7d", - }); - + const token = fastify.jwt.sign( + { userId: user._id, phone: user.phone, role: installation.profile?.role }, + "your_secret_key", + { expiresIn: "7d" } + ); + // Construct response payload const responsePayload = { simplydata: { @@ -1016,101 +1026,114 @@ fastify.post("/api/insatllLogin", { address: installation.address || "", alternativeNumber: installation.alternativeNumber || null, profilePicture: profilePicture ? profilePicture.pictureUrl : null, // Include profile picture URL if available - } + }, }; - + return reply.send(responsePayload); } catch (error) { console.error("Login Error:", error); return reply.code(500).send({ message: "Internal server error" }); } - },}); - - 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" }, - }, + }, +}); + + + 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" }, + }, + }, + }, + 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 + } + + // 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, + 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", }, - 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 () => { diff --git a/src/models/store.js b/src/models/store.js index ef0ee213..96dc9077 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -46,6 +46,7 @@ const installationschema = new mongoose.Schema({ reportingManager: { type: String, default: null }, departmentName: { type: String, default: null }, zone: { type: String, default: null }, + type: { type: String }, profile: { @@ -64,7 +65,9 @@ const installationschema = new mongoose.Schema({ installationTeamMemId: { type: String }, password: { type: String, default: null }, status: { type: String, default: "active" }, - + email: { type: String }, + alternativePhone: { type: String }, + } ], diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index d18cb3cb..bd402d5f 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -15,6 +15,10 @@ module.exports = function (fastify, opts, next) { name: { type: "string", description: "Full name of the team member" }, phone: { type: "string", description: "Phone number of the team member" }, password: { type: "string", description: "Password for the team member" }, + alternativePhone: { type: "string", }, + email: { type: "string", }, + status: { type: "string", }, + }, },