diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 08fb9ca9..098cb6d2 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -2737,26 +2737,52 @@ exports.raiseATicket = async (req, reply) => { const issuesToAdd = []; // ✅ Only raise a ticket if master is disconnected + // if (masterConnectedStatus === "disconnected") { + // const existingMasterTicket = await Support.findOne({ + // "issues.hardwareId": connected_to, + // "issues.type": "GSM or LoRa Disconnected" + // }); + + // if (!existingMasterTicket) { + // const slaveHardwareIds = disconnectedSlaves.map(s => s.slaveHardwareId); + // const slaveNames = disconnectedSlaves.map(s => s.slaveName); + + // issuesToAdd.push({ + // type: "GSM or LoRa Disconnected", + // masterHardwareId: connected_to, + // hardwareId: connected_to, // Master hardwareId + // hardwareIds: slaveHardwareIds, // Slave tankHardwareIds + // slaveNames, + // message: `Master ${connected_to} is disconnected along with ${slaveHardwareIds.length} slave(s)` + // }); + // } + // } + if (masterConnectedStatus === "disconnected") { const existingMasterTicket = await Support.findOne({ "issues.hardwareId": connected_to, "issues.type": "GSM or LoRa Disconnected" }); - + if (!existingMasterTicket) { const slaveHardwareIds = disconnectedSlaves.map(s => s.slaveHardwareId); const slaveNames = disconnectedSlaves.map(s => s.slaveName); - - issuesToAdd.push({ - type: "GSM or LoRa Disconnected", - masterHardwareId: connected_to, - hardwareId: connected_to, // Master hardwareId - hardwareIds: slaveHardwareIds, // Slave tankHardwareIds - slaveNames, - message: `Master ${connected_to} is disconnected along with ${slaveHardwareIds.length} slave(s)` - }); + + // Check if disconnection is at least 15 minutes old + if (diffInMinutes >= 15) { + issuesToAdd.push({ + type: "GSM or LoRa Disconnected", + masterHardwareId: connected_to, + hardwareId: connected_to, + hardwareIds: slaveHardwareIds, + slaveNames, + message: `Master ${connected_to} is disconnected along with ${slaveHardwareIds.length} slave(s)`, + disconnectedAt: lastDataTime // optional: for future tracking + }); + } } } + if (issuesToAdd.length > 0) { const supportRecord = await Support.findOne({ supportId: "AWHYSU64" }); diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index c9937275..f838858b 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2592,23 +2592,33 @@ exports.getOrdersByInstallationId = async (req, reply) => { }); } - // Fetch customer details & allocated sensors for each order + const uniqueCustomersMap = new Map(); + + // Build unique customerId-based map + for (const order of orders) { + if (!uniqueCustomersMap.has(order.customerId)) { + uniqueCustomersMap.set(order.customerId, order); + } + } + + // Only keep one order per customerId + const uniqueOrders = Array.from(uniqueCustomersMap.values()); + + // Enrich with customer and sensor info const ordersWithDetails = await Promise.all( - orders.map(async (order) => { - // Fetch customer details + uniqueOrders.map(async (order) => { const customer = await User.findOne({ customerId: order.customerId }).lean(); - // Fetch allocated sensors for this customer const allocatedSensors = await Insensors.find({ - storeId: order.storeId, - customerId: order.customerId, // Match only sensors allocated to this customer - status: "blocked", // Only fetch sensors that are allocated (blocked) + storeId: order.storeId, + customerId: order.customerId, + status: "blocked", }).lean(); return { ...order.toObject(), - customer: customer || null, // Include customer details or null if not found - allocated_sensors: allocatedSensors, // List of allocated sensors + customer: customer || null, + allocated_sensors: allocatedSensors, }; }) ); @@ -2618,6 +2628,7 @@ exports.getOrdersByInstallationId = async (req, reply) => { message: "Orders fetched successfully", data: ordersWithDetails, }); + } catch (err) { console.error("Error fetching orders:", err); return reply.status(500).send({ error: "Internal server error" }); @@ -2627,6 +2638,7 @@ exports.getOrdersByInstallationId = async (req, reply) => { + exports.getallocatedsensorstouser= async (req, reply) => { try { const { customerId } = req.params;