diff --git a/src/index.js b/src/index.js index e1ad2965..049fe255 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,7 @@ const createConnectionController = require("./controllers/createConnectionContro const storeController = require("./controllers/storeController.js") const boom = require("boom"); const bcrypt = require('bcrypt'); -const { ProfilePictureStore,generateinstallationId,Store, Survey, PlumbingWorkPictures, ElectrictyWorkPictures, MaterialRecievedPictures} = require("./models/store"); +const { ProfilePictureStore,generateinstallationId,Store, Survey, PlumbingWorkPictures, ElectrictyWorkPictures, MaterialRecievedPictures, Support} = require("./models/store"); const cors = require('fastify-cors'); @@ -1428,6 +1428,105 @@ fastify.post("/api/teamMemberLogin", { }, }); + fastify.post("/api/supportLogin", { + schema: { + description: "This is for Login Support", + tags: ["Support"], + summary: "This is for Login Support", + body: { + type: "object", + required: [ "phone", "password"], + properties: { + phone: { type: "string", description: "Registered phone number" }, + password: { type: "string", description: "Password for authentication" }, + }, + }, + }, + async handler(req, reply) { + try { + const { phone, password } = req.body; + + // Check if user exists in the Department Schema + const user = await Deparments.findOne({ phone }); + console.log("user", user) + + if (!user) { + return reply.code(400).send({ message: "User not found" }); + } + + // Verify Password + const isMatch = await bcrypt.compare(password, user.services.password.bcrypt); + + if (!isMatch) { + return reply.code(400).send({ message: "Invalid credentials" }); + } + + let survey = await Support.findOne({ phone }); + + if (!survey) { + survey = new Support({ + phone: user.phone, + supportId: user.departmentId, + 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, + //role: type, // Store type in profile.role + }, + }); + + await survey.save(); + } + + const token = fastify.jwt.sign( + { phone: user.phone }, + "Scret", + { expiresIn: "1h" } + ); + + return reply.send({ + simplydata: { + error: false, + message: "Login successful", + access_token: token, + phone: user.phone, + supportId: user.departmentId, + 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, + //role: type, // Store type in profile.role + }, + }, + }); + // return reply.send(survey); + } catch (error) { + console.error("Login Error:", error); + return reply.code(500).send({ message: "Internal server error" }); + } + }, + }); + fastify.post("/api/storelogin", { schema: { diff --git a/src/models/store.js b/src/models/store.js index 3c042c36..b1f0999a 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -206,6 +206,75 @@ const installationschema = new mongoose.Schema({ } }); + const supportschema = new mongoose.Schema({ + // name: { type: String }, + phone: { type: String, unique: true, trim: true }, + address: String, + supportId: { type: String }, + phoneVerified: { type: Boolean, default: false }, + phoneVerificationCode: { type: Number, default: 11111 }, + passwordResetCode: { type: Number}, + oneTimePasswordSetFlag: { type: Boolean, default: false }, + emails: [{ email: String, verified: { type: Boolean, default: false } }], + services: { password: { bcrypt: String } }, + alternativeNumber: { type: String, default: null }, + firstName: { type: String, default: null }, + lastName: { type: String, default: null }, + address1: { type: String, default: null }, + address2: { type: String, default: null }, + city: { type: String, default: null }, + designation: { type: String, default: null }, + reportingManager: { type: String, default: null }, + departmentName: { type: String, default: null }, + zone: { type: String, default: null }, + type: { type: String }, + + profile: { + + state: { type: String, default: null }, + country: { type: String, default: null }, + }, + team : { type: String, default: null}, + manager : { type: String, default: null}, + team_member: { + + team_member: [ + { + support_teamMemberId: { type: String }, + name: { type: String }, + phone: { type: String }, + installationTeamMemId: { type: String }, + password: { type: String, default: null }, + status: { type: String, default: "active" }, + email: { type: String }, + alternativePhone: { type: String }, + + } + ], + + + }, + + longitude: { type : Number,default: 0.0}, + latitude: {type: Number,default: 0.0}, + + fcmId: { type: String, default: null }, + createdAt: { + type: Date, + default: function () { + return Date.now(); + }, + }, + createdBy: ObjectId, + updatedAt: { + type: Date, + default: function () { + return Date.now(); + }, + }, + updatedBy: ObjectId, + + }); const storeSchema = new mongoose.Schema({ storename: { type: String }, @@ -785,6 +854,7 @@ const Iotprice = mongoose.model('Iotprice', iotpriceSchema); const SensorStock = mongoose.model("SensorStock", SensorStockSchema); const Install = mongoose.model("Install", installationschema); const Survey = mongoose.model("Survey", surveyschema); + const Support = mongoose.model("Support", supportschema); const HardwareCart = mongoose.model("HardwareCart", hardwareCartSchema); const ServiceCart = mongoose.model("ServiceCart", serviceCartSchema); @@ -792,4 +862,4 @@ const Iotprice = mongoose.model('Iotprice', iotpriceSchema); - module.exports = {MaterialRecievedPictures,PlumbingWorkPictures,ElectrictyWorkPictures,MasterSlaveData,SensorStock,Order,EstimationOrder,Iotprice,Sales, Install,Survey, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId, HardwareCart, ServiceCart}; + module.exports = {Support,MaterialRecievedPictures,PlumbingWorkPictures,ElectrictyWorkPictures,MasterSlaveData,SensorStock,Order,EstimationOrder,Iotprice,Sales, Install,Survey, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,ProfilePictureStore,WaterLeverSensor,MotorSwitchSensor,Insensors,generatequatationId, HardwareCart, ServiceCart};