From 636df24892528c641541a501890475171e73363f Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 5 Jun 2025 13:39:59 +0530 Subject: [PATCH] movedAt added and gsm and lora check in particular category --- src/controllers/installationController.js | 387 ++++++++++++++++------ 1 file changed, 277 insertions(+), 110 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index aa57becf..6bbe6419 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -7640,6 +7640,224 @@ exports.moveIssueToCategory = async (req, reply) => { }; +// exports.particularCategory = async (req, reply) => { +// try { +// const { supportId, category } = req.params; +// const { customerId: queryCustomerId } = req.query; + +// if (!supportId || !category) { +// return reply.code(400).send({ error: "supportId and category are required" }); +// } + +// const support = await Support.findOne({ supportId }).lean(); +// if (!support) { +// return reply.code(404).send({ message: "Support record not found" }); +// } +// console.log("support",support) +// // Choose the appropriate array based on category +// let issues = []; +// if (category === "Resolved") { +// issues = (support.resolvedIssues || []).filter(issue => issue.category === "Resolved"); +// } else { +// issues = (support.categorizedIssues || []).filter(issue => issue.category === category); +// } +// console.log("issues",issues) +// if (issues.length === 0) { +// return reply.code(404).send({ message: `No issues found for category: ${category}` }); +// } + +// const hardwareIds = [...new Set(issues.map(issue => issue.hardwareId).filter(Boolean))]; + +// if (hardwareIds.length === 0) { +// return reply.code(404).send({ message: "No hardware IDs found for these issues" }); +// } + +// let customerId = queryCustomerId; + +// if (!customerId) { +// const sensors = await Insensors.find({ hardwareId: { $in: hardwareIds } }).lean(); +// if (!sensors.length) { +// return reply.code(404).send({ message: "No sensors found matching these hardware IDs" }); +// } +// customerId = sensors[0].customerId; +// if (!customerId) { +// return reply.code(404).send({ message: "Customer ID not found for these sensors" }); +// } +// } + +// const allRelatedSensors = await Insensors.find({ +// customerId, +// hardwareId: { $in: hardwareIds } +// }).lean(); + +// if (!allRelatedSensors.length) { +// return reply.code(404).send({ message: "No sensors found for the provided customer and hardware IDs" }); +// } + +// const orders = await Order.find({ customerId }).lean(); +// const orderMap = {}; +// orders.forEach(order => { +// order.master_connections.forEach(conn => { +// orderMap[conn.hardwareId] = { +// masterName: conn.master_name || null, +// location: conn.location || null +// }; +// }); +// }); + +// const disconnectedIssues = []; + +// // for (const master of allRelatedSensors.filter(i => i.type === "master")) { +// const allMasters = allRelatedSensors.filter(i => i.type === "master"); + +// for (const master of allMasters) { +// const slaves = await Insensors.find({ +// connected_to: master.hardwareId, +// customerId +// }).lean(); + +// const slaveDetails = await Promise.all(slaves.map(async (slave) => { +// const slaveHardwareId = slave.tankhardwareId; + +// const tankInfo = await Tank.findOne({ +// $or: [ +// { hardwareId: slaveHardwareId }, +// { tankhardwareId: slaveHardwareId } +// ] +// }).lean(); + +// const slaveComments = (support.comments || []) +// .filter(comment => comment.hardwareId === slave.hardwareId) +// .map(c => c.text); + +// return { +// hardwareId: slave.tankhardwareId, +// tankName: slave.tankName || "", +// location: slave.tankLocation || "", +// connected_status: slave.connected_status, +// connected_lora_time: slave.connected_lora_time || "", +// connected_lora_date: slave.connected_lora_date || "", +// lora_last_check_time: slave.lora_last_check_time || null, +// lora_last_disconnect_time: slave.lora_last_disconnect_time || null, +// connected_to: slave.connected_to || "", +// masterName: orderMap[master.hardwareId]?.masterName || "", +// type: "slave", +// typeOfWater: tankInfo?.typeOfWater || "", +// support_lora_last_check_time: slave.support_lora_last_check_time || null, +// team_member_support_lora_last_check_time: slave.team_member_support_lora_last_check_time || null, +// comments: slaveComments +// }; +// })); + +// const masterComments = (support.comments || []) +// .filter(comment => comment.hardwareId === master.hardwareId) +// .map(c => c.text); + +// disconnectedIssues.push({ +// hardwareId: master.hardwareId, +// masterName: orderMap[master.hardwareId]?.masterName || "", +// location: orderMap[master.hardwareId]?.location || "", +// type: "master", +// connected_status: master.connected_status, +// gsm_last_check_time: master.gsm_last_check_time || null, +// gsm_last_disconnect_time: master.gsm_last_disconnect_time || null, +// connected_gsm_date: master.connected_gsm_date || "", +// connected_gsm_time: master.connected_gsm_time || "", +// connected_lora_date: master.connected_lora_date || "", +// connected_lora_time: master.connected_lora_time || "", +// support_gsm_last_check_time: master.support_gsm_last_check_time || null, +// team_member_support_gsm_last_check_time: master.team_member_support_gsm_last_check_time || null, +// connected_slave_count: slaveDetails.length, +// connected_slaves: slaveDetails, +// comments: masterComments +// }); +// } + +// return reply.send({ +// status_code: 200, +// supportId, +// customerId, +// totalMasters: disconnectedIssues.length, +// disconnectedIssues +// }); + +// } catch (err) { +// console.error("Error in particularCategory:", err); +// return reply.code(500).send({ error: "Internal server error" }); +// } +// }; + + + + + +// exports.assignCategorizeIssue = async (request, reply) => { +// const { supportId } = request.params; +// const { support_teamMemberId, startDate, endDate, category, masterHardwareId } = request.body; + +// if (!support_teamMemberId || !startDate || !endDate || !category || !masterHardwareId) { +// return reply.code(400).send({ error: 'support_teamMemberId, startDate, endDate, category, and masterHardwareId are required' }); +// } + +// const support = await Support.findOne({ supportId }); +// if (!support) { +// return reply.code(404).send({ error: 'Support record not found' }); +// } + +// const teamMembers = support.team_member?.team_member || []; +// const teamMember = teamMembers.find(m => m.support_teamMemberId === support_teamMemberId); + +// if (!teamMember) { +// return reply.code(400).send({ error: `Team member ID ${support_teamMemberId} not found` }); +// } + +// const start = new Date(startDate); +// const end = new Date(endDate); +// if (isNaN(start) || isNaN(end)) { +// return reply.code(400).send({ error: 'Invalid startDate or endDate' }); +// } + +// // ✅ Format to "DD-MM-YYYY HH:mm:ss" +// const formattedStart = moment(start).format("DD-MM-YYYY HH:mm:ss"); +// const formattedEnd = moment(end).format("DD-MM-YYYY HH:mm:ss"); + +// let assignedCount = 0; + +// support.categorizedIssues.forEach(issue => { +// if ( +// issue.masterHardwareId === masterHardwareId && +// issue.category === category +// ) { +// issue.assignedTo = { +// name: teamMember.name, +// support_teamMemberId: teamMember.support_teamMemberId, +// phone: teamMember.phone, +// email: teamMember.email, +// startDate: formattedStart, // 👈 use formatted date +// endDate: formattedEnd // 👈 use formatted date +// }; +// assignedCount++; +// } +// }); + +// if (assignedCount === 0) { +// return reply.code(404).send({ message: 'No matching issues found for assignment' }); +// } + +// await support.save(); + +// return reply.send({ +// message: `Assigned ${assignedCount} categorized issue(s) to ${teamMember.name}`, +// assignedTo: teamMember.support_teamMemberId +// }); +// }; + + + +// const moment = require('moment'); // Ensure moment is installed + + + exports.particularCategory = async (req, reply) => { try { const { supportId, category } = req.params; @@ -7650,39 +7868,21 @@ exports.particularCategory = async (req, reply) => { } const support = await Support.findOne({ supportId }).lean(); - if (!support) { - return reply.code(404).send({ message: "Support record not found" }); - } - console.log("support",support) - // Choose the appropriate array based on category - let issues = []; - if (category === "Resolved") { - issues = (support.resolvedIssues || []).filter(issue => issue.category === "Resolved"); - } else { - issues = (support.categorizedIssues || []).filter(issue => issue.category === category); - } - console.log("issues",issues) - if (issues.length === 0) { - return reply.code(404).send({ message: `No issues found for category: ${category}` }); - } + if (!support) return reply.code(404).send({ message: "Support record not found" }); - const hardwareIds = [...new Set(issues.map(issue => issue.hardwareId).filter(Boolean))]; + // Filter issues by category + const issues = (category === "Resolved" ? support.resolvedIssues : support.categorizedIssues || []) + .filter(issue => issue.category === category); + if (!issues.length) return reply.code(404).send({ message: `No issues found for category: ${category}` }); - if (hardwareIds.length === 0) { - return reply.code(404).send({ message: "No hardware IDs found for these issues" }); - } + const hardwareIds = [...new Set(issues.map(issue => issue.hardwareId).filter(Boolean))]; + if (!hardwareIds.length) return reply.code(404).send({ message: "No hardware IDs found for these issues" }); let customerId = queryCustomerId; - if (!customerId) { - const sensors = await Insensors.find({ hardwareId: { $in: hardwareIds } }).lean(); - if (!sensors.length) { - return reply.code(404).send({ message: "No sensors found matching these hardware IDs" }); - } - customerId = sensors[0].customerId; - if (!customerId) { - return reply.code(404).send({ message: "Customer ID not found for these sensors" }); - } + const sensorDoc = await Insensors.findOne({ hardwareId: { $in: hardwareIds } }).lean(); + if (!sensorDoc || !sensorDoc.customerId) return reply.code(404).send({ message: "Customer ID not found" }); + customerId = sensorDoc.customerId; } const allRelatedSensors = await Insensors.find({ @@ -7690,10 +7890,9 @@ exports.particularCategory = async (req, reply) => { hardwareId: { $in: hardwareIds } }).lean(); - if (!allRelatedSensors.length) { - return reply.code(404).send({ message: "No sensors found for the provided customer and hardware IDs" }); - } + if (!allRelatedSensors.length) return reply.code(404).send({ message: "No sensors found" }); + // Order map const orders = await Order.find({ customerId }).lean(); const orderMap = {}; orders.forEach(order => { @@ -7705,19 +7904,48 @@ exports.particularCategory = async (req, reply) => { }); }); + // Create map for issue -> movedAt/resolvedAt + const issueMap = {}; + issues.forEach(issue => { + issueMap[issue.hardwareId] = issue; + }); + const disconnectedIssues = []; - // for (const master of allRelatedSensors.filter(i => i.type === "master")) { - const allMasters = allRelatedSensors.filter(i => i.type === "master"); + const allMasters = allRelatedSensors.filter(i => i.type === "master"); - for (const master of allMasters) { - const slaves = await Insensors.find({ - connected_to: master.hardwareId, - customerId - }).lean(); + for (const master of allMasters) { + const slaves = await Insensors.find({ connected_to: master.hardwareId, customerId }).lean(); + + // Get latest IoT data for master + const latestIotData = await IotData.findOne({ hardwareId: master.hardwareId }).sort({ date: -1 }).lean(); + // GSM Status + const now = moment.tz("Asia/Kolkata"); + let gsm_last_check_time = null; + let gsmConnected = false; + + if (latestIotData?.date) { + const gsmTime = moment.tz(latestIotData.date, "Asia/Kolkata"); + const gsmDiff = now.diff(gsmTime, "minutes"); + gsmConnected = gsmDiff <= 1; + gsm_last_check_time = latestIotData.date; + } + + // Prepare slave details with LORA check const slaveDetails = await Promise.all(slaves.map(async (slave) => { - const slaveHardwareId = slave.tankhardwareId; + const slaveHardwareId = slave.tankhardwareId?.trim(); + const matchedTank = latestIotData?.tanks?.find(tank => tank.tankhardwareId === slaveHardwareId); + + let loraConnected = false; + let lora_last_check_time = null; + + if (matchedTank?.date && matchedTank?.tankHeight !== "0") { + const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata"); + const loraDiff = now.diff(tankTime, "minutes"); + loraConnected = loraDiff <= 1; + lora_last_check_time = matchedTank.date; + } const tankInfo = await Tank.findOne({ $or: [ @@ -7735,9 +7963,10 @@ exports.particularCategory = async (req, reply) => { tankName: slave.tankName || "", location: slave.tankLocation || "", connected_status: slave.connected_status, - connected_lora_time: slave.connected_lora_time || "", connected_lora_date: slave.connected_lora_date || "", - lora_last_check_time: slave.lora_last_check_time || null, + connected_lora_time: slave.connected_lora_time || "", + loraConnected, + lora_last_check_time, lora_last_disconnect_time: slave.lora_last_disconnect_time || null, connected_to: slave.connected_to || "", masterName: orderMap[master.hardwareId]?.masterName || "", @@ -7753,13 +7982,16 @@ exports.particularCategory = async (req, reply) => { .filter(comment => comment.hardwareId === master.hardwareId) .map(c => c.text); + const issue = issueMap[master.hardwareId]; + disconnectedIssues.push({ hardwareId: master.hardwareId, masterName: orderMap[master.hardwareId]?.masterName || "", location: orderMap[master.hardwareId]?.location || "", type: "master", connected_status: master.connected_status, - gsm_last_check_time: master.gsm_last_check_time || null, + gsmConnected, + gsm_last_check_time, gsm_last_disconnect_time: master.gsm_last_disconnect_time || null, connected_gsm_date: master.connected_gsm_date || "", connected_gsm_time: master.connected_gsm_time || "", @@ -7769,7 +8001,9 @@ exports.particularCategory = async (req, reply) => { team_member_support_gsm_last_check_time: master.team_member_support_gsm_last_check_time || null, connected_slave_count: slaveDetails.length, connected_slaves: slaveDetails, - comments: masterComments + comments: masterComments, + movedAt: category !== "Resolved" ? (issue?.movedAt || null) : null, + resolvedAt: category === "Resolved" ? (issue?.resolvedAt || null) : null }); } @@ -7789,73 +8023,6 @@ exports.particularCategory = async (req, reply) => { - - -// exports.assignCategorizeIssue = async (request, reply) => { -// const { supportId } = request.params; -// const { support_teamMemberId, startDate, endDate, category, masterHardwareId } = request.body; - -// if (!support_teamMemberId || !startDate || !endDate || !category || !masterHardwareId) { -// return reply.code(400).send({ error: 'support_teamMemberId, startDate, endDate, category, and masterHardwareId are required' }); -// } - -// const support = await Support.findOne({ supportId }); -// if (!support) { -// return reply.code(404).send({ error: 'Support record not found' }); -// } - -// const teamMembers = support.team_member?.team_member || []; -// const teamMember = teamMembers.find(m => m.support_teamMemberId === support_teamMemberId); - -// if (!teamMember) { -// return reply.code(400).send({ error: `Team member ID ${support_teamMemberId} not found` }); -// } - -// const start = new Date(startDate); -// const end = new Date(endDate); -// if (isNaN(start) || isNaN(end)) { -// return reply.code(400).send({ error: 'Invalid startDate or endDate' }); -// } - -// // ✅ Format to "DD-MM-YYYY HH:mm:ss" -// const formattedStart = moment(start).format("DD-MM-YYYY HH:mm:ss"); -// const formattedEnd = moment(end).format("DD-MM-YYYY HH:mm:ss"); - -// let assignedCount = 0; - -// support.categorizedIssues.forEach(issue => { -// if ( -// issue.masterHardwareId === masterHardwareId && -// issue.category === category -// ) { -// issue.assignedTo = { -// name: teamMember.name, -// support_teamMemberId: teamMember.support_teamMemberId, -// phone: teamMember.phone, -// email: teamMember.email, -// startDate: formattedStart, // 👈 use formatted date -// endDate: formattedEnd // 👈 use formatted date -// }; -// assignedCount++; -// } -// }); - -// if (assignedCount === 0) { -// return reply.code(404).send({ message: 'No matching issues found for assignment' }); -// } - -// await support.save(); - -// return reply.send({ -// message: `Assigned ${assignedCount} categorized issue(s) to ${teamMember.name}`, -// assignedTo: teamMember.support_teamMemberId -// }); -// }; - - - -// const moment = require('moment'); // Ensure moment is installed - exports.assignCategorizeIssue = async (request, reply) => { const { supportId } = request.params; const { support_teamMemberId, startDate, endDate, category, masterHardwareId } = request.body;