From 2e5766792addb832b84f223beb1e683344369d04 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 27 Mar 2025 16:53:03 +0530 Subject: [PATCH] CHANGES --- src/controllers/tanksController.js | 80 ++++++++++++++++-------------- src/routes/tanksRoute.js | 20 ++++---- 2 files changed, 54 insertions(+), 46 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index b22e9ff9..4ca5ec63 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -7562,52 +7562,58 @@ exports.getActualWaterLevelInCm = async (req, reply) => { exports.compareMeasuredHeight = async (req, reply) => { try { - const { tankName } = req.params; - const { measuredHeight } = req.body; + const { measuredHeight, tankName } = req.body; - if (!tankName || measuredHeight === undefined) { - return reply.status(400).send({ message: "Tank name and measured height are required." }); - } + if (!tankName || measuredHeight === undefined) { + return reply.status(400).send({ message: "Tank name and measured height are required." }); + } - // Fetch tank details using tankName - const tank = await Tank.findOne({ tankName }); + // Convert measuredHeight to a number + const measuredHeightNum = parseFloat(measuredHeight); + if (isNaN(measuredHeightNum)) { + return reply.status(400).send({ message: "Invalid measuredHeight. It must be a number." }); + } - if (!tank) { - return reply.status(404).send({ message: "Tank not found." }); - } + // Fetch tank details using tankName + const tank = await Tank.findOne({ tankName }); - const actualWaterLevel = parseFloat(tank.waterlevel); // Current water level in liters - const waterCapacityPerCm = parseFloat(tank.waterCapacityPerCm); // Liters per cm + if (!tank) { + return reply.status(404).send({ message: "Tank not found." }); + } - if (!actualWaterLevel || !waterCapacityPerCm) { - return reply.status(400).send({ message: "Tank data is incomplete for conversion." }); - } + const actualWaterLevel = parseFloat(tank.waterlevel); // Current water level in liters + const waterCapacityPerCm = parseFloat(tank.waterCapacityPerCm); // Liters per cm - // Convert actual water level from liters to cm - const actualWaterLevelInCm = actualWaterLevel / waterCapacityPerCm; + if (isNaN(actualWaterLevel) || isNaN(waterCapacityPerCm)) { + return reply.status(400).send({ message: "Tank data is incomplete for conversion." }); + } - // Calculate difference between measured and actual water level in cm - const heightDifferenceInCm = Math.abs(actualWaterLevelInCm - measuredHeight); + // Convert actual water level from liters to cm + const actualWaterLevelInCm = actualWaterLevel / waterCapacityPerCm; - let message; - if (heightDifferenceInCm <= 10) { // Within 10 cm is considered a match - message = "Measured height is within an acceptable range of actual water level."; - } else { - message = "Measured height differs significantly from actual water level."; - } + // Calculate difference between measured and actual water level in cm + const heightDifferenceInCm = Math.abs(actualWaterLevelInCm - measuredHeightNum); - reply.send({ - status_code: 200, - data: { - tankName, - measuredHeight: measuredHeight.toFixed(2) + " cm", - actualWaterLevelInCm: actualWaterLevelInCm.toFixed(2) + " cm", - heightDifferenceInCm: heightDifferenceInCm.toFixed(2) + " cm", - message - } - }); + let message; + if (heightDifferenceInCm <= 10) { // Within 10 cm is considered a match + message = "Measured height is within an acceptable range of actual water level."; + } else { + message = "Measured height differs significantly from actual water level."; + } + + reply.send({ + status_code: 200, + data: { + tankName, + measuredHeight: measuredHeightNum.toFixed(2) + " cm", + actualWaterLevelInCm: actualWaterLevelInCm.toFixed(2) + " cm", + heightDifferenceInCm: heightDifferenceInCm.toFixed(2) + " cm", + message + } + }); } catch (err) { - reply.status(500).send({ message: err.message }); + console.error("Error in compareMeasuredHeight:", err); + reply.status(500).send({ message: err.message }); } -}; \ No newline at end of file +}; diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index b4aac760..e9d97ba0 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -655,19 +655,21 @@ module.exports = function (fastify, opts, next) { tags: ["Tank"], description: "Compare measured height with actual water level", summary: "Checks if measured height is within 10 cm of actual water level", - params: { - type: "object", - properties: { - tankName: { type: "string" } - }, - required: ["tankName"] - }, + // params: { + // type: "object", + // properties: { + // tankName: { type: "string" } + // }, + // required: ["tankName"] + // }, body: { type: "object", properties: { - measuredHeight: { type: "number" } + measuredHeight: { type: "string" }, + tankName: { type: "string" } + }, - required: ["measuredHeight"] + required: ["measuredHeight","tankName"] } }, handler: tanksController.compareMeasuredHeight