login support chnages

master^2
Bhaskar 4 months ago
parent 02501891a1
commit b48edf73a1

@ -1428,6 +1428,8 @@ fastify.post("/api/teamMemberLogin", {
}, },
}); });
const moment = require("moment-timezone");
fastify.post("/api/supportLogin", { fastify.post("/api/supportLogin", {
schema: { schema: {
description: "This is for Login Support", description: "This is for Login Support",
@ -1437,29 +1439,30 @@ fastify.post("/api/teamMemberLogin", {
type: "object", type: "object",
required: ["phone", "password", "type"], required: ["phone", "password", "type"],
properties: { properties: {
phone: { type: "string", description: "Registered phone number" }, phone: { type: "string" },
password: { type: "string", description: "Password for authentication" }, password: { type: "string" },
type: { type: "string", description: "Role of the user (e.g., 'Support_Manager')" }, type: { type: "string" },
}, },
}, },
}, },
async handler(req, reply) { handler: async (req, reply) => {
try { try {
const { phone, password, type } = req.body; const { phone, password, type } = req.body;
const user = await Deparments.findOne({ phone }); const user = await Deparments.findOne({ phone });
if (!user) { if (!user) return reply.code(400).send({ message: "User not found" });
return reply.code(400).send({ message: "User not found" });
}
const isMatch = await bcrypt.compare(password, user.services.password.bcrypt); const isMatch = await bcrypt.compare(password, user.services.password.bcrypt);
if (!isMatch) { if (!isMatch) return reply.code(400).send({ message: "Invalid credentials" });
return reply.code(400).send({ message: "Invalid credentials" });
}
let survey = await Support.findOne({ phone }); // Format login times
if (!survey) { const now = moment().tz("Asia/Kolkata");
survey = new Support({ const dateOfLogin = now.format("DD-MM-YYYY");
const timeOfLogin = now.format("HH:mm:ss");
let support = await Support.findOne({ phone });
if (!support) {
support = new Support({
phone: user.phone, phone: user.phone,
supportId: user.departmentId, supportId: user.departmentId,
firstName: user.firstName, firstName: user.firstName,
@ -1477,15 +1480,23 @@ fastify.post("/api/teamMemberLogin", {
state: user.state, state: user.state,
country: user.country, country: user.country,
}, },
dateOfLogin,
timeOfLogin,
}); });
await survey.save(); await support.save();
} else {
// Update login time only if access token is being issued (new login)
await Support.updateOne(
{ phone },
{ $set: { dateOfLogin, timeOfLogin } }
);
} }
const token = fastify.jwt.sign( const token = fastify.jwt.sign(
{ {
userId: user._id, userId: user._id,
phone: user.phone, phone: user.phone,
role: type role: type,
}, },
"Scret", "Scret",
{ expiresIn: "1h" } { expiresIn: "1h" }
@ -1503,7 +1514,7 @@ fastify.post("/api/teamMemberLogin", {
address2: user.address2 || "", address2: user.address2 || "",
phoneVerified: false, phoneVerified: false,
oneTimePasswordSetFlag: false, oneTimePasswordSetFlag: false,
type: type, // <== returned from the request type: type,
fcmIds: null, fcmIds: null,
team: null, team: null,
city: user.city || "", city: user.city || "",
@ -1512,17 +1523,21 @@ fastify.post("/api/teamMemberLogin", {
lastName: user.lastName || "", lastName: user.lastName || "",
address: "", address: "",
alternativeNumber: user.alternativeContactNumber || "", alternativeNumber: user.alternativeContactNumber || "",
profilePicture: null profilePicture: null,
dateOfLogin,
timeOfLogin,
currentTime: now.format("HH:mm:ss")
} }
}); });
} catch (error) { } catch (error) {
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/supportTeamMemberLogin", { fastify.post("/api/supportTeamMemberLogin", {
schema: { schema: {
description: "Login Support TeamMember", description: "Login Support TeamMember",

Loading…
Cancel
Save