From bcaed8bac054f997f1323d5f57540218faaf5919 Mon Sep 17 00:00:00 2001 From: varun Date: Tue, 25 Jun 2024 02:16:10 -0400 Subject: [PATCH] consumed water till night --- src/controllers/tanksController.js | 60 ++++++++++---------------- src/models/tanks.js | 5 --- src/routes/tanksRoute.js | 69 ++++++++++++++++++++++++------ 3 files changed, 77 insertions(+), 57 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 6d20881a..25a490ad 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -824,52 +824,35 @@ let supplier_tanks = []; // }; -exports.consumption = async (request, reply) => { - try { - const customerId = req.params.customerId; - const tanks = await Tank.find({ customerId,tankLocation:"overhead"}); - const tankData = []; - for (const tank of tanks) { - const tankId = tank._id; - const waterlevel_at_midnight = parseInt(tank.waterlevel_at_midnight.replace(/,/g, ''), 10); - const total_water_added_from_midnight = parseInt(tank.total_water_added_from_midnight.replace(/,/g, ''), 10); - const waterlevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10); +// exports.consumption = async (request, reply) => { +// try { +// const customerId = req.params.customerId; +// const tanks = await Tank.find({ customerId,tankLocation:"overhead"}); +// const tankData = []; +// for (const tank of tanks) { +// const tankId = tank._id; +// const waterlevel_at_midnight = parseInt(tank.waterlevel_at_midnight.replace(/,/g, ''), 10); +// const total_water_added_from_midnight = parseInt(tank.total_water_added_from_midnight.replace(/,/g, ''), 10); +// const waterlevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10); - const tankname = tank.tankName - const consumption = (waterlevel_at_midnight+total_water_added_from_midnight)-waterlevel +// const tankname = tank.tankName +// const consumption = (waterlevel_at_midnight+total_water_added_from_midnight)-waterlevel - tankData.push({ tankname, waterLevel: newWaterLevel }); +// tankData.push({ tankname, waterLevel: consumption }); - // console.log(newWaterLevel); +// // console.log(newWaterLevel); - } - reply.send({ status_code: 200, tankData }); - - return { message: 'Water level updates started' }; - } catch (err) { - throw boom.boomify(err); - } - - - - // try { - // const { customerId } = request.params; +// } +// reply.send({ status_code: 200, tankData }); - // const tank = await Tank.find({ customerId, tankLocation:"overhead" }); +// return { message: 'Water level updates started' }; +// } catch (err) { +// throw boom.boomify(err); +// } - // if (!tank) { - // return reply.status(404).send({ error: 'Tank not found' }); - // } - // // Here you can process and return the consumption data as needed - // return reply.send(tank); - // } catch (error) { - // request.log.error(error); - // return reply.status(500).send({ error: 'Internal Server Error' }); - // } -}; function parseDateTime(dateTimeStr) { @@ -884,6 +867,7 @@ function parseDateTime(dateTimeStr) { exports.consumption = async (request, reply) => { try { + const { customerId } = request.params; const { startDate, stopDate } = request.body; @@ -898,7 +882,7 @@ exports.consumption = async (request, reply) => { const total_water_added_from_midnight = parseInt(tank.total_water_added_from_midnight.replace(/,/g, ''), 10); const waterlevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10); const tankname = tank.tankName; - + console.log(waterlevel_at_midnight,total_water_added_from_midnight,waterlevel) const tankConsumptions = await TankConsumptionSchema.find({ customerId, tankName: tank.tankName, diff --git a/src/models/tanks.js b/src/models/tanks.js index 903c4b57..4c4dae84 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -45,11 +45,6 @@ const tanksSchema = new mongoose.Schema({ waterCapacityPerCm:{ type: String, default: "0" }, typeOfWater: { type: String, default: null }, waterlevel: { type: String, default: "0" }, - tankLocation: { type: String, default: null }, - motor_status: { type: String, default: "1" }, - motor_stop_status: { type: String, default: "1" }, - motor_speed: { type: String, default: "100" }, - motor_temperature: { type: String, default: "0" }, waterlevel_at_midnight:{ type: String,default:"0" }, total_water_added_from_midnight:{ type: String,default:"0" }, auto_min_percentage :{ type: String, default: "20" }, diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 4b11695b..7cf084b2 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -308,8 +308,45 @@ module.exports = function (fastify, opts, next) { }); + // fastify.route({ + // method: "PUT", + // url: "/api/consumption/:customerId", + // schema: { + // tags: ["Tank"], + // summary: "This is for getting consumption", + // params: { + // required: ["customerId"], + // type: "object", + // properties: { + // customerId: { + // type: "string", + // description: "customerId", + // }, + // }, + // }, + + // body: { + // type: "object", + + // properties: { + + // startDate:{ type: "string" }, + // stopDate:{type:"string"}, + + // }, + // }, + // security: [ + // { + // basicAuth: [], + // }, + // ], + // }, + // //preHandler: fastify.auth([fastify.authenticate]), + // handler: tanksController.consumption, + // }); + fastify.route({ - method: "GET", + method: "PUT", url: "/api/consumption/:customerId", schema: { tags: ["Tank"], @@ -324,19 +361,17 @@ module.exports = function (fastify, opts, next) { }, }, }, - // querystring: { - // tankName: {type: 'string'} - // }, - body: { - type: "object", - // required: ['phone'], - properties: { - - startDate:{ type: "string" }, - stopDate:{type:"string"}, - - }, - }, + + body: { + type: "object", + // required: ['phone'], + properties: { + startDate:{ type: "string" }, + stopDate:{type:"string"}, + + + }, + }, security: [ { basicAuth: [], @@ -347,6 +382,12 @@ module.exports = function (fastify, opts, next) { handler: tanksController.consumption, }); + + + + + + fastify.route({ method: "PUT", url: "/api/calculateCapacity",