diff --git a/src/index.js b/src/index.js index 65125233..d261cf39 100644 --- a/src/index.js +++ b/src/index.js @@ -411,105 +411,105 @@ console.log(user) }); -fastify.post("/api/storelogin", { - schema: { - description: "This is for Store Login", - tags: ["Store-Data"], - summary: "This is for Store Login", - body: { - type: "object", - required: ["phone", "password"], - properties: { - phone: { type: "string" }, - password: { type: "string" }, - }, - }, - }, - async handler(request, reply) { - try { - let store = await Store.findOne({ phone: request.body.phone }); - if (!store) { - return reply.code(400).send({ - simplydata: { - error: true, - code: 400, - message: "Invalid Phone or Password", - }, - }); - } +// fastify.post("/api/storelogin", { +// schema: { +// description: "This is for Store Login", +// tags: ["Store-Data"], +// summary: "This is for Store Login", +// body: { +// type: "object", +// required: ["phone", "password"], +// properties: { +// phone: { type: "string" }, +// password: { type: "string" }, +// }, +// }, +// }, +// async handler(request, reply) { +// try { +// let store = await Store.findOne({ phone: request.body.phone }); +// if (!store) { +// return reply.code(400).send({ +// simplydata: { +// error: true, +// code: 400, +// message: "Invalid Phone or Password", +// }, +// }); +// } - const isMatch = await bcrypt.compare(request.body.password, store.services.password.bcrypt); - if (!isMatch) { - return reply.code(400).send({ - simplydata: { - error: true, - code: 400, - message: "Invalid Phone or Password", - }, - }); - } +// const isMatch = await bcrypt.compare(request.body.password, store.services.password.bcrypt); +// if (!isMatch) { +// return reply.code(400).send({ +// simplydata: { +// error: true, +// code: 400, +// message: "Invalid Phone or Password", +// }, +// }); +// } - const token = fastify.jwt.sign( - { - storename: store.storename, - storeId: store._id, - roles: store.profile.role, - }, - { expiresIn: "30d" } - ); +// const token = fastify.jwt.sign( +// { +// storename: store.storename, +// storeId: store._id, +// roles: store.profile.role, +// }, +// { expiresIn: "30d" } +// ); - var profilePicture = await ProfilePictureStore.findOne({ storeId: store.storeId }); +// var profilePicture = await ProfilePictureStore.findOne({ storeId: store.storeId }); - if (!profilePicture) { - reply.send({ - simplydata: { - error: false, - apiversion: fastify.config.APIVERSION, - access_token: token, - email: store.emails, - phone: store.phone, - storeId: store.storeId, - storename: store.storename, - office_address: store.profile.office_address, - phoneVerified: store.phoneVerified, - oneTimePasswordSetFlag: store.oneTimePasswordSetFlag, - latitude: store.latitude, - longitude: store.longitude, - description: store.description, - type: store.profile.role, - typeasobj: JSON.stringify(Object.assign({}, store.profile.role)), - }, - }); - } else { - reply.send({ - simplydata: { - error: false, - apiversion: fastify.config.APIVERSION, - access_token: token, - picture: profilePicture.picture, - email: store.emails, - phone: store.phone, - storeId: store.storeId, - storename: store.storename, - office_address: store.profile.office_address, - phoneVerified: store.phoneVerified, - oneTimePasswordSetFlag: store.oneTimePasswordSetFlag, - latitude: store.latitude, - longitude: store.longitude, - description: store.description, - type: store.profile.role, - typeasobj: JSON.stringify(Object.assign({}, store.profile.role)), - }, - }); - } - } catch (err) { - throw boom.boomify(err); - } - }, -}); +// if (!profilePicture) { +// reply.send({ +// simplydata: { +// error: false, +// apiversion: fastify.config.APIVERSION, +// access_token: token, +// email: store.emails, +// phone: store.phone, +// storeId: store.storeId, +// storename: store.storename, +// office_address: store.profile.office_address, +// phoneVerified: store.phoneVerified, +// oneTimePasswordSetFlag: store.oneTimePasswordSetFlag, +// latitude: store.latitude, +// longitude: store.longitude, +// description: store.description, +// type: store.profile.role, +// typeasobj: JSON.stringify(Object.assign({}, store.profile.role)), +// }, +// }); +// } else { +// reply.send({ +// simplydata: { +// error: false, +// apiversion: fastify.config.APIVERSION, +// access_token: token, +// picture: profilePicture.picture, +// email: store.emails, +// phone: store.phone, +// storeId: store.storeId, +// storename: store.storename, +// office_address: store.profile.office_address, +// phoneVerified: store.phoneVerified, +// oneTimePasswordSetFlag: store.oneTimePasswordSetFlag, +// latitude: store.latitude, +// longitude: store.longitude, +// description: store.description, +// type: store.profile.role, +// typeasobj: JSON.stringify(Object.assign({}, store.profile.role)), +// }, +// }); +// } +// } catch (err) { +// throw boom.boomify(err); +// } +// }, +// }); fastify.get("/api/reset_token/:customerId", { @@ -1216,6 +1216,106 @@ fastify.post("/api/installLogin", { } }, }); + + + fastify.post("/api/storelogin", { + schema: { + description: "This is for Store Login", + tags: ["Store-Data"], + summary: "This is for Store Login", + 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 store = await Store.findOne({ phone }); + + if (!store) { + store = new Survey({ + phone: user.phone, + storeId: 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 store.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, + storeId: 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" }); + } + }, + });