From 35637b89ba4c22d90e0dc6d9f04f759656879bfe Mon Sep 17 00:00:00 2001 From: varun Date: Tue, 8 Aug 2023 03:40:46 -0400 Subject: [PATCH] hardware integration to tank --- src/controllers/tanksController.js | 128 +++++++++++++++++++---------- src/routes/tanksRoute.js | 3 +- 2 files changed, 86 insertions(+), 45 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index fd8bc038..d98e0471 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -21,64 +21,104 @@ async function deleteOldRecords() { -exports.addTanks = async (req, reply) => { - try { +// exports.addTanks = async (req, reply) => { +// try { + +// var customerId = req.params.customerId +// //const username = req.params.username; + +// console.log(req.params); +// //const {username} = loginObject.user.username; +// //console.log(loginObject.user.username) +// // const userInfo = await User.findOne({ username: username.toString() }); +// // const updateData = req.body; - var customerId = req.params.customerId - //const username = req.params.username; -console.log(req.params); - //const {username} = loginObject.user.username; - //console.log(loginObject.user.username) - // const userInfo = await User.findOne({ username: username.toString() }); - // const updateData = req.body; +// // console.log("This is the reply in the handler after the validations", reply); +// tankData = { +// customerId: customerId, +// hardwareId: req.body.hardwareId, +// tankName: req.body.tankName, +// blockName: req.body.blockName, +// capacity: req.body.capacity, +// typeOfWater: req.body.typeOfWater, + +// tankLocation:req.body.tankLocation.toLowerCase(), +// }; +// //console.log( req.body.tankLocation.toLowerCase()) + +// var tank_name = req.body.tankName +// var tankLocation = req.body.tankLocation.toLowerCase() +// var i_tank = await Tank.findOne({ tankName: tank_name,customerId:customerId,tankLocation:tankLocation}) +// if(i_tank){ +// throw new Error('tankname already exists'); +// } +// else { + +// var tank = new Tank(tankData); + +// checkFormEncoding = isUserFormUrlEncoded(req); +// if (checkFormEncoding.isUserFormUrlEncoded) { +// usertobeInserted = checkFormEncoding.tank; +// console.log("thsi true url string"); +// tank.customerId = usertobeInserted.customerId +// tank.hardwareId = usertobeInserted.hardwareId; +// tank.tankName = usertobeInserted.tankName; +// tank.blockName = usertobeInserted.blockName; +// tank.capacity = usertobeInserted.capacity; +// tank.typeOfWater = usertobeInserted.typeOfWater; + +// tank.tankLocation = (usertobeInserted.tankLocation).toLowerCase(); +// console.log((usertobeInserted.tankLocation).toLowerCase()) +// } +// } +// const insertedTank = await tank.save(); - // console.log("This is the reply in the handler after the validations", reply); - tankData = { +// return insertedTank; + + +// } catch (err) { +// throw boom.boomify(err); +// } +// }; + + +exports.addTanks = async (req, reply) => { + try { + const customerId = req.params.customerId; + const { hardwareId, tankhardwareId } = req.body; + + // Check if the combination of hardwareId and tankhardwareId already exists + const existingTank = await Tank.findOne({ customerId: customerId, - hardwareId: req.body.hardwareId, + hardwareId: hardwareId, + tankhardwareId: tankhardwareId, + tankName:tankName, + tankLocation:tankLocation.toLowerCase() + }); + + if (existingTank) { + throw new Error('The combination of hardwareId and tankhardwareId already exists.'); + } + + const tankData = { + customerId: customerId, + hardwareId: hardwareId, + tankhardwareId: tankhardwareId, tankName: req.body.tankName, blockName: req.body.blockName, capacity: req.body.capacity, typeOfWater: req.body.typeOfWater, - - tankLocation:req.body.tankLocation.toLowerCase(), - + tankLocation: req.body.tankLocation.toLowerCase(), + // ... other fields }; - //console.log( req.body.tankLocation.toLowerCase()) - - var tank_name = req.body.tankName - var tankLocation = req.body.tankLocation.toLowerCase() - var i_tank = await Tank.findOne({ tankName: tank_name,customerId:customerId,tankLocation:tankLocation}) - if(i_tank){ - throw new Error('tankname already exists'); - } - else { - - var tank = new Tank(tankData); - - checkFormEncoding = isUserFormUrlEncoded(req); - if (checkFormEncoding.isUserFormUrlEncoded) { - usertobeInserted = checkFormEncoding.tank; - console.log("thsi true url string"); - tank.customerId = usertobeInserted.customerId - tank.hardwareId = usertobeInserted.hardwareId; - tank.tankName = usertobeInserted.tankName; - tank.blockName = usertobeInserted.blockName; - tank.capacity = usertobeInserted.capacity; - tank.typeOfWater = usertobeInserted.typeOfWater; - - tank.tankLocation = (usertobeInserted.tankLocation).toLowerCase(); - console.log((usertobeInserted.tankLocation).toLowerCase()) - } - } + + const tank = new Tank(tankData); const insertedTank = await tank.save(); return insertedTank; - - } catch (err) { throw boom.boomify(err); } diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 0a29c483..5621e99b 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -24,6 +24,7 @@ module.exports = function (fastify, opts, next) { body: { type: "object", properties: { + tankhardwareId: { type: "string" }, hardwareId: { type: "string" }, tankName: { type: "string" }, blockName: { type: "string"}, @@ -383,7 +384,7 @@ module.exports = function (fastify, opts, next) { }, ], }, - + //preHandler: fastify.auth([fastify.authenticate]), handler: tanksController.getIotD, });