diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 20c032f1..232aaa98 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -11,6 +11,16 @@ const cron = require('node-cron'); const moment = require('moment'); +async function deleteOldRecords() { + const SEVEN_DAYS_IN_MILLISECONDS = 7 * 24 * 60 * 60 * 1000; + const sevenDaysAgo = new Date(Date.now() - SEVEN_DAYS_IN_MILLISECONDS); + + await MotorData.deleteMany({ startTime: { $lt: sevenDaysAgo } }); +} + + + + exports.addTanks = async (req, reply) => { try { @@ -250,11 +260,11 @@ exports.getTanklevels = async (req, reply) => { }; const intervals = {}; - +let start_time="" exports.motorAction = async (req, reply) => { try { - let start_time,stop_time + const customerId = req.params.customerId; const action = req.body.action @@ -270,7 +280,8 @@ exports.motorAction = async (req, reply) => { const interval_variable = supplier_tank+receiver_tank if(action === "start"){ - start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + console.log(start_time) // const stop_at = req.body.stop_at @@ -579,13 +590,14 @@ exports.motorAction = async (req, reply) => { const tankToUpdate = await Tank.findOne({ customerId, tankName: receiver_tank, tankLocation: receiver_type }); // Check if all objects in inputConnections have motor_status === "0" - const allMotorStatusZero = tankToUpdate.connections.inputConnections.every(connection => connection.motor_status === "0"); + const allMotorStatusZero = tankToUpdate.connections.inputConnections.every(connection => connection.motor_status === "0"); - if (allMotorStatusZero) { + if (allMotorStatusZero) { + console.log(allMotorStatusZero) // Update the motor_status field to "0" for the tank - await Tank.findOneAndUpdate( - { customerId, tankName: receiver_tank, tankLocation: receiver_type }, - { $set: { motor_status: "0" } } + await Tank.findOneAndUpdate( + { customerId, tankName: receiver_tank, tankLocation: receiver_type }, + { $set: { motor_status: "0" } } ); } @@ -598,8 +610,8 @@ exports.motorAction = async (req, reply) => { supplier_type: supplier_tank_type, receiverTank: receiver_tank, receiver_type: receiver_type, - // startTime: startTime, - stopTime: req.body.stopTime, + startTime: start_time, + stopTime: stop_time, @@ -615,7 +627,7 @@ exports.motorAction = async (req, reply) => { motorData.receiverTank = receiver_tank; motorData.supplier_type = supplier_type; motorData.receiver_type = receiver_type; - //motorData.startTime = startTime; + motorData.startTime = start_time; motorData.stopTime = stop_time; @@ -1065,3 +1077,26 @@ exports.updatewaterlevelsatmidnight = async (req, reply) => { throw boom.boomify(err); } }; + + + +exports.deletemotordatarecordsbefore7days = async (req, reply) => { + try { + // Schedule the task to run every day at 10 seconds past the minute + cron.schedule('0 0 * * *', async () => { + try { + // Run the deletion task once a day + setInterval(async () => { + await deleteOldRecords(); + }, 24 * 60 * 60 * 1000); // 24 hours in milliseconds + + } catch (error) { + console.error('Error occurred:', error); + } + }); + + + } catch (err) { + throw boom.boomify(err); + } +}; diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 49543548..194e0b91 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -244,7 +244,7 @@ module.exports = function (fastify, opts, next) { action: { type: "string" }, percentage: { type: "string",default: "100" }, startTime:{ type: "string" }, - stopTime:{ type: "string" }, + stop_at:{type:"number"} }, @@ -523,7 +523,26 @@ module.exports = function (fastify, opts, next) { preHandler: fastify.auth([fastify.authenticate]), handler: tanksController.updatewaterlevelsatmidnight, }); + + + fastify.get("/api/deletemotordatarecordsbefore7days", { + schema: { + tags: ["Tank"], + description: "This is for deleting the data before 7 days in motorsdata", + summary: "This is for deleting the data before 7 days in motorsdata", + querystring: { + customerId: {type: 'string'} + }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.deletemotordatarecordsbefore7days, + }); next(); }