diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 3467b622..efbe2877 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -4453,7 +4453,7 @@ exports.consumptionofparticulartank = async (request, reply) => { customerId, tankName, tankLocation: tankLocation, - }).sort({ time: 1 }); + }); // Filter records in JavaScript by comparing the 'time' field after converting to Date const filteredConsumptions = tankConsumptions.filter((record) => { @@ -4461,6 +4461,13 @@ exports.consumptionofparticulartank = async (request, reply) => { return recordTime >= start && recordTime <= end; }); + // Sort filtered records by date (ascending) + filteredConsumptions.sort((a, b) => { + const dateA = moment(a.time, "DD-MMM-YYYY - HH:mm").toDate(); + const dateB = moment(b.time, "DD-MMM-YYYY - HH:mm").toDate(); + return dateA - dateB; // Sort in ascending order + }); + // Calculate total consumption from filtered records const total_consumption_from_records = filteredConsumptions.reduce((acc, record) => { return acc + parseInt(record.consumption, 10); @@ -4496,3 +4503,5 @@ exports.consumptionofparticulartank = async (request, reply) => { + +