master
Varun 12 months ago
parent 22625f9d92
commit b3c29cf9f4

@ -3791,7 +3791,7 @@ const updatetotalConsumptiontillmidnight = async () => {
const formattedDate = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); const formattedDate = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
// Check if the record already exists // Check if the record already exists
const existingRecord = await TankConsumptionOriginalSchema.findOne({ const existingRecord = await TankConsumptionSchema.findOne({
customerId: tank.customerId, customerId: tank.customerId,
tankName: tank.tankName, tankName: tank.tankName,
tankLocation: tank.tankLocation, tankLocation: tank.tankLocation,
@ -3800,7 +3800,7 @@ const updatetotalConsumptiontillmidnight = async () => {
if (!existingRecord) { if (!existingRecord) {
// Create and save the new document if it doesn't exist // Create and save the new document if it doesn't exist
const newTankConsumption = new TankConsumptionOriginalSchema({ const newTankConsumption = new TankConsumptionSchema({
customerId: tank.customerId, customerId: tank.customerId,
tankName: tank.tankName, tankName: tank.tankName,
tankLocation: tank.tankLocation, tankLocation: tank.tankLocation,
@ -4408,14 +4408,18 @@ client.on('message', async (topic, message) => {
// }; // };
//const moment = require('moment');
exports.consumptionofparticulartank = async (request, reply) => { exports.consumptionofparticulartank = async (request, reply) => {
try { try {
const { customerId } = request.params; const { customerId } = request.params;
const { startDate, stopDate, tankName, tankLocation, block } = request.body; const { startDate, stopDate, tankName, tankLocation, block } = request.body;
// Ensure dates are formatted or parsed correctly for the query // Convert input dates into strings in the same format as stored in the database
const start = startDate; const start = moment(startDate, "DD-MMM-YYYY - HH:mm").format("DD-MMM-YYYY - HH:mm");
const end = stopDate; const end = moment(stopDate, "DD-MMM-YYYY - HH:mm").format("DD-MMM-YYYY - HH:mm");
// Find the tank by customerId, tankLocation, and tankName // Find the tank by customerId, tankLocation, and tankName
const tank = await Tank.findOne({ const tank = await Tank.findOne({
@ -4435,17 +4439,16 @@ exports.consumptionofparticulartank = async (request, reply) => {
const total_water_added_from_midnight = parseInt(tank.total_water_added_from_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 waterlevel = parseInt(tank.waterlevel.replace(/,/g, ""), 10);
// Find consumption records for the tank between the given dates // Find consumption records by comparing string dates
const tankConsumptions = await TankConsumptionOriginalSchema.find({ const tankConsumptions = await TankConsumptionOriginalSchema.find({
customerId, customerId,
tankName, tankName,
tankLocation: tankLocation, tankLocation: tankLocation,
time: { time: {
$gte: start, $gte: start, // Start date as string
$lte: end, $lte: end, // End date as string
}, }
}).sort({ time: 1 });
});
// Calculate total consumption from records // Calculate total consumption from records
const total_consumption_from_records = tankConsumptions.reduce((acc, record) => { const total_consumption_from_records = tankConsumptions.reduce((acc, record) => {
@ -4479,3 +4482,5 @@ exports.consumptionofparticulartank = async (request, reply) => {
}; };

Loading…
Cancel
Save