|  |  |  | @ -2711,4 +2711,102 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |     return reply.code(500).send({ error: "Internal server error" }); | 
			
		
	
		
			
				
					|  |  |  |  |   } | 
			
		
	
		
			
				
					|  |  |  |  | }; | 
			
		
	
		
			
				
					|  |  |  |  | exports.getDisconnectedCustomerDetails = async (req, reply) => { | 
			
		
	
		
			
				
					|  |  |  |  |   try { | 
			
		
	
		
			
				
					|  |  |  |  |     const { supportId } = req.params; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (!supportId) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(400).send({ error: "supportId is required" }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // 1. Get the support record by supportId
 | 
			
		
	
		
			
				
					|  |  |  |  |     const supportRecord = await Support.findOne({ supportId }).lean(); | 
			
		
	
		
			
				
					|  |  |  |  |     if (!supportRecord) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(404).send({ message: "No support record found for this supportId" }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // 2. Extract all hardwareIds from issues
 | 
			
		
	
		
			
				
					|  |  |  |  |     const hardwareIds = []; | 
			
		
	
		
			
				
					|  |  |  |  |     for (const issue of supportRecord.issues) { | 
			
		
	
		
			
				
					|  |  |  |  |       if (issue.hardwareId) hardwareIds.push(issue.hardwareId); | 
			
		
	
		
			
				
					|  |  |  |  |       if (Array.isArray(issue.hardwareIds)) hardwareIds.push(...issue.hardwareIds); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (hardwareIds.length === 0) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(404).send({ message: "No hardware IDs found in issues" }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // 3. Find disconnected Insensors (matching hardwareId or connected_to)
 | 
			
		
	
		
			
				
					|  |  |  |  |     const disconnectedSensors = await Insensors.find({ | 
			
		
	
		
			
				
					|  |  |  |  |       $or: [ | 
			
		
	
		
			
				
					|  |  |  |  |         { hardwareId: { $in: hardwareIds } }, | 
			
		
	
		
			
				
					|  |  |  |  |         { connected_to: { $in: hardwareIds } } | 
			
		
	
		
			
				
					|  |  |  |  |       ], | 
			
		
	
		
			
				
					|  |  |  |  |       connected_status: "disconnected" | 
			
		
	
		
			
				
					|  |  |  |  |     }).lean(); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     if (disconnectedSensors.length === 0) { | 
			
		
	
		
			
				
					|  |  |  |  |       return reply.code(404).send({ message: "No disconnected issues found" }); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // 4. Get all unique customerIds
 | 
			
		
	
		
			
				
					|  |  |  |  |     const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))]; | 
			
		
	
		
			
				
					|  |  |  |  |     const customers = await User.find({ customerId: { $in: customerIds } }).lean(); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     // 5. Build enriched response
 | 
			
		
	
		
			
				
					|  |  |  |  |     const response = disconnectedSensors.map(sensor => { | 
			
		
	
		
			
				
					|  |  |  |  |       const user = customers.find(u => u.customerId === sensor.customerId); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |       return { | 
			
		
	
		
			
				
					|  |  |  |  |         hardwareId: sensor.hardwareId, | 
			
		
	
		
			
				
					|  |  |  |  |         masterName: sensor.masterName, | 
			
		
	
		
			
				
					|  |  |  |  |         location: sensor.location, | 
			
		
	
		
			
				
					|  |  |  |  |         disconnectedAt: sensor.disconnectedAt || new Date().toISOString(), | 
			
		
	
		
			
				
					|  |  |  |  |         customer: user ? { | 
			
		
	
		
			
				
					|  |  |  |  |           customerId: user.customerId, | 
			
		
	
		
			
				
					|  |  |  |  |           username: user.username || "", | 
			
		
	
		
			
				
					|  |  |  |  |           firstName: user.profile?.firstName || "", | 
			
		
	
		
			
				
					|  |  |  |  |           lastName: user.profile?.lastName || "", | 
			
		
	
		
			
				
					|  |  |  |  |           phone: user.phone || user.profile?.contactNumber || "", | 
			
		
	
		
			
				
					|  |  |  |  |           email: user.emails?.[0]?.email || "", | 
			
		
	
		
			
				
					|  |  |  |  |           phoneVerified: user.phoneVerified || false, | 
			
		
	
		
			
				
					|  |  |  |  |           address1: user.profile?.address1 || "", | 
			
		
	
		
			
				
					|  |  |  |  |           address2: user.profile?.address2 || "", | 
			
		
	
		
			
				
					|  |  |  |  |           city: user.profile?.city || "", | 
			
		
	
		
			
				
					|  |  |  |  |           state: user.profile?.state || "", | 
			
		
	
		
			
				
					|  |  |  |  |           country: user.profile?.country || "", | 
			
		
	
		
			
				
					|  |  |  |  |           zip: user.profile?.zip || "", | 
			
		
	
		
			
				
					|  |  |  |  |           notes: user.profile?.notes || "", | 
			
		
	
		
			
				
					|  |  |  |  |           latitude: user.latitude, | 
			
		
	
		
			
				
					|  |  |  |  |           longitude: user.longitude, | 
			
		
	
		
			
				
					|  |  |  |  |           fcmIds: user.fcmIds || [], | 
			
		
	
		
			
				
					|  |  |  |  |           installationId: user.installationId || "", | 
			
		
	
		
			
				
					|  |  |  |  |           notificationPreferences: { | 
			
		
	
		
			
				
					|  |  |  |  |             allowNotifications: user.allowNotifications || false, | 
			
		
	
		
			
				
					|  |  |  |  |             automaticStartAndStopNotify: user.automaticStartAndStopNotify || false, | 
			
		
	
		
			
				
					|  |  |  |  |             manualStartAndStopNotify: user.manualStartAndStopNotify || false, | 
			
		
	
		
			
				
					|  |  |  |  |             criticalLowWaterAlert: user.criticalLowWaterAlert || false, | 
			
		
	
		
			
				
					|  |  |  |  |             lowWaterAlert: user.lowWaterAlert || false, | 
			
		
	
		
			
				
					|  |  |  |  |             notificationPreference: user.notificationPreference || "never" | 
			
		
	
		
			
				
					|  |  |  |  |           }, | 
			
		
	
		
			
				
					|  |  |  |  |           surveyStatus: user.survey_status || "pending", | 
			
		
	
		
			
				
					|  |  |  |  |           stripePaymentStatus: user.stripePaymentStatus || false, | 
			
		
	
		
			
				
					|  |  |  |  |           stripeSubscriptionStatus: user.stripeSubscriptionStatus || false, | 
			
		
	
		
			
				
					|  |  |  |  |           createdAt: user.createdAt, | 
			
		
	
		
			
				
					|  |  |  |  |           updatedAt: user.updatedAt | 
			
		
	
		
			
				
					|  |  |  |  |         } : null | 
			
		
	
		
			
				
					|  |  |  |  |       }; | 
			
		
	
		
			
				
					|  |  |  |  |     }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     return reply.send({ | 
			
		
	
		
			
				
					|  |  |  |  |       status_code: 200, | 
			
		
	
		
			
				
					|  |  |  |  |       disconnectedIssues: response | 
			
		
	
		
			
				
					|  |  |  |  |     }); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |   } catch (error) { | 
			
		
	
		
			
				
					|  |  |  |  |     console.error("Error fetching disconnected customer details:", error); | 
			
		
	
		
			
				
					|  |  |  |  |     return reply.code(500).send({ error: "Internal server error" }); | 
			
		
	
		
			
				
					|  |  |  |  |   } | 
			
		
	
		
			
				
					|  |  |  |  | }; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
	
		
			
				
					|  |  |  | 
 |