From 2471c903f20159900e2aad93950d02e1a2acdfa4 Mon Sep 17 00:00:00 2001 From: Varun Date: Tue, 28 Jan 2025 14:54:54 +0530 Subject: [PATCH] changes in motor data --- src/controllers/tanksController.js | 44 +++++++++++++++++++----- src/routes/tanksRoute.js | 54 ++++++++++++++++++++++++++---- 2 files changed, 84 insertions(+), 14 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 2f3a9375..dae6bd1a 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -352,15 +352,43 @@ exports.getTanksofParticularInstaller = async (req, reply) => { exports.getTankmotordata = async (req, reply) => { try { - await MotorData.find({customerId: req.query.customerId}) - .exec() - .then((docs) => { - reply.send({ status_code: 200, data: docs, count: docs.length }); - }) - .catch((err) => { - console.log(err); - reply.send({ error: err }); + const { startDate, stopDate, block } = req.body; + const { customerId } = req.params; + const start = moment(startDate, "DD-MMM-YYYY - HH:mm").toDate(); + const end = moment(stopDate, "DD-MMM-YYYY - HH:mm").toDate(); + + // Fetch the name from the User collection based on customerId, only from profile + const user = await User.findOne({ customerId }) + .select("profile.firstName profile.lastName"); + + if (user) { + // Get the full name (combine firstName and lastName if available) + const userName = user.profile.firstName && user.profile.lastName + ? `${user.profile.firstName} ${user.profile.lastName}` + : "N/A"; // Fallback if no name is found + + // Construct the query object for motor data based on customerId + const motorQuery = { customerId }; + + // If block is provided (and not "All"), add it to the query + if (block !== "All") motorQuery.block = block; + + // Fetch motor data for the customerId within the time range + const motorDataDocs = await MotorData.find(motorQuery) + .where("date") + .gte(start) // Greater than or equal to startDate + .lte(end) // Less than or equal to stopDate + .exec(); + + reply.send({ + status_code: 200, + data: motorDataDocs, + count: motorDataDocs.length, + customerName: userName, // Add the username to the response }); + } else { + reply.send({ status_code: 404, message: "User not found" }); + } } catch (err) { throw boom.boomify(err); } diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 9b52a1b3..6ba0858f 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -821,13 +821,54 @@ module.exports = function (fastify, opts, next) { handler: tanksController.deletemotordatarecordsbefore7days, }); - fastify.get("/api/getTankmotordata", { + // fastify.get("/api/getTankmotordata", { + // schema: { + // tags: ["Tank"], + // description: "This is for Get Tank Motor Data", + // summary: "This is for to Get Tank Motor Data", + // querystring: { + // customerId: {type: 'string'} + // }, + // security: [ + // { + // basicAuth: [], + // }, + // ], + // }, + // preHandler: fastify.auth([fastify.authenticate]), + // handler: tanksController.getTankmotordata, + // }); + + + + fastify.route({ + method: "PUT", + url: "/api/getTankmotordata/:customerId", schema: { tags: ["Tank"], - description: "This is for Get Tank Motor Data", - summary: "This is for to Get Tank Motor Data", - querystring: { - customerId: {type: 'string'} + summary: "This is for Get Tank Motor Data", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + + body: { + type: "object", + // required: ['phone'], + properties: { + startDate:{ type: "string" }, + stopDate:{type:"string"}, + block:{type:"string"}, + typeofwater:{type:"string"}, + + + }, }, security: [ { @@ -835,7 +876,7 @@ module.exports = function (fastify, opts, next) { }, ], }, - preHandler: fastify.auth([fastify.authenticate]), + //preHandler: fastify.auth([fastify.authenticate]), handler: tanksController.getTankmotordata, }); @@ -843,6 +884,7 @@ module.exports = function (fastify, opts, next) { + fastify.post('/api/motor/write', { schema: { tags: ["Tank"],