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