diff --git a/src/controllers/supplierOrderController.js b/src/controllers/supplierOrderController.js index d4730232..1c754643 100644 --- a/src/controllers/supplierOrderController.js +++ b/src/controllers/supplierOrderController.js @@ -63,6 +63,7 @@ exports.orderNow = async (req, reply) => { // console.log("This is the reply in the handler after the validations", reply); deliveryData = { supplierId: supplierId, + suppliername: req.body.suppliername, name: req.body.Name, phone: req.body.phone, alternativeContactNumber: req.body.alternativeContactNumber, @@ -71,6 +72,9 @@ exports.orderNow = async (req, reply) => { state:req.body.state, zip:req.body.zip, status:req.body.status, + longitude: req.body.longitude, + latitude:req.body.latitude, + }; var agent_mobile = req.body.phone @@ -88,6 +92,7 @@ exports.orderNow = async (req, reply) => { usertobeInserted = checkFormEncoding.agent; console.log("thsi true url string"); agent.supplierId = usertobeInserted.supplierId + agent.suppliername = usertobeInserted.suppliername agent.name = usertobeInserted.name; agent.phone = usertobeInserted.phone; agent.alternativeContactNumber = usertobeInserted.alternativeContactNumber; @@ -96,11 +101,14 @@ exports.orderNow = async (req, reply) => { agent.state = usertobeInserted.state agent.zip = usertobeInserted.zip agent.status = usertobeInserted.status - + agent.longitude = usertobeInserted.longitude + agent.latitude = usertobeInserted.latitude } } const insertedagent = await agent.save(); + + console.log("inster...", insertedagent) return insertedagent; diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 0d290510..3f9b3221 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1,5 +1,5 @@ //const Tank = require("../models/tanks"); -const { Tank, MotorData } = require('../models/tanks') +const { Tank, MotorData, IotData } = require('../models/tanks') const User = require("../models/User"); const boom = require("boom"); @@ -655,3 +655,40 @@ if(shape==="horizontalellipse"){ throw boom.boomify(err); } }; + +exports.IotDevice = async (req, reply) => { + try { + const { hardwareId, tankHeight, maxLevel, minLevel, mode } = req.body; + + // create a new tank document + const tank = new IotData({ hardwareId, tankHeight, maxLevel, minLevel, mode }); + + // save the document to MongoDB + await tank.save(); + + // send a success response + reply.code(200).send({ message: 'Data saved successfully' }); + } catch (err) { + // send an error response + reply.code(500).send({ error: err.message }); + } +} + + + + +exports.getIotD = async(req, reply) => { + try { + await IotData.find({hardwareId: req.query.hardwareId}) + .exec() + .then((docs) => { + reply.send({ status_code: 200, data: docs, count: docs.length }); + }) + .catch((err) => { + console.log(err); + reply.send({ error: err }); + }); + } catch (err) { + throw boom.boomify(err); + } +} diff --git a/src/controllers/userController.js b/src/controllers/userController.js index ee0bd3d9..f8e2ecc0 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -191,6 +191,8 @@ exports.addUser = async (req, reply) => { zip: req.body.zip, notes: req.body.notes, }, + longitude: req.body.longitude, + latitude:req.body.latitude, }; @@ -214,6 +216,8 @@ exports.addUser = async (req, reply) => { user.buildingName = usertobeInserted.buildingName; user.inchargeName = usertobeInserted.inchargeName; user.customerId = usertobeInserted.customer_id; + user.latitude = usertobeInserted.latitude; + user.longitude = usertobeInserted.longitude } @@ -245,13 +249,15 @@ exports.addUser = async (req, reply) => { customerId: insertedUser.customerId, inchargeName: insertedUser.inchargeName, buildingName: insertedUser.buildingName, - + emails: [ { email: insertedUser.emails[0].email, }, ], profile: insertedUser.profile, + longitude: insertedUser.longitude, + latitude:insertedUser.latitude, }, status_code: 200, }; diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index 7cae812b..e9f20238 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -238,6 +238,33 @@ exports.loginSupplier = async(request, reply) =>{ //Login Delivery Handler exports.loginDeliveryBoy = async(request, reply) =>{ + + phone = request.body.phone; + phoneVerificationCode = request.body.phoneVerificationCode; + + // check if user exists in the system. If user exists , display message that + // username is not available + console.log( + "this is the phone and verification code", + phone, + phoneVerificationCode + ); + deliveryBoyExists = await DeliveryBoy.findOne({ + phone: phone, + phoneVerified: false, + phoneVerificationCode: phoneVerificationCode, + }); + console.log(deliveryBoyExists); + if (deliveryBoyExists) { + // update the phoneVerified flag to true. + const filter = { + phone: phone, + phoneVerificationCode: phoneVerificationCode, + }; + const update = { phoneVerified: true }; + const doc = await DeliveryBoy.findOneAndUpdate(filter, update); + updatedDeliveryBoy = await DeliveryBoy.findOne({ phone: phone }); + } loginObject = await supplierController.loginDeliveryBoy(request); console.log("loginObject...",loginObject) if (loginObject.same) { @@ -308,7 +335,10 @@ exports.loginDeliveryBoy = async(request, reply) =>{ address: loginObject.delivery.address, phoneVerified: loginObject.delivery.phoneVerified, oneTimePasswordSetFlag: loginObject.delivery.oneTimePasswordSetFlag, - + supplierId: loginObject.delivery.supplierId, + suppliername: loginObject.delivery.suppliername, + longitude:loginObject.delivery. longitude, + latitude:loginObject.delivery.latitude, }, }); }if (profilePicture) { @@ -324,6 +354,10 @@ exports.loginDeliveryBoy = async(request, reply) =>{ address: loginObject.delivery.address, phoneVerified: loginObject.delivery.phoneVerified, oneTimePasswordSetFlag: loginObject.delivery.oneTimePasswordSetFlag, + supplierId: loginObject.delivery.supplierId, + suppliername: loginObject.delivery.suppliername, + longitude:loginObject.delivery. longitude, + latitude:loginObject.delivery.latitude, }, }); } @@ -664,7 +698,7 @@ exports.logoutsupplier = async (request, reply) => { const doc = await DeliveryBoy.findOneAndUpdate(filter, update); updatedDeliveryBoy = await DeliveryBoy.findOne({ phone: phone }); - if (updatedDeliveryBoy.phoneVerified) { + if (updatedDeliveryBoy.phoneVerified) { reply.send('{"armintatankdata":{"error":false,"verified": true}}'); } else { error = { @@ -677,16 +711,6 @@ exports.logoutsupplier = async (request, reply) => { req.body.regError = error; reply.send(error); } - } else { - error = { - armintatankdata: { - error: true, - code: 10005, - message: "10005 - Verification code entered cannot be validated.", - }, - }; - req.body.regError = error; - reply.send(error); } } catch (err) { throw boom.boomify(err); diff --git a/src/index.js b/src/index.js index 31dda7cb..2e328572 100644 --- a/src/index.js +++ b/src/index.js @@ -222,6 +222,8 @@ fastify.post("/api/login", { address2: loginObject.user.profile.address2, phoneVerified: loginObject.user.phoneVerified, oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag, + latitude: loginObject.user.latitude, + longitude: loginObject.user.longitude, type: loginObject.user.profile.role, typeasobj: stringToJsonObject, }, @@ -241,6 +243,8 @@ fastify.post("/api/login", { address2: loginObject.user.profile.address2, phoneVerified: loginObject.user.phoneVerified, oneTimePasswordSetFlag: loginObject.user.oneTimePasswordSetFlag, + latitude: loginObject.user.latitude, + longitude: loginObject.user.longitude, type: loginObject.user.profile.role, typeasobj: stringToJsonObject, }, diff --git a/src/models/User.js b/src/models/User.js index 9bc02448..8373d39a 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -91,6 +91,8 @@ const userSchema = new mongoose.Schema( coordinates: { type: [Number], default: [0, 0] }, }, + longitude: { type : Number}, + latitude: {type: Number}, isActive: Boolean, tenantId: ObjectId, createdAt: { diff --git a/src/models/supplier.js b/src/models/supplier.js index 8094a502..da313600 100644 --- a/src/models/supplier.js +++ b/src/models/supplier.js @@ -108,6 +108,7 @@ const supplierSchema = new mongoose.Schema( const deliveryBoySchema = new mongoose.Schema({ supplierId:{ type: String, default: null }, + suppliername:{ type: String, default: null }, name: { type: String, default: null }, phone: { type: String, default: null,unique:true }, alternativeContactNumber : { type : String,default: null }, @@ -121,6 +122,8 @@ const supplierSchema = new mongoose.Schema( zip: { type: String, default: null }, timestamp: { type: Date, default: Date.now }, status: { type: String, default: "Inactive" }, + longitude: { type : Number}, + latitude: {type: Number}, }); diff --git a/src/models/tanks.js b/src/models/tanks.js index b1edeb69..508f688d 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -39,9 +39,21 @@ const motordataSchema = new mongoose.Schema({ }); + + +const IOttankSchema = new mongoose.Schema({ + hardwareId: { type: String, required: true }, + tankHeight: { type: String, required: true }, + maxLevel: { type: String, required: true }, + minLevel: { type: String, required: true }, + mode: { type: Number, required: true } + }); + + + const Tank = mongoose.model("Tank", tanksSchema); const MotorData = mongoose.model("MotorData", motordataSchema); - +const IotData = mongoose.model("IotData", IOttankSchema); module.exports = { - Tank, MotorData + Tank, MotorData,IotData } diff --git a/src/routes/supplierOrdersRoutes.js b/src/routes/supplierOrdersRoutes.js index f556d363..9a6d6d29 100644 --- a/src/routes/supplierOrdersRoutes.js +++ b/src/routes/supplierOrdersRoutes.js @@ -77,6 +77,7 @@ module.exports = function (fastify, opts, next) { body: { type: "object", properties: { + suppliername: { type: "string"}, Name: { type: "string" }, phone: { type: "string" }, alternativeContactNumber : { type : "string" }, @@ -85,7 +86,8 @@ module.exports = function (fastify, opts, next) { state: { type: "string", default: null }, zip: { type: "string", default: null }, status: { type: "string" }, - + latitude: { type: 'number' }, + longitude: { type: 'number'} }, }, diff --git a/src/routes/supplierRoute.js b/src/routes/supplierRoute.js index bfc7e82b..d1835df3 100644 --- a/src/routes/supplierRoute.js +++ b/src/routes/supplierRoute.js @@ -161,6 +161,7 @@ module.exports = function (fastify, opts, next) { type: "object", required: ["phone"], properties: { + phoneVerificationCode: { type: "string" }, phone: { type: "string" }, }, }, diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 07a292d0..1dedcbd3 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -298,6 +298,41 @@ module.exports = function (fastify, opts, next) { width: { type: "string", default: null }, height: { type: "string" }, diameter:{ type: "string" }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: [ + // fastify.auth([fastify.operatorAuthenticate]), + // validationHandler.validatePhoneFormat, + // ], + preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.calculateCapacity, + }); + + + + fastify.route({ + method: "POST", + url: "/api/APIWrite", + schema: { + tags: ["Tank"], + description: "This is for cretae IOT Device", + summary: "This is for Create IOT Device.", + + body: { + type: "object", + properties: { + hardwareId: { type: "string" }, + tankHeight: { type: "string"}, + maxLevel: { type: "string" }, + minLevel: { type: "string" }, + + mode: { type: "string" }, }, }, security: [ @@ -310,11 +345,32 @@ module.exports = function (fastify, opts, next) { // fastify.auth([fastify.operatorAuthenticate]), // validationHandler.validatePhoneFormat, // ], - preHandler: fastify.auth([fastify.authenticate]), - handler: tanksController.calculateCapacity, - }); + + + + handler: tanksController.IotDevice, + + }); + fastify.get("/api/APIRead", { + schema: { + tags: ["Tank"], + description: "This is for Get IOT Data", + summary: "This is for to Get IOT Data", + querystring: { + hardwareId: {type: 'string'} + }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.getIotD, + }); + next(); } diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index 98c2eb0b..a58b1103 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -155,6 +155,8 @@ module.exports = function (fastify, opts, next) { zip: { type: "string", default: null }, country: { type: "string", default: null }, notes: { type: "string", default: null }, + latitude: { type: 'number' }, + longitude: { type: 'number'} }, }, security: [