|  |  |  | @ -5141,12 +5141,111 @@ exports.moveIssueToCategory = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  | //   }
 | 
			
		
	
		
			
				
					|  |  |  |  | // };
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | // exports.particularCategory = async (req, reply) => {
 | 
			
		
	
		
			
				
					|  |  |  |  | //   try {
 | 
			
		
	
		
			
				
					|  |  |  |  | //     const { supportId, category } = req.params;
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //     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" });
 | 
			
		
	
		
			
				
					|  |  |  |  | //     }
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //     const issues = (support.categorizedIssues || []).filter(issue => issue.category === category);
 | 
			
		
	
		
			
				
					|  |  |  |  | //     if (issues.length === 0) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //       return reply.code(404).send({ message: `No issues found for category: ${category}` });
 | 
			
		
	
		
			
				
					|  |  |  |  | //     }
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //     const hardwareIds = issues.map(issue => issue.hardwareId).filter(Boolean);
 | 
			
		
	
		
			
				
					|  |  |  |  | //     const insensors = await Insensors.find({
 | 
			
		
	
		
			
				
					|  |  |  |  | //       hardwareId: { $in: hardwareIds },
 | 
			
		
	
		
			
				
					|  |  |  |  | //       connected_status: "disconnected"
 | 
			
		
	
		
			
				
					|  |  |  |  | //     }).lean();
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //     if (!insensors.length) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //       return reply.code(404).send({ message: "No disconnected devices found for this category." });
 | 
			
		
	
		
			
				
					|  |  |  |  | //     }
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //     const orderMap = {};
 | 
			
		
	
		
			
				
					|  |  |  |  | //     const customerId = insensors[0]?.customerId;
 | 
			
		
	
		
			
				
					|  |  |  |  | //     if (customerId) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //       const orders = await Order.find({ customerId }).lean();
 | 
			
		
	
		
			
				
					|  |  |  |  | //       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 insensors.filter(i => i.type === "master")) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //       const slaves = await Insensors.find({
 | 
			
		
	
		
			
				
					|  |  |  |  | //         connected_to: master.hardwareId,
 | 
			
		
	
		
			
				
					|  |  |  |  | //         connected_status: "disconnected"
 | 
			
		
	
		
			
				
					|  |  |  |  | //       }).lean();
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //       const slaveDetails = slaves.map(slave => {
 | 
			
		
	
		
			
				
					|  |  |  |  | //         const slaveIssue = issues.find(i => i.hardwareId === slave.hardwareId);
 | 
			
		
	
		
			
				
					|  |  |  |  | //         return {
 | 
			
		
	
		
			
				
					|  |  |  |  | //           hardwareId: slave.hardwareId,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           tankName: slave.tankName || "",
 | 
			
		
	
		
			
				
					|  |  |  |  | //           location: slave.tankLocation || "",
 | 
			
		
	
		
			
				
					|  |  |  |  | //           connected_status: slave.connected_status,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           lora_last_disconnect_time: slave.lora_last_disconnect_time || null,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           connected_to: slave.connected_to || "",
 | 
			
		
	
		
			
				
					|  |  |  |  | //           masterName: orderMap[master.hardwareId]?.masterName || "",
 | 
			
		
	
		
			
				
					|  |  |  |  | //           type: "slave",
 | 
			
		
	
		
			
				
					|  |  |  |  | //           typeOfWater: slave.typeOfWater || "",
 | 
			
		
	
		
			
				
					|  |  |  |  | //           support_lora_last_check_time: null,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           category,
 | 
			
		
	
		
			
				
					|  |  |  |  | //           assignedTo: slaveIssue?.assignedTo || null // <-- Include assigned details here
 | 
			
		
	
		
			
				
					|  |  |  |  | //         };
 | 
			
		
	
		
			
				
					|  |  |  |  | //       });
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //       const masterIssue = issues.find(i => i.hardwareId === 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_disconnect_time: master.gsm_last_disconnect_time || null,
 | 
			
		
	
		
			
				
					|  |  |  |  | //         support_gsm_last_check_time: null,
 | 
			
		
	
		
			
				
					|  |  |  |  | //         connected_slave_count: slaveDetails.length,
 | 
			
		
	
		
			
				
					|  |  |  |  | //         connected_slaves: slaveDetails,
 | 
			
		
	
		
			
				
					|  |  |  |  | //         category,
 | 
			
		
	
		
			
				
					|  |  |  |  | //         assignedTo: masterIssue?.assignedTo || null // <-- Include assigned details here
 | 
			
		
	
		
			
				
					|  |  |  |  | //       });
 | 
			
		
	
		
			
				
					|  |  |  |  | //     }
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //     return reply.send({
 | 
			
		
	
		
			
				
					|  |  |  |  | //       status_code: 200,
 | 
			
		
	
		
			
				
					|  |  |  |  | //       supportId,
 | 
			
		
	
		
			
				
					|  |  |  |  | //       totalMasters: disconnectedIssues.length,
 | 
			
		
	
		
			
				
					|  |  |  |  | //       disconnectedIssues
 | 
			
		
	
		
			
				
					|  |  |  |  | //     });
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | //   } catch (err) {
 | 
			
		
	
		
			
				
					|  |  |  |  | //     console.error("Error in particularCategory:", err);
 | 
			
		
	
		
			
				
					|  |  |  |  | //     return reply.code(500).send({ error: "Internal server error" });
 | 
			
		
	
		
			
				
					|  |  |  |  | //   }
 | 
			
		
	
		
			
				
					|  |  |  |  | // };
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | exports.particularCategory = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |   try { | 
			
		
	
		
			
				
					|  |  |  |  |     const { supportId, category } = req.params; | 
			
		
	
		
			
				
					|  |  |  |  |     const { customerId } = req.query; // ✅ from query string
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (!supportId || !category) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(400).send({ error: "supportId and category are required" }); | 
			
		
	
		
			
				
					|  |  |  |  |     if (!customerId || !supportId || !category) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(400).send({ error: "customerId, supportId, and category are required" }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     const support = await Support.findOne({ supportId }).lean(); | 
			
		
	
	
		
			
				
					|  |  |  | @ -5154,13 +5253,18 @@ exports.particularCategory = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(404).send({ message: "Support record not found" }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     const issues = (support.categorizedIssues || []).filter(issue => issue.category === category); | 
			
		
	
		
			
				
					|  |  |  |  |     const issues = (support.categorizedIssues || []).filter( | 
			
		
	
		
			
				
					|  |  |  |  |       issue => issue.category === category | 
			
		
	
		
			
				
					|  |  |  |  |     ); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (issues.length === 0) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(404).send({ message: `No issues found for category: ${category}` }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     const hardwareIds = issues.map(issue => issue.hardwareId).filter(Boolean); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     const insensors = await Insensors.find({ | 
			
		
	
		
			
				
					|  |  |  |  |       customerId, | 
			
		
	
		
			
				
					|  |  |  |  |       hardwareId: { $in: hardwareIds }, | 
			
		
	
		
			
				
					|  |  |  |  |       connected_status: "disconnected" | 
			
		
	
		
			
				
					|  |  |  |  |     }).lean(); | 
			
		
	
	
		
			
				
					|  |  |  | @ -5170,23 +5274,21 @@ exports.particularCategory = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     const orderMap = {}; | 
			
		
	
		
			
				
					|  |  |  |  |     const customerId = insensors[0]?.customerId; | 
			
		
	
		
			
				
					|  |  |  |  |     if (customerId) { | 
			
		
	
		
			
				
					|  |  |  |  |       const orders = await Order.find({ customerId }).lean(); | 
			
		
	
		
			
				
					|  |  |  |  |       orders.forEach(order => { | 
			
		
	
		
			
				
					|  |  |  |  |         order.master_connections.forEach(conn => { | 
			
		
	
		
			
				
					|  |  |  |  |           orderMap[conn.hardwareId] = { | 
			
		
	
		
			
				
					|  |  |  |  |             masterName: conn.master_name || null, | 
			
		
	
		
			
				
					|  |  |  |  |             location: conn.location || null | 
			
		
	
		
			
				
					|  |  |  |  |           }; | 
			
		
	
		
			
				
					|  |  |  |  |         }); | 
			
		
	
		
			
				
					|  |  |  |  |     const orders = await Order.find({ customerId }).lean(); | 
			
		
	
		
			
				
					|  |  |  |  |     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 insensors.filter(i => i.type === "master")) { | 
			
		
	
		
			
				
					|  |  |  |  |       const slaves = await Insensors.find({ | 
			
		
	
		
			
				
					|  |  |  |  |         customerId, | 
			
		
	
		
			
				
					|  |  |  |  |         connected_to: master.hardwareId, | 
			
		
	
		
			
				
					|  |  |  |  |         connected_status: "disconnected" | 
			
		
	
		
			
				
					|  |  |  |  |       }).lean(); | 
			
		
	
	
		
			
				
					|  |  |  | @ -5199,13 +5301,14 @@ exports.particularCategory = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |           location: slave.tankLocation || "", | 
			
		
	
		
			
				
					|  |  |  |  |           connected_status: slave.connected_status, | 
			
		
	
		
			
				
					|  |  |  |  |           lora_last_disconnect_time: slave.lora_last_disconnect_time || null, | 
			
		
	
		
			
				
					|  |  |  |  |           team_member_support_lora_last_check_time: slave.team_member_support_lora_last_check_time, | 
			
		
	
		
			
				
					|  |  |  |  |           connected_to: slave.connected_to || "", | 
			
		
	
		
			
				
					|  |  |  |  |           masterName: orderMap[master.hardwareId]?.masterName || "", | 
			
		
	
		
			
				
					|  |  |  |  |           type: "slave", | 
			
		
	
		
			
				
					|  |  |  |  |           typeOfWater: slave.typeOfWater || "", | 
			
		
	
		
			
				
					|  |  |  |  |           support_lora_last_check_time: null, | 
			
		
	
		
			
				
					|  |  |  |  |           category, | 
			
		
	
		
			
				
					|  |  |  |  |           assignedTo: slaveIssue?.assignedTo || null // <-- Include assigned details here
 | 
			
		
	
		
			
				
					|  |  |  |  |           assignedTo: slaveIssue?.assignedTo || null | 
			
		
	
		
			
				
					|  |  |  |  |         }; | 
			
		
	
		
			
				
					|  |  |  |  |       }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
	
		
			
				
					|  |  |  | @ -5218,17 +5321,19 @@ exports.particularCategory = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |         type: "master", | 
			
		
	
		
			
				
					|  |  |  |  |         connected_status: master.connected_status, | 
			
		
	
		
			
				
					|  |  |  |  |         gsm_last_disconnect_time: master.gsm_last_disconnect_time || null, | 
			
		
	
		
			
				
					|  |  |  |  |         team_member_support_gsm_last_check_time: master.team_member_support_gsm_last_check_time, | 
			
		
	
		
			
				
					|  |  |  |  |         support_gsm_last_check_time: null, | 
			
		
	
		
			
				
					|  |  |  |  |         connected_slave_count: slaveDetails.length, | 
			
		
	
		
			
				
					|  |  |  |  |         connected_slaves: slaveDetails, | 
			
		
	
		
			
				
					|  |  |  |  |         category, | 
			
		
	
		
			
				
					|  |  |  |  |         assignedTo: masterIssue?.assignedTo || null // <-- Include assigned details here
 | 
			
		
	
		
			
				
					|  |  |  |  |         assignedTo: masterIssue?.assignedTo || null | 
			
		
	
		
			
				
					|  |  |  |  |       }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     return reply.send({ | 
			
		
	
		
			
				
					|  |  |  |  |       status_code: 200, | 
			
		
	
		
			
				
					|  |  |  |  |       supportId, | 
			
		
	
		
			
				
					|  |  |  |  |       customerId, | 
			
		
	
		
			
				
					|  |  |  |  |       totalMasters: disconnectedIssues.length, | 
			
		
	
		
			
				
					|  |  |  |  |       disconnectedIssues | 
			
		
	
		
			
				
					|  |  |  |  |     }); | 
			
		
	
	
		
			
				
					|  |  |  | @ -5238,6 +5343,8 @@ exports.particularCategory = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |     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; | 
			
		
	
	
		
			
				
					|  |  |  | 
 |