installation login

master^2
Bhaskar 7 months ago
parent 40ac93a540
commit 421f16c812

@ -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();
},
});

@ -616,7 +616,7 @@ const {Storage} = require('@google-cloud/storage');
const { Supplier, profilePictureSupplier } = require("./models/supplier"); const { Supplier, profilePictureSupplier } = require("./models/supplier");
const multer = require('fastify-multer'); const multer = require('fastify-multer');
const { ProfilePictureInstall, Install } = require("./models/store.js"); 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(require('fastify-formbody'));
// fastify.register(multer.contentParser); // fastify.register(multer.contentParser);
// const multipart = require('fastify-multipart'); // const multipart = require('fastify-multipart');
@ -941,73 +941,86 @@ fastify.post("/api/insatllLogin", {
try { try {
const { phone, password } = req.body; const { phone, password } = req.body;
// Check if an install with the phone number exists // Check if user exists in the Department Schema
const install = await Install.findOne({ phone }); const user = await Deparments.findOne({ phone });
if (!install) { if (!user) {
return reply.status(401).send({ return reply.code(400).send({ message: "User not found" });
simplydata: {
error: true,
message: 'Invalid Phone or password'
}
});
} }
// Compare the password entered by the user with the hashed password stored in the database // Verify Password
const isPasswordValid = await bcrypt.compare(password, install.services.password.bcrypt); const isMatch = await bcrypt.compare(password, user.services.password.bcrypt);
if (!isPasswordValid) { if (!isMatch) {
return reply.status(401).send({ return reply.code(400).send({ message: "Invalid credentials" });
simplydata: {
error: true,
message: 'Invalid phone or password'
} }
// 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 // Fetch profile picture if available
const token = fastify.jwt.sign({ phone: install.phone }, 'your_jwt_secret', { expiresIn: '30d' }); const profilePicture = await ProfilePictureInstall.findOne({ customerId: installation._id });
// Fetch the profile picture if it exists // Generate JWT Token
const profilePicture = await ProfilePictureInstall.findOne({ customerId: install._id }); const token =fastify.jwt.sign({ userId: user._id, phone: user.phone }, "your_secret_key", {
expiresIn: "7d",
});
// Construct response payload
const responsePayload = { const responsePayload = {
simplydata: { simplydata: {
error: false, error: false,
apiversion: fastify.config.APIVERSION, apiversion: fastify.config.APIVERSION,
access_token: token, access_token: token,
email: install.emails, email: installation.emails || [],
installationId: install.installationId, installationId: installation.installationId,
phone: install.phone, phone: installation.phone,
address1: install.address1, address1: installation.address1,
address2: install.address2, address2: installation.address2,
phoneVerified: install.phoneVerified, phoneVerified: installation.phoneVerified,
oneTimePasswordSetFlag: install.oneTimePasswordSetFlag, oneTimePasswordSetFlag: installation.oneTimePasswordSetFlag,
type: install.profile.role, type: installation.profile?.role || "user", // Default to "user" if not available
fcmIds: install.fcmIds, fcmIds: installation.fcmId || null,
team: install.team, team: installation.team,
city: install.city, city: installation.city,
manager: install.manager, manager: installation.manager,
firstName: install.firstName, firstName: installation.firstName,
lastName: install.lastName, lastName: installation.lastName,
address: install.address, address: installation.address || "",
alternativeNumber: install.alternativeNumber, 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); return reply.send(responsePayload);
} catch (err) { } catch (error) {
reply.status(500).send({ console.error("Login Error:", error);
simplydata: { return reply.code(500).send({ message: "Internal server error" });
error: true,
message: err.message
}
});
} }
},}); },});

@ -54,6 +54,21 @@ const installationschema = new mongoose.Schema({
}, },
team : { type: String, default: null}, team : { type: String, default: null},
manager : { 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}, longitude: { type : Number,default: 0.0},
latitude: {type: Number,default: 0.0}, latitude: {type: Number,default: 0.0},

@ -0,0 +1,7 @@
const installationController = require("../controllers/installationController")
module.exports = function (fastify, opts, next) {
next();
}
Loading…
Cancel
Save