hardware integration to tank

master
varun 2 years ago
parent 40a600f1ea
commit 35637b89ba

@ -21,64 +21,104 @@ async function deleteOldRecords() {
exports.addTanks = async (req, reply) => { // exports.addTanks = async (req, reply) => {
try { // 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); // return insertedTank;
tankData = {
// } 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, 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, tankName: req.body.tankName,
blockName: req.body.blockName, blockName: req.body.blockName,
capacity: req.body.capacity, capacity: req.body.capacity,
typeOfWater: req.body.typeOfWater, typeOfWater: req.body.typeOfWater,
tankLocation: req.body.tankLocation.toLowerCase(),
tankLocation:req.body.tankLocation.toLowerCase(), // ... other fields
}; };
//console.log( req.body.tankLocation.toLowerCase())
const tank = new Tank(tankData);
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(); const insertedTank = await tank.save();
return insertedTank; return insertedTank;
} catch (err) { } catch (err) {
throw boom.boomify(err); throw boom.boomify(err);
} }

@ -24,6 +24,7 @@ module.exports = function (fastify, opts, next) {
body: { body: {
type: "object", type: "object",
properties: { properties: {
tankhardwareId: { type: "string" },
hardwareId: { type: "string" }, hardwareId: { type: "string" },
tankName: { type: "string" }, tankName: { type: "string" },
blockName: { type: "string"}, blockName: { type: "string"},
@ -383,7 +384,7 @@ module.exports = function (fastify, opts, next) {
}, },
], ],
}, },
//preHandler: fastify.auth([fastify.authenticate]), //preHandler: fastify.auth([fastify.authenticate]),
handler: tanksController.getIotD, handler: tanksController.getIotD,
}); });

Loading…
Cancel
Save