diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 9020b91a..d536595b 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -394,36 +394,34 @@ exports.getTankmotordata = async (req, reply) => { } // Convert input dates to ISO 8601 format for Date comparison - const startISODate = moment(startDate, "DD-MMM-YYYY - HH:mm").toDate(); - const stopISODate = moment(stopDate, "DD-MMM-YYYY - HH:mm").toDate(); + const start = moment(startDate, "DD-MMM-YYYY - HH:mm").toDate(); + const end = moment(stopDate, "DD-MMM-YYYY - HH:mm").toDate(); + // Convert input dates to string format for string-based comparison - const startStringDate = moment(startDate, "DD-MMM-YYYY - HH:mm").format("DD-MMM-YYYY - HH:mm"); - const stopStringDate = moment(stopDate, "DD-MMM-YYYY - HH:mm").format("DD-MMM-YYYY - HH:mm"); + // Fetch the username based on customerId const user = await User.findOne({ customerId }).select("username"); if (user) { const userName = user.username || "N/A"; - - // Query the MotorData collection - const motorDataDocs = await MotorData.find({ + const motordatas = await MotorData.find({ customerId, - $or: [ - { - startTime: { $gte: startISODate, $lte: stopISODate }, // Date-based comparison - }, - { - startTime: { $gte: startStringDate, $lte: stopStringDate }, // String-based comparison - }, - ], - }).exec(); + + }); + + + const filtereddatas = motordatas.filter((record) => { + const recordTime = moment(record.startTime, "DD-MMM-YYYY - HH:mm").toDate(); + return recordTime >= start && recordTime <= end; + }); + reply.send({ status_code: 200, - data: motorDataDocs, - count: motorDataDocs.length, + data: filtereddatas, + count: filtereddatas.length, customerName: userName, }); } else {