diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index f916211a..fa9ad4a7 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -1061,8 +1061,6 @@ exports.getMasterSlaveSummary = async (req, reply) => { // } // }; - - exports.getIotDataByCustomer = async (req, reply) => { try { const { customerId } = req.params; @@ -1071,12 +1069,14 @@ exports.getIotDataByCustomer = async (req, reply) => { return reply.code(400).send({ error: "customerId is required" }); } + // Fetch sensors for the customer const sensors = await Insensors.find({ customerId }); if (!sensors || sensors.length === 0) { return reply.code(404).send({ message: "No sensors found for this customer." }); } + // Extract unique connected hardwareIds const hardwareIds = [ ...new Set( sensors @@ -1089,12 +1089,15 @@ exports.getIotDataByCustomer = async (req, reply) => { return reply.send({ status_code: 200, message: "No connected hardwareIds found", data: [] }); } + // Fetch latest IoT data and merge with sensor info const latestDataPromises = hardwareIds.map(async hardwareId => { const latestRecord = await IotData.findOne({ hardwareId }) .sort({ date: -1 }) .lean(); - // Determine the message based on tanks data + const relatedSensor = sensors.find(sensor => sensor.connected_to?.trim() === hardwareId); + + // Determine GSM status message let message = "GSM is not connected"; if (latestRecord && Array.isArray(latestRecord.tanks)) { const hasTankData = latestRecord.tanks.some( @@ -1109,7 +1112,8 @@ exports.getIotDataByCustomer = async (req, reply) => { hardwareId, message, latestRecord, - + masterName: relatedSensor?.masterName ?? null, + location: relatedSensor?.location ?? null }; }); @@ -1126,6 +1130,3 @@ exports.getIotDataByCustomer = async (req, reply) => { return reply.code(500).send({ error: "Internal Server Error" }); } }; - - - diff --git a/src/models/store.js b/src/models/store.js index 6d7be765..52aac643 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -362,7 +362,8 @@ const insensorsSchema = new mongoose.Schema({ tankLocation: { type: String, default: "0" }, connected_slave: { type: String, default: null }, connected_status: { type: String, enum: ["connected", "Not connected", "unknown"], default: "unknown" }, - + masterName: { type: String, default: null }, + location: { type: String, default: null }, quality_check_details: [{ damage_check: { result: String }, stickering_check: { result: String },