From d02394b8273417c6d3086c228cece1505e34351b Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 22 Aug 2024 03:37:45 -0400 Subject: [PATCH] get blocks --- src/controllers/tanksController.js | 35 +++++++++++++++++++++++++----- src/models/tanks.js | 1 + src/routes/tanksRoute.js | 30 +++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index f4b5e9e9..07d9023d 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -942,15 +942,17 @@ let supplier_tanks = []; exports.consumption = async (request, reply) => { try { const { customerId } = request.params; - const { startDate, stopDate, block, typeofwater } = request.body; - + const { startDate, stopDate, block } = req.body; + let { typeofwater } = req.body; + // Convert typeofwater to lowercase + typeofwater = typeofwater.toLowerCase(); const start = startDate; const end = stopDate; // Construct the query object based on block and typeofwater inputs const tankQuery = { customerId, tankLocation: "overhead" }; - if (block !== "all") { + if (block !== "All") { tankQuery.blockName = block; // Filter by specific block if not "all" } @@ -975,7 +977,7 @@ exports.consumption = async (request, reply) => { $gte: start, $lte: end }, - ...(block !== "all" && { block: tank.blockName }), // Ensure correct field names + ...(block !== "All" && { block: tank.blockName }), // Ensure correct field names ...(typeofwater !== "all" && { typeofwater: tank.typeOfWater }) // Ensure correct field names }); @@ -3131,4 +3133,27 @@ setInterval(storeWaterLevels, 15 * 60 * 1000); -console.log('Cron job scheduled to update water levels at midnight'); \ No newline at end of file +console.log('Cron job scheduled to update water levels at midnight'); + + +exports.getBlockData = async (req, reply) => { + try { + const customerId = req.params.customerId; + + // Get all tank documents for the current customerId + const tanks = await Tank.find({ customerId }); + + // Extract the blockName from each tank + const blockNames = tanks.map(tank => tank.blockName); + + // Remove duplicates by converting the array to a Set and then back to an array + const uniqueBlockNames = [...new Set(blockNames)]; + + // Send the unique blockNames in the response + reply.code(200).send({ blockNames: uniqueBlockNames }); + + } catch (err) { + // Send an error response + reply.code(500).send({ error: err.message }); + } +}; diff --git a/src/models/tanks.js b/src/models/tanks.js index 487eec58..67469115 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -107,6 +107,7 @@ const motordataSchema = new mongoose.Schema({ receiverfinalwaterlevel: { type: String, default: "0" }, startTime: { type: String, default: null }, stopTime: { type: String, default: null }, + runtime:{type:String, default:"0"}, supplier_type: { type: String, default: null }, receiver_type: { type: String, default: null }, quantity_delivered:{ type: String, default: null }, diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index eeb66d6d..f51c7fea 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -935,6 +935,36 @@ module.exports = function (fastify, opts, next) { //preHandler: fastify.auth([fastify.authenticate]), handler: tanksController.update_auto_percentage, }); + + + fastify.route({ + method: "GET", + url: "/api/getBlockData/:customerId", + schema: { + tags: ["Tank"], + summary: "This is for get blocks under particular user", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + // querystring: { + // tankName: {type: 'string'} + // }, + security: [ + { + basicAuth: [], + }, + ], + }, + //preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.getBlockData, + }); next(); }