type added in login installation

master^2
Bhaskar 7 months ago
parent 22c193dff4
commit 0709c0a56c

@ -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

@ -925,23 +925,24 @@ 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 });
@ -979,19 +980,28 @@ 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 = {
@ -1016,7 +1026,7 @@ 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);
@ -1024,7 +1034,9 @@ fastify.post("/api/insatllLogin", {
console.error("Login Error:", error);
return reply.code(500).send({ message: "Internal server error" });
}
},});
},
});
fastify.post("/api/teamMemberLogin", {
schema: {
@ -1033,17 +1045,17 @@ fastify.post("/api/insatllLogin", {
summary: "Login as a Team Member",
body: {
type: "object",
required: ["phone", "password"],
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 { phone, password } = request.body;
const { type, phone, password } = request.body;
// Find team member in any installation
const installation = await Install.findOne({ "team_member.team_member.phone": phone });
@ -1083,10 +1095,16 @@ fastify.post("/api/insatllLogin", {
});
}
// 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: "team_member", installationId: installation.installationId },
"JWT_SECRET",
{ phone: teamMember.phone, role: type, installationId: installation.installationId },
process.env.JWT_SECRET,
{ expiresIn: "1h" }
);
@ -1097,6 +1115,10 @@ fastify.post("/api/insatllLogin", {
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
},
});
@ -1109,9 +1131,10 @@ fastify.post("/api/insatllLogin", {
},
});
}
}
},
});
// Run the server!
const start = async () => {

@ -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,6 +65,8 @@ const installationschema = new mongoose.Schema({
installationTeamMemId: { type: String },
password: { type: String, default: null },
status: { type: String, default: "active" },
email: { type: String },
alternativePhone: { type: String },
}
],

@ -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", },
},
},

Loading…
Cancel
Save