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