diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 427bd050..9b0a0914 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -6512,6 +6512,112 @@ exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => { }; +// exports.getDisconnectedMoveCustomerDetails = async (req, reply) => { +// try { +// const { supportId } = req.params; + +// if (!supportId) { +// return reply.code(400).send({ error: "supportId is required" }); +// } + +// // 1. Fetch support record +// const supportRecord = await Support.findOne({ supportId }).lean(); +// if (!supportRecord) { +// return reply.code(404).send({ message: "No support record found for this supportId" }); +// } + +// // 2. Only proceed if categorizedIssues exist +// if (!Array.isArray(supportRecord.categorizedIssues) || supportRecord.categorizedIssues.length === 0) { +// return reply.code(404).send({ message: "No categorized issues to process" }); +// } + +// // 3. Collect hardware IDs from categorized issues +// const categorizedHardwareIds = []; +// for (const issue of supportRecord.categorizedIssues) { +// if (issue.hardwareId) categorizedHardwareIds.push(issue.hardwareId); +// if (Array.isArray(issue.hardwareIds)) categorizedHardwareIds.push(...issue.hardwareIds); +// } + +// if (categorizedHardwareIds.length === 0) { +// return reply.code(404).send({ message: "No hardware IDs in categorized issues" }); +// } + +// // 4. Get disconnected sensors from Insensors +// const disconnectedSensors = await Insensors.find({ +// $or: [ +// { hardwareId: { $in: categorizedHardwareIds } }, +// { connected_to: { $in: categorizedHardwareIds } } +// ], +// connected_status: "disconnected" +// }).lean(); + +// if (disconnectedSensors.length === 0) { +// return reply.code(404).send({ message: "No disconnected sensors found" }); +// } + +// // 5. Get unique customerIds +// const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))]; + +// // 6. Fetch corresponding customers +// const customers = await User.find({ customerId: { $in: customerIds } }).lean(); + +// const uniqueCustomerMap = {}; + +// for (const user of customers) { +// const cid = user.customerId; +// if (!uniqueCustomerMap[cid]) { +// uniqueCustomerMap[cid] = { +// customer: { +// customerId: cid, +// username: user.username || "", +// firstName: user.profile?.firstName || user.firstName || "", +// lastName: user.profile?.lastName || user.lastName || "", +// phone: user.phone || user.profile?.contactNumber || user.alternativeNumber || "", +// email: user.emails?.[0]?.email || user.email || "", +// phoneVerified: user.phoneVerified || false, +// address1: user.profile?.address1 || user.address1 || "", +// address2: user.profile?.address2 || user.address2 || "", +// city: user.profile?.city || user.city || "", +// state: user.profile?.state || user.state || "", +// country: user.profile?.country || user.country || "", +// zip: user.profile?.zip || "", +// notes: user.profile?.notes || "", +// latitude: user.latitude || 0, +// longitude: user.longitude || 0, +// 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 +// } +// }; +// } +// } + +// const response = Object.values(uniqueCustomerMap); + +// return reply.send({ +// status_code: 200, +// data: response +// }); + +// } catch (error) { +// console.error("Error fetching disconnected customer details:", error); +// return reply.code(500).send({ error: "Internal server error" }); +// } +// }; + exports.getDisconnectedMoveCustomerDetails = async (req, reply) => { try { const { supportId } = req.params; @@ -6520,104 +6626,113 @@ exports.getDisconnectedMoveCustomerDetails = async (req, reply) => { return reply.code(400).send({ error: "supportId is required" }); } - // 1. Fetch support record const supportRecord = await Support.findOne({ supportId }).lean(); if (!supportRecord) { return reply.code(404).send({ message: "No support record found for this supportId" }); } - // 2. Only proceed if categorizedIssues exist - if (!Array.isArray(supportRecord.categorizedIssues) || supportRecord.categorizedIssues.length === 0) { - return reply.code(404).send({ message: "No categorized issues to process" }); - } - - // 3. Collect hardware IDs from categorized issues const categorizedHardwareIds = []; - for (const issue of supportRecord.categorizedIssues) { + const resolvedHardwareIds = []; + + for (const issue of supportRecord.categorizedIssues || []) { if (issue.hardwareId) categorizedHardwareIds.push(issue.hardwareId); if (Array.isArray(issue.hardwareIds)) categorizedHardwareIds.push(...issue.hardwareIds); } - if (categorizedHardwareIds.length === 0) { - return reply.code(404).send({ message: "No hardware IDs in categorized issues" }); + for (const issue of supportRecord.resolvedIssues || []) { + if (issue.hardwareId) resolvedHardwareIds.push(issue.hardwareId); + if (Array.isArray(issue.hardwareIds)) resolvedHardwareIds.push(...issue.hardwareIds); + } + + const allHardwareIds = [...new Set([...categorizedHardwareIds, ...resolvedHardwareIds])]; + if (allHardwareIds.length === 0) { + return reply.code(404).send({ message: "No hardware IDs in support issues" }); } - // 4. Get disconnected sensors from Insensors - const disconnectedSensors = await Insensors.find({ + const allSensors = await Insensors.find({ $or: [ - { hardwareId: { $in: categorizedHardwareIds } }, - { connected_to: { $in: categorizedHardwareIds } } - ], - connected_status: "disconnected" + { hardwareId: { $in: allHardwareIds } }, + { connected_to: { $in: allHardwareIds } } + ] }).lean(); - if (disconnectedSensors.length === 0) { - return reply.code(404).send({ message: "No disconnected sensors found" }); + if (allSensors.length === 0) { + return reply.code(404).send({ message: "No sensors found for support issues" }); } - // 5. Get unique customerIds - const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))]; + const customerStatusMap = {}; - // 6. Fetch corresponding customers - const customers = await User.find({ customerId: { $in: customerIds } }).lean(); + for (const sensor of allSensors) { + const cid = sensor.customerId; + if (!cid) continue; - const uniqueCustomerMap = {}; + if (!customerStatusMap[cid]) { + customerStatusMap[cid] = { status: "unknown" }; + } - for (const user of customers) { - const cid = user.customerId; - if (!uniqueCustomerMap[cid]) { - uniqueCustomerMap[cid] = { - customer: { - customerId: cid, - username: user.username || "", - firstName: user.profile?.firstName || user.firstName || "", - lastName: user.profile?.lastName || user.lastName || "", - phone: user.phone || user.profile?.contactNumber || user.alternativeNumber || "", - email: user.emails?.[0]?.email || user.email || "", - phoneVerified: user.phoneVerified || false, - address1: user.profile?.address1 || user.address1 || "", - address2: user.profile?.address2 || user.address2 || "", - city: user.profile?.city || user.city || "", - state: user.profile?.state || user.state || "", - country: user.profile?.country || user.country || "", - zip: user.profile?.zip || "", - notes: user.profile?.notes || "", - latitude: user.latitude || 0, - longitude: user.longitude || 0, - 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 - } - }; + if (resolvedHardwareIds.includes(sensor.hardwareId) || resolvedHardwareIds.includes(sensor.connected_to)) { + customerStatusMap[cid].status = "resolved"; + } else if (sensor.connected_status === "disconnected") { + customerStatusMap[cid].status = "disconnected"; + } else if (sensor.connected_status === "connected" && customerStatusMap[cid].status !== "disconnected" && customerStatusMap[cid].status !== "resolved") { + customerStatusMap[cid].status = "connected"; } } - const response = Object.values(uniqueCustomerMap); + const customerIds = Object.keys(customerStatusMap); + const users = await User.find({ customerId: { $in: customerIds } }).lean(); + + const combinedCustomerList = users.map(user => { + const cid = user.customerId; + return { + customerId: cid, + connectionStatus: customerStatusMap[cid]?.status || "unknown", + username: user.username || "", + firstName: user.profile?.firstName || user.firstName || "", + lastName: user.profile?.lastName || user.lastName || "", + phone: user.phone || user.profile?.contactNumber || user.alternativeNumber || "", + email: user.emails?.[0]?.email || user.email || "", + phoneVerified: user.phoneVerified || false, + address1: user.profile?.address1 || user.address1 || "", + address2: user.profile?.address2 || user.address2 || "", + city: user.profile?.city || user.city || "", + state: user.profile?.state || user.state || "", + country: user.profile?.country || user.country || "", + zip: user.profile?.zip || "", + notes: user.profile?.notes || "", + latitude: user.latitude || 0, + longitude: user.longitude || 0, + 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 + }; + }); return reply.send({ status_code: 200, - data: response + data: combinedCustomerList }); } catch (error) { - console.error("Error fetching disconnected customer details:", error); + console.error("Error in getDisconnectedMoveCustomerDetails:", error); return reply.code(500).send({ error: "Internal server error" }); } }; + exports.getResolvedCustomerDetails = async (req, reply) => { try { const { supportId } = req.params;