add team member and login apis

master^2
Bhaskar 7 months ago
parent dd70847b04
commit 22c193dff4

@ -4,6 +4,7 @@ const jwt = require('jsonwebtoken');
const customJwtAuth = require("../customAuthJwt"); const customJwtAuth = require("../customAuthJwt");
const { Deparments } = require("../models/Department"); const { Deparments } = require("../models/Department");
const { Install } = require("../models/store"); const { Install } = require("../models/store");
const { Counter } = require("../models/User")
const fastify = require("fastify")({ const fastify = require("fastify")({
logger: true, logger: true,
//disableRequestLogging: 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",
},
});
}
};

@ -602,6 +602,8 @@ fastify.register(require("./routes/friendRequestRoute"));
fastify.register(require("./routes/adminRoute")); fastify.register(require("./routes/adminRoute"));
fastify.register(require("./routes/storeRoute")); fastify.register(require("./routes/storeRoute"));
fastify.register(require("./routes/departmentRoute.js")); 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 // 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", { fastify.post("/api/insatllLogin", {
schema: { schema: {
description: "This is for Login Install", description: "This is for Login Install",
tags: ["Install"], tags: ["Installation"],
summary: "This is for Login Install", summary: "This is for Login Install",
body: { body: {
type: "object", 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! // Run the server!
const start = async () => { const start = async () => {

@ -58,6 +58,7 @@ const installationschema = new mongoose.Schema({
team_member: [ team_member: [
{ {
teamMemberId: { type: String },
name: { type: String }, name: { type: String },
phone: { type: String }, phone: { type: String },
installationTeamMemId: { type: String }, installationTeamMemId: { type: String },

@ -2,6 +2,27 @@ const installationController = require("../controllers/installationController")
module.exports = function (fastify, opts, next) { 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(); next();
} }
Loading…
Cancel
Save