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) => { exports.createTeamMember = async (request, reply) => {
try { try {
const { installationId, name, phone, password } = request.body; const { installationId, name, phone, password,email,alternativePhone ,status} = request.body;
// Check if installation exists // Check if installation exists
const installation = await Install.findOne({ installationId }); const installation = await Install.findOne({ installationId });
@ -66,9 +66,11 @@ exports.createTeamMember = async (request, reply) => {
teamMemberId, teamMemberId,
name, name,
phone, phone,
email,
alternativePhone,
installationTeamMemId: installationId, installationTeamMemId: installationId,
password: hashedPassword, password: hashedPassword,
status: "active", status,
}; };
// Add to team members array // 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: { schema: {
description: "This is for Login Install", description: "This is for Login Install",
tags: ["Installation"], tags: ["Installation"],
summary: "This is for Login Install", summary: "This is for Login Install",
body: { body: {
type: "object", type: "object",
required: ["phone", "password"], required: ["type", "phone", "password"],
properties: { properties: {
phone: { type: "string" }, type: { type: "string", description: "User role type (e.g., 'admin', 'manager')" },
password: { type: "string" }, phone: { type: "string", description: "Registered phone number" },
password: { type: "string", description: "Password for authentication" },
}, },
}, },
}, },
async handler(req, reply) { async handler(req, reply) {
try { try {
const { phone, password } = req.body; const { type, phone, password } = req.body;
// Check if user exists in the Department Schema // Check if user exists in the Department Schema
const user = await Deparments.findOne({ phone }); const user = await Deparments.findOne({ phone });
@ -957,11 +958,11 @@ fastify.post("/api/insatllLogin", {
return reply.code(400).send({ message: "Invalid credentials" }); 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 }); let installation = await Install.findOne({ phone });
if (!installation) { 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({ installation = new Install({
phone: user.phone, phone: user.phone,
installationId: user.departmentId, // Store departmentId in installationId installationId: user.departmentId, // Store departmentId in installationId
@ -979,19 +980,28 @@ fastify.post("/api/insatllLogin", {
profile: { profile: {
state: user.state, state: user.state,
country: user.country, country: user.country,
role: type, // Store type in profile.role
}, },
}); });
await installation.save(); 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 // Fetch profile picture if available
const profilePicture = await ProfilePictureInstall.findOne({ customerId: installation._id }); const profilePicture = await ProfilePictureInstall.findOne({ customerId: installation._id });
// Generate JWT Token // Generate JWT Token
const token =fastify.jwt.sign({ userId: user._id, phone: user.phone }, "your_secret_key", { const token = fastify.jwt.sign(
expiresIn: "7d", { userId: user._id, phone: user.phone, role: installation.profile?.role },
}); "your_secret_key",
{ expiresIn: "7d" }
);
// Construct response payload // Construct response payload
const responsePayload = { const responsePayload = {
@ -1016,7 +1026,7 @@ fastify.post("/api/insatllLogin", {
address: installation.address || "", address: installation.address || "",
alternativeNumber: installation.alternativeNumber || null, alternativeNumber: installation.alternativeNumber || null,
profilePicture: profilePicture ? profilePicture.pictureUrl : null, // Include profile picture URL if available profilePicture: profilePicture ? profilePicture.pictureUrl : null, // Include profile picture URL if available
} },
}; };
return reply.send(responsePayload); return reply.send(responsePayload);
@ -1024,7 +1034,9 @@ fastify.post("/api/insatllLogin", {
console.error("Login Error:", error); console.error("Login Error:", error);
return reply.code(500).send({ message: "Internal server error" }); return reply.code(500).send({ message: "Internal server error" });
} }
},}); },
});
fastify.post("/api/teamMemberLogin", { fastify.post("/api/teamMemberLogin", {
schema: { schema: {
@ -1033,17 +1045,17 @@ fastify.post("/api/insatllLogin", {
summary: "Login as a Team Member", summary: "Login as a Team Member",
body: { body: {
type: "object", type: "object",
required: ["phone", "password"], required: ["type", "phone", "password"],
properties: { 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" }, phone: { type: "string", description: "Registered phone number of the team member" },
password: { type: "string", description: "Password for authentication" }, password: { type: "string", description: "Password for authentication" },
}, },
}, },
}, },
async handler(request, reply) { async handler(request, reply) {
try { try {
const { phone, password } = request.body; const { type, phone, password } = request.body;
// Find team member in any installation // Find team member in any installation
const installation = await Install.findOne({ "team_member.team_member.phone": phone }); 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 // Generate JWT token
const token = fastify.jwt.sign( const token = fastify.jwt.sign(
{ phone: teamMember.phone, role: "team_member", installationId: installation.installationId }, { phone: teamMember.phone, role: type, installationId: installation.installationId },
"JWT_SECRET", process.env.JWT_SECRET,
{ expiresIn: "1h" } { expiresIn: "1h" }
); );
@ -1097,6 +1115,10 @@ fastify.post("/api/insatllLogin", {
access_token: token, access_token: token,
phone: teamMember.phone, phone: teamMember.phone,
teamMemberId: teamMember.teamMemberId, 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! // Run the server!
const start = async () => { const start = async () => {

@ -46,6 +46,7 @@ const installationschema = new mongoose.Schema({
reportingManager: { type: String, default: null }, reportingManager: { type: String, default: null },
departmentName: { type: String, default: null }, departmentName: { type: String, default: null },
zone: { type: String, default: null }, zone: { type: String, default: null },
type: { type: String },
profile: { profile: {
@ -64,6 +65,8 @@ const installationschema = new mongoose.Schema({
installationTeamMemId: { type: String }, installationTeamMemId: { type: String },
password: { type: String, default: null }, password: { type: String, default: null },
status: { type: String, default: "active" }, 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" }, name: { type: "string", description: "Full name of the team member" },
phone: { type: "string", description: "Phone number of the team member" }, phone: { type: "string", description: "Phone number of the team member" },
password: { type: "string", description: "Password for the team member" }, password: { type: "string", description: "Password for the team member" },
alternativePhone: { type: "string", },
email: { type: "string", },
status: { type: "string", },
}, },
}, },

Loading…
Cancel
Save