diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 7cc0c534..06413486 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -936,13 +936,6 @@ let supplier_tanks = []; -function parseDateTime(dateTimeStr) { - const [datePart, timePart] = dateTimeStr.split(' - '); - const [day, month, year] = datePart.split('-'); - const monthIndex = new Date(Date.parse(month +" 1, 2024")).getMonth(); // Parse the month name to get its index - const [hours, minutes] = timePart.split(':'); - return new Date(year, monthIndex, day, hours, minutes); -} @@ -952,10 +945,11 @@ exports.consumption = async (request, reply) => { const { customerId } = request.params; const { startDate, stopDate } = request.body; - const start = parseDateTime(startDate); - const end = parseDateTime(stopDate); + const start = startDate; + const end = stopDate; const tanks = await Tank.find({ customerId, tankLocation: "overhead" }); const tankData = []; + console.log(start,end) for (const tank of tanks) { const tankId = tank._id; @@ -974,6 +968,8 @@ exports.consumption = async (request, reply) => { } }); + console.log(tankConsumptions) + const total_consumption_from_records = tankConsumptions.reduce((acc, record) => { return acc + parseInt(record.consumption, 10); }, 0); @@ -2330,31 +2326,43 @@ cron.schedule('0 0 * * *', updatewaterlevelsatmidnight, { - - - - -const updatetotalConsumptiontillmidnight = async () => { +const updatetotalConsumptiontillmidnight = async () => { console.log('Cron job triggered at:', moment().tz('Asia/Kolkata').format()); try { const tanks = await Tank.find({}); for (const tank of tanks) { - 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 totalconsumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel - const newTankConsumption = new TankConsumptionSchema({ + 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 totalconsumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel; + + // Format the date in the desired format + const formattedDate = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); + + // Check if the record already exists + const existingRecord = await TankConsumptionSchema.findOne({ customerId: tank.customerId, tankName: tank.tankName, tankLocation: tank.tankLocation, - consumption: totalconsumption.toString(), - time: new Date().toISOString() + time: formattedDate }); - // Save the new document - await newTankConsumption.save(); - + if (!existingRecord) { + // Create and save the new document if it doesn't exist + const newTankConsumption = new TankConsumptionSchema({ + customerId: tank.customerId, + tankName: tank.tankName, + tankLocation: tank.tankLocation, + consumption: totalconsumption.toString(), + time: formattedDate // Save the formatted date + }); + + await newTankConsumption.save(); + console.log(`Created new record for tank ${tank.tankName} at ${formattedDate}`); + } else { + console.log(`Record already exists for tank ${tank.tankName} at ${formattedDate}`); + } } console.log('Waterlevel noted in waterlevel_at_midnight'); } catch (error) { @@ -2362,14 +2370,54 @@ const updatetotalConsumptiontillmidnight = async () => { } }; -// Schedule the task to run every day at 13:49 IST (1:49 PM IST) -cron.schedule('55 23 * * *', updatetotalConsumptiontillmidnight, { +// Schedule the task to run every day at 12:49 PM IST +cron.schedule('55 23 * * *', updatetotalConsumptiontillmidnight, { timezone: "Asia/Kolkata" }); +// const updatetotalConsumptiontillmidnight = async () => { +// console.log('Cron job triggered at:', moment().tz('Asia/Kolkata').format()); + +// try { +// const tanks = await Tank.find({}); +// for (const tank of tanks) { +// 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 totalconsumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel; + +// // Format the date in the desired format +// const formattedDate = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); + +// const newTankConsumption = new TankConsumptionSchema({ +// customerId: tank.customerId, +// tankName: tank.tankName, +// tankLocation: tank.tankLocation, +// consumption: totalconsumption.toString(), +// time: formattedDate // Save the formatted date +// }); + +// // Save the new document +// await newTankConsumption.save(); + +// } +// console.log('Waterlevel noted in waterlevel_at_midnight'); +// } catch (error) { +// console.error('Error occurred:', error); +// } +// }; + +// Schedule the task to run every day at 23:55 IST (11:55 PM IST) +// cron.schedule('55 23 * * *', updatetotalConsumptiontillmidnight, { +// timezone: "Asia/Kolkata" +// }); + + + + exports.deletemotordatarecordsbefore7days = async (req, reply) => { try {