|
|
|
@ -1041,8 +1041,6 @@ exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|
// 1. Fetch all insensors for this customer
|
|
|
|
|
const sensors = await Insensors.find({ customerId });
|
|
|
|
|
|
|
|
|
|
console.log("Sensors found:", sensors.length);
|
|
|
|
|
|
|
|
|
|
if (!sensors || sensors.length === 0) {
|
|
|
|
|
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) {
|
|
|
|
|
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 records = await IotData.find({ hardwareId })
|
|
|
|
|
const latestRecord = await IotData.findOne({ hardwareId })
|
|
|
|
|
.sort({ date: -1 })
|
|
|
|
|
.limit(3)
|
|
|
|
|
.lean();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
hardwareId,
|
|
|
|
|
records,
|
|
|
|
|
latestRecord
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -1080,11 +1075,13 @@ exports.getIotDataByCustomer = async (req, reply) => {
|
|
|
|
|
return reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "Success",
|
|
|
|
|
data: iotDataGrouped,
|
|
|
|
|
data: iotDataGrouped
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Error fetching IoT data by customerId:", err);
|
|
|
|
|
return reply.code(500).send({ error: "Internal Server Error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|