diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index e81e388d..693d33e6 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -586,7 +586,7 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { console.log("Fetching details for hardwareId:", hardwareId); - // Fetch the latest 3 records sorted by date descending + // Fetch latest 3 IoT records const iotData = await IotData.find({ hardwareId }) .sort({ date: -1 }) .limit(3) @@ -596,20 +596,23 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { return reply.send({ status_code: 404, message: "IoT Data not found", data: null }); } - const latestRecord = iotData[0]; // Most recent one + const latestRecord = iotData[0]; // Most recent record - // Format current timestamp or use `latestRecord.date` - const now = new Date(latestRecord.date || Date.now()); - const connected_gsm_date = now.toISOString().split('T')[0]; // yyyy-mm-dd - const connected_gsm_time = now.toTimeString().split(' ')[0]; // hh:mm:ss + // Step 1: Get GSM Date from top-level date + const connected_gsm_date = new Date(latestRecord.date).toISOString().split('T')[0]; // yyyy-mm-dd - // Update insensor where connected_to = hardwareId + // Step 2: Get GSM Time from first tank time if available + const firstTankTime = latestRecord.tanks?.[0]?.time || new Date(latestRecord.date).toTimeString().split(' ')[0]; + + const connected_gsm_time = firstTankTime; + + // Step 3: Update Insensor const updateResult = await Insensors.findOneAndUpdate( { connected_to: hardwareId }, { $set: { - connected_gsm_time, - connected_gsm_date + connected_gsm_date, + connected_gsm_time } }, { new: true }