From 421f16c812580de6ce10e7b78385ac36496ea119 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 27 Feb 2025 17:34:35 +0530 Subject: [PATCH] installation login --- src/controllers/installationController.js | 17 ++++ src/index.js | 113 ++++++++++++---------- src/models/store.js | 17 +++- src/routes/installationRoute.js | 7 ++ 4 files changed, 103 insertions(+), 51 deletions(-) create mode 100644 src/controllers/installationController.js create mode 100644 src/routes/installationRoute.js diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js new file mode 100644 index 00000000..0053695f --- /dev/null +++ b/src/controllers/installationController.js @@ -0,0 +1,17 @@ +const boom = require("boom"); +const bcrypt = require('bcrypt'); +const jwt = require('jsonwebtoken'); +const customJwtAuth = require("../customAuthJwt"); +const { Deparments } = require("../models/Department"); +const { Install } = require("../models/store"); +const fastify = require("fastify")({ + logger: true, + //disableRequestLogging: true, + genReqId(req) { + // you get access to the req here if you need it - must be a synchronous function + return uuidv4(); + }, +}); + + + diff --git a/src/index.js b/src/index.js index 3da28c46..a299150a 100644 --- a/src/index.js +++ b/src/index.js @@ -616,7 +616,7 @@ const {Storage} = require('@google-cloud/storage'); const { Supplier, profilePictureSupplier } = require("./models/supplier"); const multer = require('fastify-multer'); const { ProfilePictureInstall, Install } = require("./models/store.js"); -const { TeamMemberProfilePicture, CompanyProfilePicture } = require("./models/Department.js"); +const { TeamMemberProfilePicture, CompanyProfilePicture, Deparments } = require("./models/Department.js"); fastify.register(require('fastify-formbody')); // fastify.register(multer.contentParser); // const multipart = require('fastify-multipart'); @@ -941,73 +941,86 @@ fastify.post("/api/insatllLogin", { try { const { phone, password } = req.body; - // Check if an install with the phone number exists - const install = await Install.findOne({ phone }); + // Check if user exists in the Department Schema + const user = await Deparments.findOne({ phone }); - if (!install) { - return reply.status(401).send({ - simplydata: { - error: true, - message: 'Invalid Phone or password' - } - }); + if (!user) { + return reply.code(400).send({ message: "User not found" }); } - // Compare the password entered by the user with the hashed password stored in the database - const isPasswordValid = await bcrypt.compare(password, install.services.password.bcrypt); + // Verify Password + const isMatch = await bcrypt.compare(password, user.services.password.bcrypt); - if (!isPasswordValid) { - return reply.status(401).send({ - simplydata: { - error: true, - message: 'Invalid phone or password' - } + if (!isMatch) { + return reply.code(400).send({ message: "Invalid credentials" }); + } + + // Check if department details already exist in installationschema + let installation = await Install.findOne({ phone }); + + if (!installation) { + // Create a new entry in installationschema with departmentId as installationId + installation = new Install({ + phone: user.phone, + installationId: user.departmentId, // Store departmentId in installationId + firstName: user.firstName, + lastName: user.lastName, + email: user.email, + alternativeNumber: user.alternativeContactNumber, + departmentName: user.departmentName, + designation: user.desginationName, + reportingManager: user.reportingManager, + city: user.city, + zone: user.zone, + address1: user.address1, + address2: user.address2, + profile: { + state: user.state, + country: user.country, + }, }); + + await installation.save(); } - // Generate a JWT token for the authenticated install - const token = fastify.jwt.sign({ phone: install.phone }, 'your_jwt_secret', { expiresIn: '30d' }); + // Fetch profile picture if available + const profilePicture = await ProfilePictureInstall.findOne({ customerId: installation._id }); - // Fetch the profile picture if it exists - const profilePicture = await ProfilePictureInstall.findOne({ customerId: install._id }); + // Generate JWT Token + const token =fastify.jwt.sign({ userId: user._id, phone: user.phone }, "your_secret_key", { + expiresIn: "7d", + }); + // Construct response payload const responsePayload = { simplydata: { error: false, apiversion: fastify.config.APIVERSION, access_token: token, - email: install.emails, - installationId: install.installationId, - phone: install.phone, - address1: install.address1, - address2: install.address2, - phoneVerified: install.phoneVerified, - oneTimePasswordSetFlag: install.oneTimePasswordSetFlag, - type: install.profile.role, - fcmIds: install.fcmIds, - team: install.team, - city: install.city, - manager: install.manager, - firstName: install.firstName, - lastName: install.lastName, - address: install.address, - alternativeNumber: install.alternativeNumber, + email: installation.emails || [], + installationId: installation.installationId, + phone: installation.phone, + address1: installation.address1, + address2: installation.address2, + phoneVerified: installation.phoneVerified, + oneTimePasswordSetFlag: installation.oneTimePasswordSetFlag, + type: installation.profile?.role || "user", // Default to "user" if not available + fcmIds: installation.fcmId || null, + team: installation.team, + city: installation.city, + manager: installation.manager, + firstName: installation.firstName, + lastName: installation.lastName, + address: installation.address || "", + alternativeNumber: installation.alternativeNumber || null, + profilePicture: profilePicture ? profilePicture.pictureUrl : null, // Include profile picture URL if available } }; - if (profilePicture) { - responsePayload.simplydata.picture = profilePicture.picture; - } - - // Return the token and user details to the client return reply.send(responsePayload); - } catch (err) { - reply.status(500).send({ - simplydata: { - error: true, - message: err.message - } - }); + } catch (error) { + console.error("Login Error:", error); + return reply.code(500).send({ message: "Internal server error" }); } },}); diff --git a/src/models/store.js b/src/models/store.js index eff5e1a5..e75beb9a 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -54,7 +54,22 @@ const installationschema = new mongoose.Schema({ }, team : { type: String, default: null}, manager : { type: String, default: null}, - + team_member: { + + team_member: [ + { + name: { type: String }, + phone: { type: String }, + installationTeamMemId: { type: String }, + password: { type: String, default: null }, + status: { type: String, default: "active" }, + + } + ], + + + }, + longitude: { type : Number,default: 0.0}, latitude: {type: Number,default: 0.0}, diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js new file mode 100644 index 00000000..a501331d --- /dev/null +++ b/src/routes/installationRoute.js @@ -0,0 +1,7 @@ +const installationController = require("../controllers/installationController") + +module.exports = function (fastify, opts, next) { + + next(); + +} \ No newline at end of file