diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 383ac2f5..a588f959 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -4551,7 +4551,6 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { } }; - exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => { try { const { support_teamMemberId } = req.params; @@ -4560,94 +4559,76 @@ exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => { return reply.code(400).send({ error: "support_teamMemberId is required" }); } - // 1. Find support document that includes the given team member ID + // Step 1: Get support record with categorized issues assigned to the team member const supportRecord = await Support.findOne({ "team_member.team_member.support_teamMemberId": support_teamMemberId }).lean(); if (!supportRecord) { - return reply.code(404).send({ message: "No support record found for this team member ID" }); + return reply.code(404).send({ message: "Support record not found" }); } - // 2. Extract all hardwareIds from issues - // 2. Extract all hardwareIds and masterHardwareIds 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 (issue.masterHardwareId) hardwareIds.push(issue.masterHardwareId); // Add this line -} + // Step 2: Filter categorized issues assigned to this team member + const assignedIssues = (supportRecord.categorizedIssues || []).filter( + issue => issue.assignedTo?.support_teamMemberId === support_teamMemberId + ); + // Step 3: Extract unique hardwareIds from assigned issues + const assignedHardwareIds = [ + ...new Set( + assignedIssues.map(issue => issue.hardwareId || issue.masterHardwareId).filter(Boolean) + ) + ]; - if (hardwareIds.length === 0) { - return reply.code(404).send({ message: "No hardware IDs found in issues" }); + if (assignedHardwareIds.length === 0) { + return reply.code(404).send({ message: "No categorized issues assigned to this team member" }); } - console.log("hardwareIds",hardwareIds) - // 3. Find disconnected Insensors matching connected_to - const uniqueHardwareIds = [...new Set(hardwareIds)]; - const disconnectedSensors = await Insensors.find({ - connected_to: { $in: uniqueHardwareIds }, + // Step 4: Find disconnected insensors (either masters or slaves) + const disconnectedDevices = await Insensors.find({ + $or: [ + { hardwareId: { $in: assignedHardwareIds } }, + { connected_to: { $in: assignedHardwareIds } } + ], connected_status: "disconnected" }).lean(); - if (disconnectedSensors.length === 0) { - return reply.code(404).send({ message: "No disconnected issues found" }); + if (disconnectedDevices.length === 0) { + return reply.code(404).send({ message: "No disconnected devices assigned to this team member" }); } - // 4. Get unique customerIds - const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))]; - - // 5. Get User data - const customers = await User.find({ customerId: { $in: customerIds } }).lean(); - - // 6. Format response - const uniqueCustomerMap = {}; - for (const user of customers) { - if (!uniqueCustomerMap[user.customerId]) { - uniqueCustomerMap[user.customerId] = { - customer: { - 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 || []).filter(fcm => typeof fcm === "string" && fcm.startsWith("d")), - 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", - buildingName: user.buildingName, - stripePaymentStatus: user.stripePaymentStatus || false, - stripeSubscriptionStatus: user.stripeSubscriptionStatus || false, - createdAt: user.createdAt, - updatedAt: user.updatedAt - } - }; - } - } + // Step 5: Extract unique customerIds + const customerIds = [...new Set(disconnectedDevices.map(d => d.customerId))]; + + const users = await User.find({ customerId: { $in: customerIds } }).lean(); + + // Step 6: Prepare final response + const response = users.map(user => ({ + customerId: user.customerId, + firstName: user.profile?.firstName || "", + lastName: user.profile?.lastName || "", + phone: user.phone || user.profile?.contactNumber || "", + email: user.emails?.[0]?.email || "", + city: user.profile?.city || "", + state: user.profile?.state || "", + country: user.profile?.country || "", + latitude: user.latitude, + longitude: user.longitude, + fcmIds: (user.fcmIds || []).filter(fcm => typeof fcm === "string"), + 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" + }, + createdAt: user.createdAt, + updatedAt: user.updatedAt + })); - return reply.send({ - status_code: 200, - data: Object.values(uniqueCustomerMap) - }); + return reply.send({ status_code: 200, data: response }); } catch (error) { console.error("Error in getDisconnectedCustomerDetailsByTeamMemberId:", error);