From 22c193dff4f1c5028711d22bb1cf3f5304fd025c Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 28 Feb 2025 16:14:39 +0530 Subject: [PATCH] add team member and login apis --- src/controllers/installationController.js | 81 ++++++++++++++++++++ src/index.js | 90 ++++++++++++++++++++++- src/models/store.js | 1 + src/routes/installationRoute.js | 21 ++++++ 4 files changed, 192 insertions(+), 1 deletion(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 0053695f..fa8f8197 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -4,6 +4,7 @@ const jwt = require('jsonwebtoken'); const customJwtAuth = require("../customAuthJwt"); const { Deparments } = require("../models/Department"); const { Install } = require("../models/store"); +const { Counter } = require("../models/User") const fastify = require("fastify")({ logger: true, //disableRequestLogging: true, @@ -14,4 +15,84 @@ const fastify = require("fastify")({ }); +const generateTeamMemberId = async () => { + var result = await Counter.findOneAndUpdate( + { _id: 'teamMemberId_id' }, + { $inc: { seq: 1 } }, + { upsert: true, new: true } + ); + + return result.seq; + }; + + +exports.createTeamMember = async (request, reply) => { + try { + const { installationId, name, phone, password } = request.body; + + // Check if installation exists + const installation = await Install.findOne({ installationId }); + + if (!installation) { + return reply.status(404).send({ + simplydata: { + error: true, + message: "Installation not found", + }, + }); + } + + // Check if phone number already exists in the team + const existingMember = installation.team_member.team_member.find( + (member) => member.phone === phone + ); + + if (existingMember) { + return reply.status(400).send({ + simplydata: { + error: true, + message: "Phone number already exists in the team", + }, + }); + } + + // Hash password + const hashedPassword = await bcrypt.hash(password, 10); + + const c_id = await generateTeamMemberId(); + const teamMemberId = `AWTM${c_id}`; + // Create new team member + const newTeamMember = { + teamMemberId, + name, + phone, + installationTeamMemId: installationId, + password: hashedPassword, + status: "active", + }; + + // Add to team members array + installation.team_member.team_member.push(newTeamMember); + await installation.save(); + + return reply.send({ + simplydata: { + error: false, + message: "Team member created successfully", + teamMemberId: newTeamMember.teamMemberId, + }, + }); + + } catch (err) { + console.error("Error creating team member:", err); + reply.status(500).send({ + simplydata: { + error: true, + message: "Internal server error", + }, + }); + } + }; + + diff --git a/src/index.js b/src/index.js index a299150a..e628e3b4 100644 --- a/src/index.js +++ b/src/index.js @@ -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 () => { diff --git a/src/models/store.js b/src/models/store.js index e75beb9a..ef0ee213 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -58,6 +58,7 @@ const installationschema = new mongoose.Schema({ team_member: [ { + teamMemberId: { type: String }, name: { type: String }, phone: { type: String }, installationTeamMemId: { type: String }, diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index a501331d..d18cb3cb 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -2,6 +2,27 @@ const installationController = require("../controllers/installationController") module.exports = function (fastify, opts, next) { + fastify.post("/api/createTeamMember", { + schema: { + description: "Create a new team member under an installation", + tags: ["Installation"], + summary: "Create Team Member", + body: { + type: "object", + required: ["installationId", "name", "phone", "password"], + properties: { + installationId: { type: "string", description: "Installation ID to associate the team member with" }, + 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" }, + }, + }, + + }, + handler: installationController.createTeamMember, + }); + + next(); } \ No newline at end of file