ashok 6 months ago
commit b646f61f14

@ -1041,8 +1041,6 @@ exports.getIotDataByCustomer = async (req, reply) => {
// 1. Fetch all insensors for this customer // 1. Fetch all insensors for this customer
const sensors = await Insensors.find({ customerId }); const sensors = await Insensors.find({ customerId });
console.log("Sensors found:", sensors.length);
if (!sensors || sensors.length === 0) { if (!sensors || sensors.length === 0) {
return reply.code(404).send({ message: "No sensors found for this customer." }); return reply.code(404).send({ message: "No sensors found for this customer." });
} }
@ -1056,22 +1054,19 @@ exports.getIotDataByCustomer = async (req, reply) => {
), ),
]; ];
console.log("Collected hardwareIds:", hardwareIds);
if (hardwareIds.length === 0) { if (hardwareIds.length === 0) {
return reply.send({ status_code: 200, message: "No connected hardwareIds found", data: [] }); return reply.send({ status_code: 200, message: "No connected hardwareIds found", data: [] });
} }
// 3. Fetch latest 3 records per hardwareId // 3. Fetch latest record per hardwareId
const latestDataPromises = hardwareIds.map(async hardwareId => { const latestDataPromises = hardwareIds.map(async hardwareId => {
const records = await IotData.find({ hardwareId }) const latestRecord = await IotData.findOne({ hardwareId })
.sort({ date: -1 }) .sort({ date: -1 })
.limit(3)
.lean(); .lean();
return { return {
hardwareId, hardwareId,
records, latestRecord
}; };
}); });
@ -1080,11 +1075,13 @@ exports.getIotDataByCustomer = async (req, reply) => {
return reply.send({ return reply.send({
status_code: 200, status_code: 200,
message: "Success", message: "Success",
data: iotDataGrouped, data: iotDataGrouped
}); });
} catch (err) { } catch (err) {
console.error("Error fetching IoT data by customerId:", err); console.error("Error fetching IoT data by customerId:", err);
return reply.code(500).send({ error: "Internal Server Error" }); return reply.code(500).send({ error: "Internal Server Error" });
} }
}; };

Loading…
Cancel
Save