master^2
Bhaskar 6 months ago
parent 81f46e1d60
commit 35d081d9cc

@ -1038,14 +1038,12 @@ exports.getIotDataByCustomer = async (req, reply) => {
return reply.code(400).send({ error: "customerId is required" });
}
// 1. Fetch all insensors for this customer
const sensors = await Insensors.find({ customerId });
if (!sensors || sensors.length === 0) {
return reply.code(404).send({ message: "No sensors found for this customer." });
}
// 2. Get unique connected_to hardwareIds
const hardwareIds = [
...new Set(
sensors
@ -1058,15 +1056,27 @@ exports.getIotDataByCustomer = async (req, reply) => {
return reply.send({ status_code: 200, message: "No connected hardwareIds found", data: [] });
}
// 3. Fetch latest record per hardwareId
const latestDataPromises = hardwareIds.map(async hardwareId => {
const latestRecord = await IotData.findOne({ hardwareId })
.sort({ date: -1 })
.lean();
// Determine the message based on tanks data
let message = "GSM is not connected";
if (latestRecord && Array.isArray(latestRecord.tanks)) {
const hasTankData = latestRecord.tanks.some(
tank => tank.tankHeight && parseFloat(tank.tankHeight) > 0
);
if (hasTankData) {
message = "GSM is connected";
}
}
return {
hardwareId,
latestRecord
message,
latestRecord,
};
});
@ -1085,3 +1095,4 @@ exports.getIotDataByCustomer = async (req, reply) => {
};

Loading…
Cancel
Save